Reading mails using IMAP and MailSystem.NET



http://briancaos.wordpress.com/2012/04/24/reading-mails-using-imap-and-mailsystem-net/


how you can use C# and MailSystem.NET to read mails from any IMAP source, including GMail?


First you need to copy ActiveUp.Net.Common.dll and ActiveUp.Net.Imap4.dll from the MailSystem.NET package to your /bin/ folder.

Reading from MailSystem.NET is pretty straight forward:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ActiveUp.Net.Mail;
 
namespace PT.MailIntegration.IMAP
{
  public class MailRepository
  {
    private Imap4Client _client = null;
 
    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
      {
        if (_client == null)
          _client = new Imap4Client();
        return _client;
      }
    }
 
    private MessageCollection GetMails(string mailBox, string searchPhrase)
    {
      Mailbox mails = Client.SelectMailbox(mailBox);
      MessageCollection messages = mails.SearchParse(searchPhrase);
      return messages;
    }
  }
}

This repository can now be used to read all mails or all unread mails from a mailbox. The constructor takes the mail server name, port and SSL information, as well as the email address and password of the account to read from. GetAllMails() read all mails from the specified mailbox, while GetUnreadMails() reads all unread mails. Both functions returns a list of Message objects. A Message is the complete email in one object.

To read from a GMail mailbox you need to connect using SSL on port 993:

using ActiveUp.Net.Mail;
using PT.MailIntegration.IMAP;
 
protected void TestMail()
{
  MailRepository rep = new MailRepository("imap.gmail.com", 993, true, @"mailaddress@gmail.com", "password");
  foreach (Message email in rep.GetUnreadMails("Inbox"))
  {
    Response.Write(string.Format("<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)
      {
        Response.Write(string.Format("<p>Attachment: {0} {1}</p>", attachment.ContentName, attachment.ContentType.MimeType));
      }
    }
  }
}

Other IMAP mail servers might not need SSL, or requires another port number.

When calling GetUnreadMails(), mails will be marked as read afterwards.

Please note that reading using IMAP (at lest from GMail accounts) is very slow. Don’t be surprised if it takes up to a minute to get 30 mails from a mailbox.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This book has existed, in one form or another, since the first release of C# and the .NET Platform was published in step with Microsoft’s release of .NET 1.0 Beta 2 (circa the summer of 2001). Since that point, I have been extremely happy to see that this text continues to be very well received by the press. Over the years, it was nominated as a 2002 Jolt Award finalist (I lost, but hey, life goes on); it was also awarded the 2003 Referenceware Excellence Award for programming book of the year. More importantly, I have been very grateful to receive e-mails from readers all around the world. It is very cool to chat with so many people who have told me this book has helped their careers in some small way. On a related note, I have to say that this book is better than it has ever been, thanks to the readers who have send me various suggestions for improvements, pointed out typos in the text, and alerted me to other glitches on my part. I have also been taken aback to learn that this very book has been, and is being used, in college and university settings, and is required reading for numerous undergraduate and graduate courses in the field of computer science. To the press, readers, and professors, I have nothing to say but thank you and happy programming! In any case, since the initial release of this book, I have worked hard to keep the material current with each release of the .NET platform. The fifth edition of this text, which you hold in your hands, has been fully updated to provide coverage on the new features of the C# 2010 programming language, as well as the new features of .NET 4.0 Platform. Here you will find information on the new Dynamic Language Runtime (DLR), the Task Parallel Library (TPL), Parallel LINQ (PLINQ), the ADO.NET Entity Framework (EF), and several “minor” (but very useful) updates, such as C# 2010 named arguments, C# 2010 optional parameters, the Lazy<T> class type, and so on. In addition to covering all the new bits, this book continues to provide you with a rock-solid foundation of the C# language as a whole, the pillars of object oriented programming (OOP), assembly configuration, database access (through ADO.NET), as well as the process of building desktop GUI applications, web applications, and distributed systems (among other topics). As with the earlier editions, this edition presents the C# programming language and .NET base class libraries using a friendly and approachable tone (or so I have been told!). As well, this new edition remains focused on providing you with the information you need to build software solutions today, rather than spending too much time examining esoteric details that few individuals will ever actually care about.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值