[英]MailKit.Net.Imap read Gmail attachments when “Content-Disposition; inline” is set
I have ac# console application that I use to search a Gmail account for emails with attachments and download them. 我有一个ac#console应用程序,用于在Gmail帐户中搜索带附件的电子邮件并下载它们。 I am using MailKit.Net.Imap.ImapClient to do this.
我正在使用MailKit.Net.Imap.ImapClient来执行此操作。
If the email has been sent with the "Content-Disposition; attachment" set then all works well. 如果电子邮件已经与“内容处置;附件”设置一起发送,则一切正常。 If the email has been sent with "Content-Disposition; inline" set, the ImapClient does not see the attachments and I can't download them.
如果电子邮件已通过“Content-Disposition; inline”设置发送,则ImapClient不会看到附件,我无法下载它们。
Below is the code I use to do this. 下面是我用来执行此操作的代码。 Does anyone have any idea how to fix this?
有谁知道如何解决这个问题?
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); } }
You can use the BodyParts
property instead of the Attachments
property. 您可以使用
BodyParts
属性而不是Attachments
属性。
For example, you could do this: 例如,您可以这样做:
foreach (MimePart part in message.BodyParts.OfType<MimePart> ().Where (x => x.IsAttachment || (/* other criteria that suggests an attachment */))
Console.WriteLine("\t* " + part.FileName);
If you want to treat all MimeParts with a Content-Disposition filename attribute or a Content-Type name attribute set, then you could do: 如果要使用Content-Disposition文件名属性或Content-Type名称属性集处理所有MimeParts,则可以执行以下操作:
!string.IsNullOrEmpty (x.FileName)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.