从电子邮件中获取附件



我正在尝试从MimeMessage获取附件。

我做到了:

using (Stream ms = Utils.GenerateStreamFromString(mail.Rfc822Content))
{
MimeMessage mimeMessage = MimeMessage.Load(ms);
// collect our list of attachments
foreach (MimeEntity attachment in mimeMessage.Attachments)
{
if (!fileexists)
{
using (FileStream file = new FileStream(System.IO.Path.Combine(System.IO.Path.Combine(docMap), bijlageNaam), FileMode.Create, FileAccess.Write))
{
ms.Position = 0;
ms.CopyTo(file);
file.Flush();
}
}
}
}

但问题是,现在它讨厌地写完整的内容,因为我使用的是ms.CopyTo(file),这不是我想要的。

我正在尝试将附件附加到电子邮件中,并将其写入文件。

我通过用using (var iter = new MimeIterator(mimeMessage))替换foreach解决了问题

并使用while(item.MoveNext())开始循环

由于这一点,我访问了MimePart,我能够获得的Stream,我将该流放入另一个using (stream stream = part.content.stream)中,并将该流用于stream.CopyTo(file)

最新更新