当" Content-Disposition时,MailKit.Net.Imap会读取Gmail附件;直列"已设定

时间:2017-05-31 00:47:23

标签: c# .net mailkit

我有一个c#控制台应用程序,用于在Gmail帐户中搜索带附件的电子邮件并下载它们。我正在使用MailKit.Net.Imap.ImapClient来执行此操作。

如果电子邮件已与" Content-Disposition一起发送;附件"然后设置好一切。如果电子邮件已与" Content-Disposition一起发送;直列"设置,ImapClient没有看到附件,我无法下载它们。

以下是我用来执行此操作的代码。有没有人知道如何解决这个问题?



        static public void mail_ReadEmail()
        {
            string mail_login = "your login here";
            string mail_password = "your password here";
            string mail_folderName = "your gmail folder name here";

            // Get client & open folder
            ImapClient client = mail_GetImapClient(mail_login, mail_password);
            IMailFolder folder = mail_GetIMailFolder(client, mail_folderName);

            // Get emails
            DateTime startTime = DateTime.Now.AddMonths(-6);
            IList<UniqueId> email = folder.Search(SearchQuery
                .DeliveredAfter(startTime)
                .And(SearchQuery
                .FromContains("canon.co.nz")));

            // Loop through emails (oldest first)
            for (int i = email.Count - 1; i >= 0; i--)
            {
                // Get message and display subject and date
                MimeMessage message = folder.GetMessage(email[i]);
                Console.WriteLine(message.Subject + " - " + message.Date.DateTime.ToShortDateString());

                // Show all attachments for this message
                foreach (MimePart part in message.Attachments)
                    Console.WriteLine("\t* " + part.FileName);
            }
        }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以使用BodyParts属性而不是Attachments属性。

例如,你可以这样做:

foreach (MimePart part in message.BodyParts.OfType<MimePart> ().Where (x => x.IsAttachment || (/* other criteria that suggests an attachment */))
    Console.WriteLine("\t* " + part.FileName);

如果您想使用Content-Disposition filename属性或Content-Type name属性集来处理所有MimeParts,那么您可以这样做:

!string.IsNullOrEmpty (x.FileName)