【问题标题】:Getting attachments from gmail using AE.NET not working使用 AE.NET 从 gmail 获取附件不起作用
【发布时间】:2019-04-10 11:31:32
【问题描述】:

我正在使用AE.NET 使用 IMAP 从 Gmail 获取邮件。我能够收到消息,但是当我尝试遍历消息附件时没有。返回 message.Value.Attachments.Count() 给我 0。

using (var imap = new AE.Net.Mail.ImapClient("imap.gmail.com", mailAccount.UserName, mailAccount.Password, AE.Net.Mail.ImapClient.AuthMethods.Login, 993, true))
{
    //Get all new messages
    var msgs = imap.SearchMessages(
        SearchCondition.Unseen()
    );
    string ret = "";

    foreach (var message in msgs)
    {
        foreach (var attachment in message.Value.Attachments)
        {
            //Save the attachment
        }
    }
}

正如我所说,我已将附件计数与邮件主题一起记录下来,并确定邮件正在被检索,但它们没有附件,这是不正确的,因为我可以在 Gmail 中看到附件。

【问题讨论】:

    标签: c# gmail-imap


    【解决方案1】:

    你只在做imap.SearchMessages(SearchCondition.Unseen()),但是这个方法只加载邮件的标题。您需要通过 SearchMessages 方法中获得的消息 ID 使用下面的代码:

    List<string> ids = new List<string>();
    List<AE.Net.Mail.MailMessage> mails = new List<AE.Net.Mail.MailMessage>();
    
    using (var imap = new AE.Net.Mail.ImapClient("imap.gmail.com", mailAccount.UserName, mailAccount.Password, AE.Net.Mail.ImapClient.AuthMethods.Login, 993, true)) 
    {
        var msgs = imap.SearchMessages(SearchCondition.Unseen());
        for (int i = 0; i < msgs.Length; i++) {
            string msgId = msgs[i].Uid;
            ids.Add(msgId);            
        }
    
        foreach (string id in ids)
        {
            mails.Add(imap.GetMessage(id, headersonly: false));
        }
    }
    

    然后使用:

    foreach(var msg in mails)
    {
        foreach (var att in msg.Attachments) 
        {
            string fName;
            fName = att.Filename;
        }
    }
    

    【讨论】:

      【解决方案2】:
          using (var imap = new AE.Net.Mail.ImapClient("imap.gmail.com", mailAccount.UserName, mailAccount.Password, AE.Net.Mail.ImapClient.AuthMethods.Login, 993, true))  {
              var msgs = imap.SearchMessages(SearchCondition.Unseen());
              for (int i = 0; i < msgs.Length; i++) {
                  MailMessage msg = msgs[i].Value;
      
                  foreach (var att in msg.Attachments) {
                      string fName;
                      fName = att.Filename;
                  }
              }
          }
      

      【讨论】:

        【解决方案3】:

        图标。由于作者不提供任何预建下载,您必须自己编译。 (我相信你可以通过 NuGet 获得它)。 bin/ 文件夹中不再有 .dll。没有文档,我认为这是一个缺点,但我能够通过查看源代码(开源!)并使用 Intellisense 来解决这个问题。以下代码专门连接到 Gmail 的 IMAP 服务器:

        // Connect to the IMAP server. The 'true' parameter specifies to use SSL 
        // which is important (for Gmail at least) 
        ImapClient ic = new ImapClient("imap.gmail.com", "name@gmail.com", "pass", ImapClient.AuthMethods.Login, 993, true); 
        // Select a mailbox. Case-insensitive ic.SelectMailbox("INBOX"); Console.WriteLine(ic.GetMessageCount()); 
        // Get the first *11* messages. 0 is the first message; 
        // and it also includes the 10th message, which is really the eleventh ;) 
        // MailMessage represents, well, a message in your mailbox 
        MailMessage[] mm = ic.GetMessages(0, 10); 
        foreach (MailMessage m in mm) {     
           Console.WriteLine(m.Subject); 
        } // Probably wiser to use a using statement ic.Dispose();
        

        【讨论】:

          猜你喜欢
          • 2018-03-11
          • 1970-01-01
          • 1970-01-01
          • 2014-11-08
          • 1970-01-01
          • 2011-08-27
          • 2020-05-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多