MailAttachment 序列化和反序列化不起作用

发布于 2024-10-19 03:01:34 字数 2551 浏览 3 评论 0原文

我正在尝试使用 IXmlSerialized 接口的实现来序列化 MailMessage 对象。然后,序列化的对象使用 Image DataType 存储在数据库(即使用 SQL Server CE 3.5)中。除了附件集合之外,反序列化时一切正常。反序列化时,图像已附加,但在电子邮件中未正确显示,文本文件为空。

这是要反序列化的代码(仅附件列表部分)

 // 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);
        }

这是要序列化的代码

// 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 (BinaryReader reader = new BinaryReader(attachment.ContentStream))
                        {
                            byte[] data = reader.ReadBytes((int)attachment.ContentStream.Length);

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

                        writer.WriteEndElement();
                    }

                    writer.WriteEndElement();
                }

我从 CodePlex 上的 GOPI C# 邮件发送库获取了此代码 http://gopi.codeplex.com/

即使在问题跟踪器中,这也是一个问题。请告知可能出了什么问题。

编辑1:抱歉大家,我已经发布了我的试用代码。现在显示了正确的代码。(在 writer.WriteBase64(data, 0, data.Length); 的序列化代码中

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 (BinaryReader 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2024-10-26 03:01:34

您在序列化时转换为 Base64,但在反序列化时不这样做

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

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

byte[] data = Convert.FromBase64String (node.InnerText);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文