Code Đoc và tải file đính kèm từ Email
Chào các bạn, hôm nay mình sẽ giới thiệu các bạn code lấy mail tự động, từ Imap
Đầu tiên tới địa chỉ  http://www.limilabs.com/mail để tải thư viện xử lý Mail
| 
Code
  đơn giản để nhận thư 
using(Pop3 pop3 = new Pop3()) | 
| 
{ | 
| 
    pop3.Connect("pop3.server.com");  //
  or ConnectSSL for SSL       | 
| 
    pop3.UseBestLogin("user", "password"); | 
|  | 
| 
    List<string>
  uids = pop3.GetAll(); | 
| 
// Lấy tất cả các
  thư | 
| 
    foreach (string uid in uids) | 
| 
    { | 
| 
        IMail
  email = new MailBuilder() | 
| 
            .CreateFromEml(pop3.GetMessageByUID(uid)); | 
| 
        Console.WriteLine(email.Subject); | 
| 
    } | 
| 
    pop3.Close(); | 
| 
} 
Nâng cao 
Bộ lọc: Nhận các thư trong khoảng thời gian và tiêu đề
  nhập vào  
1)  Tạo mail 
Email email = new Email (mailServer, email,
  passwordEmail, subjectEmail, attachedFileTypes, onDate);  
2)  Kết nối và lấy nội dung thư theo
  ID với email được truyền từ bước 1. Hãy đảm bảo rằng Mail của bạn đã được cấu
  hình Bật IMap nhé 
public override List<SaveFile>
  DownLoadFileAttachment(Email email) 
        { 
            List<SaveFile> listSavedFiles = new List<SaveFile>(); 
            using (Imap imap = new Imap()) 
            { 
                try 
                { 
                   
  imap.ConnectSSL(email.MailServer); 
                    imap.Login(email.UserName,
  email.Password); 
                    if (imap.Connected) 
                    { 
                        DateTime fromDate = DateTime.Today; 
                        DateTime toDate = DateTime.Today; 
                        if (!string.IsNullOrEmpty(email.FromDate)) 
                        { 
                            fromDate = DateTime.Parse(email.FromDate); 
                        } 
                        if (!string.IsNullOrEmpty(email.ToDate)) 
                        { 
                            toDate = DateTime.Parse(email.ToDate); 
                        } 
                        if (!string.IsNullOrEmpty(email.OnDate)) 
                        { 
                            fromDate = DateTime.Parse(email.OnDate).Date; 
                            toDate = DateTime.Parse(email.OnDate).Date.AddDays(1); 
                        } 
                        imap.SelectInbox(); 
                        List<long> listUIDS =
  GetEmailUIDS(imap, email); 
// Lấy tất cả các mail theo
  điều kiện thỏa mãn của mail ở bước 1 
                        foreach (long uid in listUIDS) 
                        { 
                            string eml =
  imap.GetMessageByUID(uid); 
                            IMail mail = new MailBuilder().CreateFromEml(eml); 
                            if (mail != null &&
  mail.Date.HasValue) 
                            { 
                                if (mail.Date.Value.Date
  >= fromDate && mail.Date.Value.Date <= toDate) 
                                { 
                                   
  listSavedFiles.AddRange(SaveFile(mail, email)); 
                                } 
                            } 
                        } 
                    } 
                } 
                catch (Exception ex) 
                { 
                    Logger.WriteErrorLog(LogPath, ex,
  null); 
                } 
                finally 
                { 
                    imap.Close(); 
                } 
            } 
            return listSavedFiles; 
        } | 
|  | 
