How do I serialize a mail message?

后端 未结 3 535
野的像风
野的像风 2021-02-08 05:02

I get the following when tying to serialize a mial message using the los foratter.

Error: Sys.WebForms.PageRequestManagerServerErrorException: Error serializing value \'

3条回答
  •  旧巷少年郎
    2021-02-08 05:23

    I know this is an older post, but I also had the issue where I needed to serialize the MailAddress class so I created a serializable version. If you're in a position to use a custom MailAddress class instead of the System.Net.Mail.MailAddress class, this may work for you.

    /// 
    /// Serializable implementation of .
    /// 
    [Serializable]
    public class MailAddress : System.Net.Mail.MailAddress, ISerializable
    {
        // Keep reference to the display name encoding so we can serialize/deserialize the value
        private readonly Encoding _displayNameEncoding;
    
        public MailAddress(string address)
            : this(address, null, null)
        {
        }
    
        public MailAddress(string address, string displayName)
            : this(address, displayName, null)
        {
        }
    
        public MailAddress(string address, string displayName, Encoding displayNameEncoding)
            : base(address, displayName, displayNameEncoding)
        {
            // Keep reference to the supplied displayNameEncoding so we can serialize/deserialize this value
            _displayNameEncoding = displayNameEncoding ?? Encoding.GetEncoding("utf-8");
        }
    
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Address", base.Address);
            info.AddValue("DisplayName", base.DisplayName);
            info.AddValue("DisplayNameEncoding", _displayNameEncoding);
        }
    
        protected MailAddress(SerializationInfo info, StreamingContext context)
            : this(info.GetString("Address"), info.GetString("DisplayName"), (Encoding)info.GetValue("DisplayNameEncoding", typeof (Encoding)))
        {
        }
    }
    

提交回复
热议问题