简体   繁体   English

如何使用MailKit为每封邮件下载多个附件?

[英]How to download multiple attachments per message with MailKit?

I've looked at a lot of the other questions on stackoverflow about this, but I'm still confused. 我已经看过很多关于stackoverflow的其他问题,但是我仍然感到困惑。

I want to download the attachments of emails- I was successfully able to do this, but only if the email had ONE attachment; 我想下载电子邮件的附件-我成功地做到了这一点,但前提是该电子邮件只有一个附件。 when an email has more than one attachment, it stops working. 当一封电子邮件有多个附件时,它将停止工作。

How do I download multiple attachments per email? 如何为每个电子邮件下载多个附件?

Also, is there a way to determine the file extension when downloading? 另外,有没有一种方法可以在下载时确定文件扩展名? Currently, for example, if there is a pdf attachment, the file downloads, but with no .pdf, so windows doesn't know the file type. 例如,当前,如果有pdf附件,则将下载文件,但没有.pdf,因此Windows不知道文件类型。

The code below is from here: MailKit save Attachments . 下面的代码来自这里: MailKit保存附件 I've been basing my code off of that. 我一直以此为基础。

foreach (var attachment in message.Attachments)
{
    using (var stream = File.Create ("fileName"))
    {
        if (attachment is MessagePart)
        {
            var part = (MessagePart) attachment;
            part.Message.WriteTo (stream);
        }
        else
        {
            var part = (MimePart) attachment;
            part.ContentObject.DecodeTo (stream);
        }
    }
}

Please help! 请帮忙! Thanks! 谢谢!

The code you pasted will already save all attachments. 您粘贴的代码将已经保存所有附件。

Look at the raw message source. 查看原始消息源。 Does each "attachment" that you consider to be an attachment have a header Content-Disposition: attachment ? 您认为是附件的每个“附件”是否都有标题Content-Disposition: attachment If not, that's the problem you are hitting. 如果没有,那就是您要解决的问题。

You can instead use message.BodyParts and apply your own logic to determine if the part is what you would consider to be an "attachment". 您可以改用message.BodyParts并应用自己的逻辑来确定零件是否就是您认为的“附件”。

Also, is there a way to determine the file extension when downloading? 另外,有没有一种方法可以在下载时确定文件扩展名? Currently, for example, if there is a pdf attachment, the file downloads, but with no .pdf, so windows doesn't know the file type. 例如,当前,如果有pdf附件,则将下载文件,但没有.pdf,因此Windows不知道文件类型。

Yes. 是。 You can check the file extension on the FileName property on MimePart objects. 您可以在MimePart对象的FileName属性上检查文件扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM