开发者

MailAttachment Serialization and Deserialization not working

开发者 https://www.devze.com 2023-02-13 19:16 出处:网络
I am trying to serialize a MailMessage object using implementation of IXmlSerializable interface. The serialized object is then stored in database (that is using SQL Server CE 3.5) using Image DataTyp

I am trying to serialize a MailMessage object using implementation of IXmlSerializable interface. The serialized object is then stored in database (that is using SQL Server CE 3.5) using Image DataType. Everything works fine on deserialization except the Attachment Collection. On deserilzing the Images are attached but do not show up correctly in the Email, Text files are empty.

This is the code to deserialize (only the attachment list part)

 // Attachments
            XmlNode attachmentsNode = GetConfigSection(xml, "SerializableMailMessage/MailMessage/Attachments");
            if (attachmentsNode != null)
            {
                foreach (XmlNode node in attachmentsNode.ChildNodes)
                {
                    string contentTypeString = string.Empty;
                    if (node.Attributes["ContentType"] != null)
                        contentTypeString = node.Attributes["ContentType"].Value;

                    ContentType contentType = new ContentType(contentTypeString);

                    MemoryStream stream = new MemoryStream();
                    byte[] data = Encoding.UTF8.GetBytes(node.InnerText);
                    stream.Write(data, 0, data.Length);

                    Attachment attachment = new Attachment(stream, contentType);
                    this.Email.Attachments.Add(attachment);
                }
            }

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

and this is the code to serialize

// Attachments
                if (this.AttachmentList!=null)
                {
                    writer.WriteStartElement("Attachments");

                    foreach (Attachment attachment in this.AttachmentList)
                    {
                        writer.WriteStartElement("Attachment");

                        if (!string.IsNullOrEmpty(attachment.Name))
                            writer.WriteAttributeString("ContentType", attachment.ContentType.ToString());

                        using (Bina开发者_运维知识库ryReader reader = new BinaryReader(attachment.ContentStream))
                        {
                            byte[] data = reader.ReadBytes((int)attachment.ContentStream.Length);

                            writer.WriteBase64(data, 0, data.Length);
                        }

                        writer.WriteEndElement();
                    }

                    writer.WriteEndElement();
                }

I got this code from the GOPI C# mail sending library on CodePlex http://gopi.codeplex.com/

Even in the issue tracker this is an issue. Kindly advise what may be going wrong.

EDIT 1: Sorry guys, I had posted my tryout code. The correct code is shown now.(in the serialize code at writer.WriteBase64(data, 0, data.Length);


You Convert to Base64 on the serialize but you don't do that on the deserialize

byte[] data = Convert.FromBase64String (node.InnerText);
0

精彩评论

暂无评论...
验证码 换一张
取 消