我曾尝试过使用名为“Koolwired.Imap”的开源项目在C#上执行此操作。
在下载邮件时,效果还不错,但对于附件,它仅列出了附件的文件名。是否有人尝试过这个?
如果没有,是否有其他更好的免费库可以做到同样的事情(我需要一个免费/开源的解决方案,因为我正在为校园项目做这个)
ImapConnect connection = new ImapConnect("imap.gmail.com", 993, true);
ImapCommand command = new ImapCommand(connection);
ImapAuthenticate auth = new ImapAuthenticate(connection, "<username>@gmail.com", "<password>");
connection.Open();
auth.Login();
string htmlbody = "";
ImapMailbox mailbox = command.Select("INBOX");
mailbox = command.Fetch(mailbox);
int mailCount = mailbox.Messages.Count;
for (int i = 0; i < mailCount ; i++)
{
ImapMailboxMessage msg = mailbox.Messages[mailCount - 1];
msg = command.FetchBodyStructure(msg);
msg.BodyParts[0].ContentEncoding = BodyPartEncoding.NONE;
msg = command.FetchBodyPart(msg, msg.HTML);
foreach (ImapMessageBodyPart a in msg.BodyParts)
{
if (a.Data != null)
{
string fileName = "";
if (a.Attachment) fileName = ParseFileName(a.Disposition);
string mimeType = a.ContentType.MediaType.ToLower();
a.ContentEncoding = BodyPartEncoding.UTF7;
htmlbody = a.Data;
}
}
}
auth.Logout();
connection.Close();