在C#中从Gmail读取电子邮件

前端之家收集整理的这篇文章主要介绍了在C#中从Gmail读取电子邮件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试从Gmail读取电子邮件.我已经尝试过我可以找到的每个API /开源项目,不能让任何一个工作.

有没有人有一个工作代码的样本,可以让我从Gmail帐户验证和下载电子邮件

最终工作版本如下:https://stackoverflow.com/a/19570553/550198

解决方法

使用库: http://mailsystem.codeplex.com/

以下是我的完整代码示例:

电子邮件存储库

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

另一个例子,这次使用MailKit

public class MailRepository : IMailRepository
{
    private readonly string mailServer,login,password;
    private readonly int port;
    private readonly bool ssl;

    public MailRepository(string mailServer,string password)
    {
        this.mailServer = mailServer;
        this.port = port;
        this.ssl = ssl;
        this.login = login;
        this.password = password;
    }

    public IEnumerable<string> GetUnreadMails()
    {
        var messages = new List<string>();

        using (var client = new ImapClient())
        {
            client.Connect(mailServer,port,ssl);

            // Note: since we don't have an OAuth2 token,disable
            // the XOAUTH2 authentication mechanism.
            client.AuthenticationMechanisms.Remove("XOAUTH2");

            client.Authenticate(login,password);

            // The InBox folder is always available on all IMAP servers...
            var inBox = client.InBox;
            inBox.Open(FolderAccess.ReadOnly);
            var results = inBox.Search(SearchOptions.All,SearchQuery.Not(SearchQuery.Seen));
            foreach (var uniqueId in results.UniqueIds)
            {
                var message = inBox.GetMessage(uniqueId);

                messages.Add(message.HtmlBody);

                //Mark message as read
                //inBox.AddFlags(uniqueId,MessageFlags.Seen,true);
            }

            client.Disconnect(true);
        }

        return messages;
    }

    public IEnumerable<string> GetAllMails()
    {
        var messages = new List<string>();

        using (var client = new ImapClient())
        {
            client.Connect(mailServer,SearchQuery.NotSeen);
            foreach (var uniqueId in results.UniqueIds)
            {
                var message = inBox.GetMessage(uniqueId);

                messages.Add(message.HtmlBody);

                //Mark message as read
                //inBox.AddFlags(uniqueId,true);
            }

            client.Disconnect(true);
        }

        return messages;
    }
}

用法

[Test]
public void GetAllEmails()
{
    var mailRepository = new MailRepository("imap.gmail.com","YOUREMAILHERE@gmail.com","YOURPASSWORDHERE");
    var allEmails = mailRepository.GetAllMails();

    foreach(var email in allEmails)
    {
        Console.WriteLine(email);
    }

    Assert.IsTrue(allEmails.ToList().Any());
}

猜你在找的C#相关文章