简体   繁体   English

“内容处理”时MailKit.Net.Imap读取Gmail附件; 内联“已设置

[英]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.

相关问题 ASP.Net 核心内容处理附件/内联 - ASP.Net Core Content-Disposition attachment/inline 如何读取Web API中的内容配置? - How to read the content-disposition in Web API? .NET-WebAPI中未显示内容处置标头 - .NET - Content-Disposition Header Not Showing in WebAPI 带有Content-Disposition Set的XmlDocument下载具有不正确的扩展名 - XmlDocument downloads with incorrect extension even with Content-Disposition Set httpclient - 如何在多部分中将Content-Disposition设置为“application / json;” - httpclient - how to set Content-Disposition to “application/json;” in a multipart 使用Content-Disposition:attachment时XML节点被重写为小写 - XML nodes rewritten to lowercase when using Content-Disposition:attachment Google Cloud Storage不会更新现有的默认Content-Disposition,而是使用.NET Client Libraries创建新的Content-Disposition元数据 - Google Cloud Storage not updating the existing default Content-Disposition but creating a new Content-Disposition metadata with .NET Client Libraries 内容处置始终为 null - Content-Disposition is always null Content-Disposition 标头中的 Unicode - Unicode in Content-Disposition header 获取 Content-Disposition 参数 - Get Content-Disposition parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM