4

sourceforgeの「Koolwired.Imap」というオープンソースプロジェクトを使用して、C#でこれを試しました。

メールのダウンロード時はOKだったのですが、添付ファイルの場合は添付ファイルのファイル名しか記載されていません。これを試した人はいますか?

そうでない場合、同じことができる他のより良い無料のライブラリはありません(キャンパスプロジェクトでこれを行っているため、これには無料/オープンソースのソリューションが必要です)

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

私の選択は、 codeplex のinterimapプロジェクトです。添付ファイルを完全に処理します。

于 2009-10-08T05:44:21.687 に答える
1

これを使用して、eml ファイルから添付ファイルを読み取りました。 http://www.codeproject.com/KB/cs/mime_project.aspx?msg=3455831#xx3455831xx

于 2010-04-29T13:06:49.260 に答える
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 ファイルとして保存し、誰でも実行できる十分なサンプルを保存できます。1か月間無料で完全に機能し、その後は有料です

同時に、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 に答える