小寒的blog
Programming is a darned hard thing—but I am going to like it.
class App
    {
        
public static void Main(string[] msgs)
        {
            Console.Title 
= "Serialize MailMessage";

            SerializableMailMessage mailToSerialize 
= new SerializableMailMessage();

            mailToSerialize.From 
= new MailAddress("xhan@test.com""xhan");
            mailToSerialize.To.Add(
new MailAddress("xhan@test.com""Test1"));
            mailToSerialize.To.Add(
new MailAddress("test2@test.com"));
            mailToSerialize.CC.Add(
new MailAddress("test3@test.com""Test3"));
            mailToSerialize.Bcc.Add(
new MailAddress("test4@test.com"));
            mailToSerialize.Subject 
= "Test Email";
            mailToSerialize.Body 
= "<p>This is a test email for serialization!</p>";
            mailToSerialize.IsBodyHtml 
= true;
            mailToSerialize.Priority 
= MailPriority.High;

            mailToSerialize.Attachments.Add(
new Attachment(@"e:\247677.jpg"));


            
string xmlMail = SerializableMailMessage.Serialize(mailToSerialize);

            Console.WriteLine(xmlMail);

            SerializableMailMessage mailToDeserialize 
= SerializableMailMessage.Deserialize(xmlMail);
           

            SmtpClient client 
= new SmtpClient();
            client.Credentials 
= new System.Net.NetworkCredential("xhan@test.com""testxxx");
            client.Host 
= "smtp.test.com";
            client.DeliveryMethod 
= SmtpDeliveryMethod.Network;
            client.Send(mailToDeserialize);

            Console.ReadKey();
        
        }
    }


using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.IO;

namespace ProcessingApplication.Message
{
    
public class SerializableMailMessage :MailMessage,IXmlSerializable
    {
        
public MailMessage Email { getset; }

        
public XmlSchema GetSchema()
        {
            
return null;
        }

        
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();
                
//Attachments
                if (this.Attachments.Count > 0)
                {
                    writer.WriteStartElement(
"Attachments");
                    
foreach (Attachment attachment in this.Attachments)
                    {
                        writer.WriteStartElement(
"Attachment");

                        writer.WriteStartElement(
"Name");
                        writer.WriteString(attachment.Name);
                        writer.WriteEndElement();

                        writer.WriteStartElement(
"Data");
                        BinaryReader br 
= new BinaryReader(attachment.ContentStream);
                        
byte[] bytes = br.ReadBytes((int)attachment.ContentStream.Length);
                        writer.WriteBase64(bytes, 
0, bytes.Length);
                        writer.WriteEndElement();

                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();

            }

        }


        
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");

            
if (ccNode != null)
            {
                
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");

            
if (bccNode != null)
            {
                
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 attachmentsNode 
= GetConfigSection(xml, "SerializableMailMessage/MailMessage/Attachments");
            
if (attachmentsNode != null)
            {
                
foreach (XmlNode node in attachmentsNode.ChildNodes)
                {
                    
byte[] bytes = System.Convert.FromBase64String(node.LastChild.InnerText);
                    Stream stream 
= new MemoryStream(bytes);
                    Attachment attachment 
= new Attachment(stream, node.FirstChild.InnerText);
                    
this.Attachments.Add(attachment);
                }
            }
        }


        
public XmlNode GetConfigSection(XmlDocument xml, string nodePath)
        {

            
return xml.SelectSingleNode(nodePath);

        }

        
public static string Serialize(SerializableMailMessage mailMessage)
        { 
            XmlSerializer serializer 
= new XmlSerializer(typeof(SerializableMailMessage));

            
using (StringWriter stringWriter = new StringWriter())
            {
                serializer.Serialize(stringWriter, mailMessage);

                
return stringWriter.ToString();
            }
        }
        
public static SerializableMailMessage Deserialize(string xmlMail)
        {
            XmlSerializer serializer 
= new XmlSerializer(typeof(SerializableMailMessage));

            
using (StringReader stringReader = new StringReader(xmlMail))
            {
                SerializableMailMessage mailMessage 
= serializer.Deserialize(stringReader) as SerializableMailMessage;
                
return mailMessage;
            }
        }

    }

    
}


posted on 2010-01-12 17:08  xhan  阅读(720)  评论(0编辑  收藏  举报