使用AE.NET从gmail获取附件无法正常工作

时间:2012-12-09 05:36:47

标签: c# gmail-imap

我正在使用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中看到附件。

3 个答案:

答案 0 :(得分:2)

您正在执行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;
    }
}

答案 1 :(得分:0)

    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;
            }
        }
    }

答案 2 :(得分:0)

图标。由于作者不提供任何预建下载,因此您必须自己编译。 (我相信您可以通过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();
相关问题