【问题标题】:Get attachments from E-mail从电子邮件中获取附件
【发布时间】:2020-11-06 23:30:35
【问题描述】:

我正在尝试从 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();
            }
         }
     }
 }

但问题是,现在它显然会写入 fullContent,因为我使用的是 ms.CopyTo(file),这不是我想要的。

我正在尝试获取电子邮件的附件并将其写入文件。

【问题讨论】:

标签: c# file filestream mime mime-message


【解决方案1】:

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

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

感谢我访问了MimePart,我能够获得Stream,然后我将该流放入另一个using (stream stream = part.content.stream),并将该流用于stream.CopyTo(file)

【讨论】:

  • 你有没有像我建议的那样打电话给attachment.WriteTo
  • 老实说我没有,虽然我认为它也会起作用.. 这是因为我面前有 2 个选项,我只是来回走动,直到一个最终起作用。然后我注意到了你的消息。
猜你喜欢
  • 1970-01-01
  • 2012-08-23
  • 2011-08-20
  • 1970-01-01
  • 2013-04-24
  • 2016-11-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
相关资源
最近更新 更多