我正在使用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中看到附件。
你只是在做imap.SearchMessages(SearchCondition.Unseen())
,但是这个方法只加载邮件的标题。您需要使用SearchMessages
方法中的消息ids使用下面的代码:
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;
}
}
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;
}
}
}
图标。由于作者未提供任何预先构建的下载,您必须自己编译。 (我相信你可以通过NuGet获得它)。 bin /文件夹中不再有.dll。没有文档,我认为这是一个缺点,但我能够通过查看源代码(yay for open source!)和使用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", "[email protected]", "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();