Reading mails using IMAP and MailSystem.NET
来源:互联网 发布:阿里钉钉软件 编辑:程序博客网 时间:2024/06/01 09:13
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 ofMessage 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.
- Reading mails using IMAP and MailSystem.NET
- Using .NET and Excel 2003 To Validate E-Mails
- Sending E-Mails using ASP.NET
- [ZT]Sending E-Mails using ASP.NET
- ASP.NET mailsystem 转
- [ZT]Sending Mass E-Mails using ASP.NET
- Create HTML mails in SSJS using MIME
- Reading and Writing using a Large Random Access File
- Android: Reading, using and working with XML data and web services in Android
- #Paper Reading# Topic Modeling using Topics from Many Domains, Lifelong Learning and Big Data
- Send Mails from within a .NET 2.0 Application (1)
- Send Mails from within a .NET 2.0 Application (2)
- Programmatically using ADO.NET and XML
- Retrieving Database Metadata using C# and .NET
- AJAX Using jQuery and ASP.NET
- net-snmp-Using and loading MIBS
- vmime.NET 邮件收发类库 支持Smtp, Pop3, Imap,TLS and SSL
- Reading Excel files using ODBC
- HTTP状态码(HTTP Status Code)
- Android----项目集合
- Android系统分区理解及分区目录细解
- 找工作, 谢谢
- C/C++开发中遇到的时间Time计算
- Reading mails using IMAP and MailSystem.NET
- CodeForces 126D
- [转]用反射+特性列出所有的枚举变量及其描述信息,绑定到DropDownList上。
- 从Nutch的输出日志分析其流程
- Objective C--原型模式
- python数据结构的-----列表
- 黑马程序员_JAVA多线程_1
- spring MVC的配置方法
- shu_1572 涂色游戏