ImapClient imap = new ImapClient(ImapServer, 993, true);
imap.Login(ImapUserame, ImapPwd, AuthMethod.Login);
IEnumerable<uint> uids = imap.Search(SearchCondition.Unseen());//获取未读邮件
foreach (uint uid in uids)
{
MailMessage item = imap.GetMessage(uid);
foreach (Attachment attachment in item.Attachments)
{
byte[] allbytes = new byte[attachment.ContentStream.Length];
int bytesread = attachment.ContentStream.Read(allbytes, 0, (int)attachment.ContentStream.Length);
string addrmail = addr + @"err\" + attachment.Name;
BinaryWriter writer = new BinaryWriter(new FileStream(addrmail, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
writer.Write(allbytes);
writer.Close();
}
imap.SetMessageFlags(uid, null, MessageFlag.Seen);//标记为已读
}
imap.Dispose();
阿里云邮箱测试成功
引用了S22.Imap 3.6.0
下载地址:https://www.nuget.org/packages/S22.Imap/3.6.0