MailAttachment 序列化和反序列化不起作用
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在序列化时转换为 Base64,但在反序列化时不这样做
You Convert to Base64 on the serialize but you don't do that on the deserialize