MailKit:如何从MimeMessage在本地下载所有附件 [英] MailKit: How to download all attachments locally from a MimeMessage

查看:476
本文介绍了MailKit:如何从MimeMessage在本地下载所有附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在线查看了其他示例,但是无法弄清楚如何从MimeMessage对象下载和存储所有附件. 我确实调查了WriteTo(),但无法正常工作. 还想知道是否会根据原始文件名保存附件,然后在电子邮件中键入内容. 这是我到目前为止的内容:

I have looked at other examples online, but I am unable to figure out how to download and store ALL the attachments from a MimeMessage object. I did look into the WriteTo(), but I could not get it to work. Also wondering whether attachments will be saved according to the original file name, and type inside the email. Here is what I have so far:

using (var client = new ImapClient())
{
    client.Connect(Constant.GoogleImapHost, Constant.ImapPort, SecureSocketOptions.SslOnConnect);
    client.AuthenticationMechanisms.Remove(Constant.GoogleOAuth);
    client.Authenticate(Constant.GoogleUserName, Constant.GenericPassword);

    if (client.IsConnected == true)
    {
        FolderAccess inboxAccess = client.Inbox.Open(FolderAccess.ReadWrite);
        IMailFolder inboxFolder = client.GetFolder(Constant.InboxFolder);
        IList<UniqueId> uids = client.Inbox.Search(SearchQuery.All);

        if (inboxFolder != null & inboxFolder.Unread > 0)
        {
            foreach (UniqueId msgId in uids)
            {
                MimeMessage message = inboxFolder.GetMessage(msgId);

                foreach (MimeEntity attachment in message.Attachments)
                {
                    //need to save all the attachments locally
                }
            }
        }
    }
}

推荐答案

这全部在常见问题解答在我如何保存附件?"中部分.

This is all explained in the FAQ in the "How do I save attachments?" section.

这是您在问题中发布的代码的固定版本:

Here is a fixed version of the code you posted in your question:

using (var client = new ImapClient ()) {
    client.Connect (Constant.GoogleImapHost, Constant.ImapPort, SecureSocketOptions.SslOnConnect);
    client.AuthenticationMechanisms.Remove (Constant.GoogleOAuth);
    client.Authenticate (Constant.GoogleUserName, Constant.GenericPassword);

    client.Inbox.Open (FolderAccess.ReadWrite);
    IList<UniqueId> uids = client.Inbox.Search (SearchQuery.All);

    foreach (UniqueId uid in uids) {
        MimeMessage message = client.Inbox.GetMessage (uid);

        foreach (MimeEntity attachment in message.Attachments) {
            var fileName = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name;

            using (var stream = File.Create (fileName)) {
                if (attachment is MessagePart) {
                    var rfc822 = (MessagePart) attachment;

                    rfc822.Message.WriteTo (stream);
                } else {
                    var part = (MimePart) attachment;

                    part.Content.DecodeTo (stream);
                }
            }
        }
    }
}

一些注意事项:

  1. 验证后无需检查client.IsConnected是否.如果未连接,它将在Authenticate()方法中引发异常.如果不成功,它也会在Connect()方法中引发异常.如果您只是叫出Connect() 2行,则无需检查IsConnected状态.
  2. 如果什至没有在任何地方使用它,为什么还要检查inboxFolder.Unread?如果您只想下载未读的邮件,请将搜索更改为SearchQuery.NotSeen,这样只会为您提供未读的邮件UID.
  3. 我删除了您的IMailFolder inboxFolder = client.GetFolder(Constant.InboxFolder);逻辑,因为您不需要它.如果要使用client.Inbox进行搜索,请不要使用其他文件夹对象遍历结果.
  1. There's no need to check if client.IsConnected after authenticating. If it wasn't connected, it would have thrown an exception in the Authenticate() method. It would have thrown an exception in the Connect() method as well if it didn't succeed. There is no need to check the IsConnected state if you literally just called Connect() 2 lines up.
  2. Why are you checking inboxFolder.Unread if you don't even use it anywhere? If you just want to download unread messages, change your search to be SearchQuery.NotSeen which will give you only the message UIDs that have not been read.
  3. I removed your IMailFolder inboxFolder = client.GetFolder(Constant.InboxFolder); logic because you don't need it. If you are going to do the SEARCH using client.Inbox, then don't iterate over the results with a different folder object.

这篇关于MailKit:如何从MimeMessage在本地下载所有附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆