4

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

4

3 回答 3

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;
    }
}
于 2016-10-04T12:54:22.013 回答
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;
            }
        }
    }
于 2013-03-12T08:22:19.693 回答
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();
于 2019-04-10T01:35:31.333 回答