C# C上的IMAP-下载邮件和附件
我曾在C上使用开源项目sourceforge上名为Koolwired.Imap的项目尝试过这一点 下载邮件时还可以,但对于附件,它只列出了附件的文件名。有人试过这个吗 如果没有其他更好的免费图书馆可以做到这一点,我需要一个免费/开源的解决方案,因为我这样做是为了我的校园项目C# C上的IMAP-下载邮件和附件,c#,imap,attachment,C#,Imap,Attachment,我曾在C上使用开源项目sourceforge上名为Koolwired.Imap的项目尝试过这一点 下载邮件时还可以,但对于附件,它只列出了附件的文件名。有人试过这个吗 如果没有其他更好的免费图书馆可以做到这一点,我需要一个免费/开源的解决方案,因为我这样做是为了我的校园项目 ImapConnect connection = new ImapConnect("imap.gmail.com", 993, true); ImapCommand command = new ImapCommand(con
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();
我的选择是codeplex上的一个项目。它完美地处理了附件。如果您想短期使用它,请使用chilkat IMAP API。您可以将整个电子邮件保存为一个eml文件,这样就有足够的样本让任何人运行。 它可以免费使用一个月,然后再付费 同时,您要使用coolwired单独下载附件,请使用以下命令
ImapMailboxMessage mbStructure = new ImapMailboxMessage();
mbStructure = command.FetchBodyStructure(a);
for (int j = 0; j < a.BodyParts.Count; j++)
{
//create dir if doesnot exist
if (!Directory.Exists(path))
{
DirectoryInfo di = Directory.CreateDirectory(path);
}
if (mbStructure.BodyParts[j].Attachment)
{
//Attachment
command.FetchBodyPart(mbStructure, mbStructure.BodyParts.IndexOf(mbStructure.BodyParts[j]));
//Write Binary File
FileStream fs = new FileStream(path + mbStructure.BodyParts[j].FileName, FileMode.Create);
fs.Write(mbStructure.BodyParts[j].DataBinary, 0, (int)mbStructure.BodyParts[j].DataBinary.Length);
fs.Flush();
fs.Close();
}
}
我用它从eml文件中读取附件。 你在哪里写作
ImapMailboxMessage msg = mailbox.Messages[mailCount - 1];
您可以使用ImapMailboxMessage msg=mailbox.Messages[i]
以便在选定文件夹中有多封电子邮件时更好地工作
[mailCount-1]永远不会读取最后一封邮件。并且不要与他人共享您的登录详细信息:这是一个测试帐户。。没问题。。感谢您的提醒..我如何运行此代码?只从gmail帐户下载邮件有效吗?下载后是否可以转换下载的文件?