4

我已经在 C# 上使用 sourceforge 上名为“Koolwired.Imap”的开源项目进行了尝试。

下载邮件时没问题,但对于附件,它只列出附件的文件名。有没有人试过这个?

如果没有,还有其他更好的免费图书馆可以做同样的事情(我需要一个免费/开源的解决方案,因为我正在为我的校园项目做这个)

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();
4

4 回答 4

1

我用它从 eml 文件中读取附件。 http://www.codeproject.com/KB/cs/mime_project.aspx?msg=3455831#xx3455831xx

于 2010-04-29T13:06:49.260 回答
1

我的选择是codeplex 上的interimap项目。它完美地处理附件。

于 2009-10-08T05:44:21.687 回答
0

你写的地方

ImapMailboxMessage msg = mailbox.Messages[mailCount - 1];

您可以使用ImapMailboxMessage msg = mailbox.Messages[i];

当您在所选文件夹中有多个电子邮件时,可以更好地工作。

[mailCount -1]永远不会阅读最后一条消息。

于 2016-11-07T13:37:50.893 回答
-1

如果您想在短时间内使用它,请使用 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();
 }
}                                  
于 2009-12-29T05:21:58.087 回答