阅读电子邮件从Gmail在C# [英] Reading emails from Gmail in C#
本文介绍了阅读电子邮件从Gmail在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想读从Gmail电子邮件。我想尽API /开源项目,我能找到,并不能得到任何人的工作。
I am trying to read emails from Gmail. I have tried every API / open source project I can find, and can not get any of them working.
有没有人有工作code的样本,让我进行身份验证,并从Gmail帐户下载电子邮件?
Does anyone have a sample of working code that will allow me to authenticate and download emails from a Gmail account?
最后工作版本贴出如下: http://stackoverflow.com/a/19570553/550198
Final working version posted below: http://stackoverflow.com/a/19570553/550198
推荐答案
从使用库: HTTP://mailsystem.$c $ cplex.com /
下面是我的完整code样品:</ P>
电子邮件信息库
Here is my complete code sample:
using System.Collections.Generic;
using System.Linq;
using ActiveUp.Net.Mail;
namespace GmailReadImapEmail
{
public class MailRepository
{
private Imap4Client client;
public MailRepository(string mailServer, int port, bool ssl, string login, string password)
{
if (ssl)
Client.ConnectSsl(mailServer, port);
else
Client.Connect(mailServer, port);
Client.Login(login, password);
}
public IEnumerable<Message> GetAllMails(string mailBox)
{
return GetMails(mailBox, "ALL").Cast<Message>();
}
public IEnumerable<Message> GetUnreadMails(string mailBox)
{
return GetMails(mailBox, "UNSEEN").Cast<Message>();
}
protected Imap4Client Client
{
get { return client ?? (client = new Imap4Client()); }
}
private MessageCollection GetMails(string mailBox, string searchPhrase)
{
Mailbox mails = Client.SelectMailbox(mailBox);
MessageCollection messages = mails.SearchParse(searchPhrase);
return messages;
}
}
}
用法
[TestMethod]
public void ReadImap()
{
var mailRepository = new MailRepository(
"imap.gmail.com",
993,
true,
"yourEmailAddress@gmail.com",
"yourPassword"
);
var emailList = mailRepository.GetAllMails("inbox");
foreach (Message email in emailList)
{
Console.WriteLine("<p>{0}: {1}</p><p>{2}</p>", email.From, email.Subject, email.BodyHtml.Text);
if (email.Attachments.Count > 0)
{
foreach (MimePart attachment in email.Attachments)
{
Console.WriteLine("<p>Attachment: {0} {1}</p>", attachment.ContentName, attachment.ContentType.MimeType);
}
}
}
}
这篇关于阅读电子邮件从Gmail在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文