IMAP在C# - 下载电子邮件和附件 [英] IMAP on C# - Download Mails and Attachments

查看:1110
本文介绍了IMAP在C# - 下载电子邮件和附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用所谓的Koolwired.Imap在SourceForge开源项目尝试这样的C#。



在下载邮件时,它是确定的,但对附件它只列出附件的文件名。是否有任何人谁试过这种?



如果不是有没有其他更好的免费库,可以做同样的(我需要这样的自由/开源解决方案,因为我这样做对我的校园项目)

  ImapConnect连接=新ImapConnect(imap.gmail.com,993,真); 
ImapCommand命令=新ImapCommand(连接);
ImapAuthenticate AUTH =新ImapAuthenticate(连接,<用户名> @ gmail.com,<密码和GT;);
connection.Open();
auth.Login();

串htmlbody =;
ImapMailbox邮箱= command.Select(INBOX);
邮箱= command.Fetch(邮箱);
INT mailCount = mailbox.Messages.Count;

的for(int i = 0; I< mailCount;我++)
{
ImapMailboxMessage味精= mailbox.Messages [mailCount - 1];
味精= command.FetchBodyStructure(MSG);

msg.BodyParts [0] = .ContentEncoding BodyPartEncoding.NONE;
味精= command.FetchBodyPart(味精,msg.HTML);

的foreach(在msg.BodyParts ImapMessageBodyPart一)
{
如果(A.数据!= NULL)
{
字符串文件名=;

如果(a.Attachment)文件名= ParseFileName(a.Disposition);
串mime类型= a.ContentType.MediaType.ToLower();

a.ContentEncoding = BodyPartEncoding.UTF7;
htmlbody = A.数据;
}
}
}

auth.Logout();
的Connection.close();


解决方案

ImapX是最好的图书馆。与Gmail完美的作品。 。死了简单易用



的http:// hellowebapps。 COM /产品/ imapx /


I have tried this on C# using open source project called "Koolwired.Imap" on sourceforge.

It was OK when downloading mails, but for attachments it is only listing the file name of the attachment. Is there anyone who has tried this?

If not is there any other better free library which can do the same (I need a free/open source solution for this, because I am doing this for my campus project)

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

解决方案

ImapX is the best library. Works perfectly with GMail. Dead simple to use.

http://hellowebapps.com/products/imapx/

这篇关于IMAP在C# - 下载电子邮件和附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆