保存附加邮件的附件

时间:2013-05-06 13:22:51

标签: c# imap

我正在使用来自limilabs的Mail.dll来管理IMAP文件夹。 有一封邮件附件是一个eml文件,所以是一封邮件。 它又有一个我需要提取的附加的eml文件。

所以电子邮件结构如下:

Email
  |- Attachment: file.eml
                   |- Attachment file2.eml

这是我的代码:

IMail email = new MailBuilder().CreateFromEml(imap.GetMessageByUID(uid));
Console.WriteLine(email.Subject);
// save all attachments to disk
foreach(MimeData mime in email.Attachments)
{
if (uid == 1376)
{
        System.IO.Directory.CreateDirectory(string.Format(@"c:\EMAIL\{0}", uid));
        mime.Save(@"c:\EMAIL\" + uid + "\\" + mime.SafeFileName);
        MimeData help;
        if (mime.ContentType.ToString() == "message/rfc822")
        {
        //i need to cast this attach in a imail
        }
    }
}

如何提取最内层的eml文件(上述结构中的file2.eml)?

2 个答案:

答案 0 :(得分:2)

this link开始,您应该可以执行以下操作:

if (attachment.ContentType == ContentType.MessageRfc822)
{
    string eml = ((MimeText)attachment).Text;
    IMail attachedMessage = new MailBuilder().CreateFromEml(eml);
    // process further
}

答案 1 :(得分:0)

如果您只需要从所有内部消息中提取所有附件,则可以使用 IMail.ExtractAttachmentsFromInnerMessages 方法:

IMail email = new MailBuilder().CreateFromEml(imap.GetMessageByUID(uid));

ReadOnlyCollection<MimeData> attachments = mail.ExtractAttachmentsFromInnerMessages();
foreach (MimeData mime in attachments)
{
     mime.Save(@"c:\" + mime.SafeFileName);
}