我如何从OpenPOP库的MessageRaw中提取附件文件 [英] How i can extract attachment file from MessageRaw in OpenPOP library
问题描述
嗨
我将OpenPOP库用于发送和接收电子邮件.
我程序的一部分是将收到的电子邮件保存到数据库中.
在程序的另一部分中,我必须向用户显示保存在数据库中的附件文件.
在我的保存方法中,我将RawMessage(包括标头,发件人,附件等)保存到数据库中.
Hi
I use OpenPOP library for Send and Recive Email.
A part of my program is save recived email to database.
In another part of my program i must show attachment files that saved into database to user.
In my save method, i save RawMessage (include headers, sender, attachments and etc) into database.
<br />
<pre lang="cs"><br />
var MailContent = GetMessage(MessageNumber);<br />
DB.RawMessage = (from a in MailContent.RawMessage<br />
select (byte)a).ToArray();</pre><br />
.<br />
.<br />
.<br />
//finaly save DB into Database<br />
现在我的问题是:如何从保存的RawMessage中提取附件文件?
Now my question is: How i can extract attachment files from saved RawMessage?
推荐答案
请检查此 ^ ]以获得附件上的代码.
像
Hi, check this blog[^] for the code on attachment.
Something like
OpenPOP.MIMEParser.Message msg = MailContent.GetMessage(x, false);
if (msg != null) {
for (int y = 0; y < msg.AttachmentCount; y++) {
Attachment attachment = (Attachment)msg.Attachments[y];
if (string.Compare(attachment.ContentType, "text/xml") == 0) {
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
string xml = attachment.DecodeAsText();
doc.LoadXml(xml);
doc.Save(@"C:\POP3Temp\test.xml");
}
}
}
嗨 谢谢您的回复.
我用下面的代码解决了我的问题:
首先从OpenPOP.Mime.Message创建一个消息对象:
Hi Thank you for your response.
I solve my problem with below code:
First creat a message object from OpenPOP.Mime.Message:
string StringRawContent = System.Text.ASCIIEncoding.ASCII.GetString(DB.RawMessage.ToArray());
OpenPOP.MIME.Message raw = new OpenPOP.MIME.Message(true, StringRawContent, false);
现在,我们在原始对象中包含了电子邮件的所有部分,例如主题,正文,发件人,附件等.
例如,我通过以下代码获取第一个附件文件:
Now we have all parts of email such as Subject, Body, Sender, Attachments and etc in raw object.
For example i get first attachment file via below code:
if (raw.Attachments.Count > 0)
AttachmentFileContent = (from a in raw.Attachments.First().RawContent
select (byte)a).ToArray();
这篇关于我如何从OpenPOP库的MessageRaw中提取附件文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!