IMAP收邮件

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using LumiSoft.Net.IMAP;
 10 using LumiSoft.Net.IMAP.Client;
 11 using System.IO;
 12 using LumiSoft.Net.Mail;
 13 using System.Net.Mime;
 14 using System.Net.Mail;
 15 using LumiSoft.Net.MIME;
 16 namespace EMailTest
 17 {
 18     public partial class Form1 : Form
 19     {
 20         public Form1()
 21         {
 22             InitializeComponent();
 23         }
 24 
 25         private void Handle(object sender, LumiSoft.Net.EventArgs<IMAP_r_u> e)
 26         {
 27             Console.WriteLine(e.Value.ToString());
 28         }
 29         private void button1_Click(object sender, EventArgs oe)
 30         {
 31             IMAP_Client client = new IMAP_Client();
 32 
 33             try
 34             {
 35                 //连接邮件服务器通过传入邮件服务器地址和用于IMAP协议的端口号
 36                 //SSL 993 Other 143
 37                 //client.Connect("imap.qq.com", 993, true);
 38                 //client.Login("4587405@qq.com", "*******");
 39 
 40                 client.Connect("imap.163.com", 143, false);
 41                 client.Login("ylx-1982@163.com", "*****");
 42                 client.GetFolders(null).ToList().ForEach(f => {
 43 
 44                     Console.WriteLine(f.FolderName);
 45                     var s = client.FolderStatus(f.FolderName);
 46                     s.ToList().ForEach(sIt => {
 47                         Console.WriteLine("总数:{0},未读:{1},最近{2}", sIt.MessagesCount, sIt.MessagesCount, sIt.UnseenCount);
 48                     });
 49 
 50                 });
 51                 
 52 
 53             
 54                 client.SelectFolder("INBOX");
 55                
 56                 var seqSet = IMAP_t_SeqSet.Parse("1000:*");
 57                 var items = new IMAP_t_Fetch_i[]
 58                 {
 59                     new IMAP_t_Fetch_i_Envelope(),
 60                     new IMAP_t_Fetch_i_Uid(),
 61                     new IMAP_t_Fetch_i_Flags(),
 62                     new IMAP_t_Fetch_i_InternalDate(),
 63                     new IMAP_t_Fetch_i_Rfc822()
 64                 };
 65 
 66 
 67                 //Fetch 第一个参数false时seqSet有效
 68                 client.Fetch(false, seqSet, items, (s, e) =>
 69                 {
 70                     try
 71                     {
 72                         
 73                         var email = e.Value as IMAP_r_u_Fetch;
 74                         //using (var ctx = new DBTEntities())
 75                         //{
 76                         //    var ent = new T_EMail();
 77                         //    ent.Flags = email.Flags.Flags.ToString();
 78                         //    ent.ReceiveDate = email.InternalDate.Date;
 79                         //    ent.Subject = email.Envelope.Subject;
 80                         //    ent.UId = email.UID.UID;
 81                         //    ctx.T_EMail.AddObject(ent);
 82                         //    ctx.SaveChanges();
 83 
 84                         //}
 85                         Console.WriteLine(" ");
 86                         Console.WriteLine("标题:" + email.UID.UID +"," + email.InternalDate.Date   +"," + email.Envelope.Subject );
 87                         Console.WriteLine("------------内容------------------------");
 88 
 89                         if (email.Rfc822 != null)
 90                         {
 91                             email.Rfc822.Stream.Position = 0;
 92                             var mine = Mail_Message.ParseFromStream(email.Rfc822.Stream);
 93                             email.Rfc822.Stream.Close();
 94 
 95                             //Console.WriteLine(mine.BodyHtmlText);
 96                             //Console.WriteLine(mine.Body.MediaType);
 97                             if (mine.Attachments.Count() > 0)
 98                             {
 99                                 
100                                 var list= mine.Attachments.ToList();
101                                 foreach (var att in list)
102                                 {
103                                     var part = att.Body as MIME_b_SinglepartBase;
104 
105                                     string filename=@"C:\xx\" + att.ContentType.Param_Name;
106                                     File.WriteAllBytes(filename, part.Data);
107                               
108                                   
109                                 }
110                                
111                             }
112                         }
113                     }
114                     catch (Exception ex)
115                     {
116                         Console.WriteLine("Handle-Err:" + ex.Message);
117                     }
118                     
119                 });
120 
121 
122 
123             }
124             catch (Exception ex)
125             {
126                 Console.WriteLine(ex.Message);
127             }
128             finally
129             {
130 
131             }
132 
133 
134 
135         }
136     }
137 }
 1 private void button2_Click(object sender, EventArgs e)
 2         {
 3             var list = new List<EmailAccount>();
 4             //list.Add(new EmailAccount() { Username = "doomguards@126.com", Password = "8888", SmtpHost = "smtp.126.com", SmtpPort = 25 });
 5             //list.Add(new EmailAccount() { Username = "ylx-1982@163.com", Password = "****", SmtpHost = "smtp.163.com", SmtpPort = 25 });
 6             list.Add(new EmailAccount() { Username = "wdfrog@hotmail.com", Password = "8888", SmtpHost = "smtp-mail.outlook.com", SmtpPort = 587,EnableSsl=true });//587,
 7             //list.Add(new EmailAccount() { Username = "4587405@qq.com", Password = "***", SmtpHost = "smtp.qq.com", SmtpPort = 25 });//
 8             foreach (var account in list)
 9             {
10                 TestMail(account);
11                 Console.WriteLine("完成:" + account);
12             }
13             Console.WriteLine("完成!");
14         }
15         private void TestMail(EmailAccount account)
16         {
17             string _to = "4587405@qq.com";
18             string _from = account.Username;
19             string _subject = "Using the new SMTP client.邮箱SMTP测试";
20             string _body = @"//设置邮箱端口,pop3端口:110, smtp端口是:25  Using this new feature, you can send an e-mail message from an application very easily.";
21             MailMessage message = new MailMessage();
22             message.From = new MailAddress(_from);
23             //可以利用MailMessage.To.Add方法增加要发送的邮件地址  
24             message.To.Add(new MailAddress("1228953303@qq.com"));
25             message.To.Add(new MailAddress(_to));
26             message.Subject = _subject;
27             message.Body = _body;
28 
29 
30             #region 添加附件
31             Attachment a = new Attachment(@"d:/x.jpg");
32             message.Attachments.Add(a);
33            // message.Attachments.Add(new Attachment(@"C:\Documents and Settings\Administrator\桌面\蓝牙串口—线缆噩梦终结者.docx"));
34             #endregion
35             //设置邮箱的地址或IP  
36             using (SmtpClient client = new SmtpClient(account.SmtpHost, account.SmtpPort))
37             {
38             
39                 //设置邮箱端口,pop3端口:110, smtp端口是:25   
40                 client.EnableSsl = account.EnableSsl;
41                 //设置超时时间  
42                 client.Timeout = 0;
43                 client.DeliveryMethod = SmtpDeliveryMethod.Network;
44 
45                 //要输入邮箱用户名与密码  
46                 // client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
47                 client.Credentials = new NetworkCredential(account.Username, account.Password);
48                 client.Send(message);
49                 client.Dispose();
50                 
51             }
52             
53         }
54 
55         void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
56         {
57             if (e.Error != null)
58             {
59                 Console.WriteLine(e.Error);
60             }
61         }
62         public class EmailAccount
63         {
64             public string Username { get; set; }
65             public string Password { get; set; }
66             public string SmtpHost { get; set; }
67             public int SmtpPort { get; set; }
68             public bool EnableSsl { get; set; }
69             public EmailAccount()
70             {
71                 EnableSsl = false;
72                 SmtpPort = 25;
73             }
74             public override string ToString()
75             {
76                 return Username;
77             }
78 
79         }

 

posted @ 2015-07-03 09:36  银河系上的地球  阅读(524)  评论(1编辑  收藏  举报