2009-07-13 10 views
10

los foratterを使用してメッセージをシリアライズするときは、次のようになります。メールメッセージをシリアル化するにはどうすればよいですか?

エラー:Sys.WebForms.PageRequestManagerServerErrorException: 'System.Net.Mail.MailMessage'タイプの値 'System.Net.Mail.MailMessage'をシリアル化する際にエラーが発生しました。

このオブジェクトをシリアル化する簡単な方法はありますか、それともそれぞれのプロパティを個別に調べる必要がありますか?

+0

どこにシリアル化しようとしていますか? –

+0

私はそれをバイト配列に入れようとしています。 – Middletone

答えて

0

悲しいことに、System.Net.Mail.MailMessageクラスはシリアライズ可能とマークされていません。だから、はい、あなたはそれを自分で行う必要があります。次のブログの記事で説明しているように、どのように進行させるかを知る方法があります。How to Serialize a MailMessage ...基本的には、それぞれのプロパティを個別に取り出す必要があります。引用:

To serialize the properties of a MailMessage object you can create a new class and create a property of MailMessage type for it that embeds your MailMessage in the class. In this new class you can implement IXmlSerializable interface to manually serialize its MailMessage. Here I create this class and call it SerializableMailMessage [...]

[code implementation of WriteXml() and ReadXml() methods follow; see source link]

+0

リンクはブログのホームページにリダイレクトされます。 –

+0

リンクが壊れています。 –

+0

@GunnarSteinnありがとうございました。 –

2

私はこれは古い記事ですけど、私はまた、私は、シリアライズ版を作成したので、MailAddressクラスをシリアル化するために必要な問題がありました。 System.Net.Mail.MailAddressクラスの代わりにカスタムMailAddressクラスを使用する立場にある場合は、これが有効です。 Keyvan Nayyeriによって書かれたXMLにはMailMessageをシリアライズする方法上の完全なソースコードをお探しの方に

/// <summary> 
/// Serializable implementation of <see cref="System.Net.Mail.MailAddress"/>. 
/// </summary> 
[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))) 
    { 
    } 
} 
2

、ここで彼のgithubのへのリンクです:

https://github.com/keyvan/Gopi/blob/master/Gopi/Gopi/SerializableMailMessage.cs

そしてここにはあります今は亡きである彼のブログのアーカイブ:

http://web.archive.org/web/20131124074822/http://keyvan.io/how-to-serialize-a-mailmessage

は、念のために上記のリンクはIここに再掲載彼のブログここで、あまりにも死者になる可能性があります:

.NETの2はMailMessageクラスがありますどのようにはMailMessage

をシリアル化します。最初のものはSystem.Web.Mail名前空間にあり、.NET 2.0以降では廃止されており、2つ目はSystem.Net.Mailで使用可能です。私は廃止されたものを気にしないので、System.Net.Mail.MailMessageクラスのインスタンスをどのようにシリアライズできるかを示します。

MailMessageオブジェクトのプロパティをシリアル化するには、新しいクラスを作成し、そのクラスにMailMessageを埋め込むMailMessage型のプロパティを作成します。この新しいクラスでは、IXmlSerializableインターフェイスを実装して、MailMessageを手動でシリアル化できます。作業の

シリアライズ

シリアライズ側はWriteXmlメソッドを実装するのと同じくらい簡単です。以下のコードは私がこれを行うために書いたものです。

public void WriteXml(XmlWriter writer) 
{ 
    if (this != null) 
    { 
     writer.WriteStartElement("MailMessage"); 
     writer.WriteAttributeString("Priority", Convert.ToInt16(this.Priority).ToString()); 
     writer.WriteAttributeString("IsBodyHtml", this.IsBodyHtml.ToString()); 

     // From 
     writer.WriteStartElement("From"); 
     if (!string.IsNullOrEmpty(this.From.DisplayName)) 
      writer.WriteAttributeString("DisplayName", this.From.DisplayName); 
     writer.WriteRaw(this.From.Address); 
     writer.WriteEndElement(); 

     // To 
     writer.WriteStartElement("To"); 
     writer.WriteStartElement("Addresses"); 
     foreach (MailAddress address in this.To) 
     { 
      writer.WriteStartElement("Address"); 
      if (!string.IsNullOrEmpty(address.DisplayName)) 
       writer.WriteAttributeString("DisplayName", address.DisplayName); 
      writer.WriteRaw(address.Address); 
      writer.WriteEndElement(); 
     } 
     writer.WriteEndElement(); 
     writer.WriteEndElement(); 

     // CC 
     if (this.CC.Count > 0) 
     { 
      writer.WriteStartElement("CC"); 
      writer.WriteStartElement("Addresses"); 
      foreach (MailAddress address in this.CC) 
      { 
       writer.WriteStartElement("Address"); 
       if (!string.IsNullOrEmpty(address.DisplayName)) 
        writer.WriteAttributeString("DisplayName", address.DisplayName); 
       writer.WriteRaw(address.Address); 
       writer.WriteEndElement(); 
      } 
      writer.WriteEndElement(); 
      writer.WriteEndElement(); 
     } 

     // Bcc 
     if (this.Bcc.Count > 0) 
     { 
      writer.WriteStartElement("Bcc"); 
      writer.WriteStartElement("Addresses"); 
      foreach (MailAddress address in this.Bcc) 
      { 
       writer.WriteStartElement("Address"); 
       if (!string.IsNullOrEmpty(address.DisplayName)) 
        writer.WriteAttributeString("DisplayName", address.DisplayName); 
       writer.WriteRaw(address.Address); 
       writer.WriteEndElement(); 
      } 
      writer.WriteEndElement(); 
      writer.WriteEndElement(); 
     } 

     // Subject 
     writer.WriteStartElement("Subject"); 
     writer.WriteRaw(this.Subject); 
     writer.WriteEndElement(); 

     // Body 
     writer.WriteStartElement("Body"); 
     writer.WriteCData(Body); 
     writer.WriteEndElement(); 

     writer.WriteEndElement(); 
    } 
} 

同じアプローチで、添付ファイルのような他のプロパティをシリアル化できます。来週、Gopiの新しいバージョンをリリースします.Gopiには、コードを手に入れるためにすべてをシリアライズする更新されたコードが含まれています。上記のコード

デシリアライズ

public void ReadXml(XmlReader reader) 
{ 
    XmlDocument xml = new XmlDocument(); 
    xml.Load(reader); 

    // Properties 
    XmlNode rootNode = GetConfigSection(xml, "SerializableMailMessage/MailMessage"); 
    this.IsBodyHtml = Convert.ToBoolean(rootNode.Attributes["IsBodyHtml"].Value); 
    this.Priority = (MailPriority)Convert.ToInt16(rootNode.Attributes["Priority"].Value); 

    // From 
    XmlNode fromNode = GetConfigSection(xml, "SerializableMailMessage/MailMessage/From"); 
    string fromDisplayName = string.Empty; 
    if (fromNode.Attributes["DisplayName"] != null) 
     fromDisplayName = fromNode.Attributes["DisplayName"].Value; 
    MailAddress fromAddress = new MailAddress(fromNode.InnerText, fromDisplayName); 
    this.From = fromAddress; 

    // To 
    XmlNode toNode = GetConfigSection(xml, "SerializableMailMessage/MailMessage/To/Addresses"); 
    foreach (XmlNode node in toNode.ChildNodes) 
    { 
     string toDisplayName = string.Empty; 
     if (node.Attributes["DisplayName"] != null) 
      toDisplayName = node.Attributes["DisplayName"].Value; 
     MailAddress toAddress = new MailAddress(node.InnerText, toDisplayName); 
     this.To.Add(toAddress); 
    } 

    // CC 
    XmlNode ccNode = GetConfigSection(xml, "SerializableMailMessage/MailMessage/CC/Addresses"); 
    foreach (XmlNode node in ccNode.ChildNodes) 
    { 
     string ccDisplayName = string.Empty; 
     if (node.Attributes["DisplayName"] != null) 
      ccDisplayName = node.Attributes["DisplayName"].Value; 
     MailAddress ccAddress = new MailAddress(node.InnerText, ccDisplayName); 
     this.CC.Add(ccAddress); 
    } 

    // Bcc 
    XmlNode bccNode = GetConfigSection(xml, "SerializableMailMessage/MailMessage/Bcc/Addresses"); 
    foreach (XmlNode node in bccNode.ChildNodes) 
    { 
     string bccDisplayName = string.Empty; 
     if (node.Attributes["DisplayName"] != null) 
      bccDisplayName = node.Attributes["DisplayName"].Value; 
     MailAddress bccAddress = new MailAddress(node.InnerText, bccDisplayName); 
     this.Bcc.Add(bccAddress); 
    } 

    // Subject 
    XmlNode subjectNode = GetConfigSection(xml, "SerializableMailMessage/MailMessage/Subject"); 
    this.Subject = subjectNode.InnerText; 

    // Body 
    XmlNode bodyNode = GetConfigSection(xml, "SerializableMailMessage/MailMessage/Body"); 
    this.Body = bodyNode.InnerText; 
} 

異なるセクションのためのXmlNodeを取得するためのヘルパーメソッドを使用しています。

public XmlNode GetConfigSection(XmlDocument xml, string nodePath) 
{ 
    return xml.SelectSingleNode(nodePath); 
} 

このコードは自明であり、任意のコメントを必要としますが、あなたのクラス名用のXPath式を更新する必要があることに注意しません。もちろん、私はこのコードをより一般的にするためにリフレクションを使用できますが、時には私は悪い人です!

関連する問題