1.引用LumiSoft.Net.dll(可到官網下載最新版)
2.使用pop3接收郵件
添加引用
using LumiSoft.Net.POP3.Client; using LumiSoft.Net.Mime;
if (String.Compare(sSERVICE, "pop3", true) == 0) { using (POP3_Client pop3 = new POP3_Client()) { //與Pop3服務器建立連接 pop3.Connect(sSERVER_URL, nPORT, false); //驗證身份 pop3.Authenticate(sEMAIL_USER, sEMAIL_PASSWORD, false); //獲取郵件信息列表 POP3_ClientMessageCollection infos = pop3.Messages; foreach (POP3_ClientMessage info in infos) { byte[] bytes = info.MessageToByte(); Mime mime = Mime.Parse(bytes); //存庫 SaveEmail(mime.MainEntity.Subject, mime.MainEntity.Date, mime.BodyText, mime.BodyHtml, mime.MainEntity.From.ToAddressListString(), mime.MainEntity.To.ToAddressListString(), mime.MainEntity.Cc == null ? string.Empty : mime.MainEntity.Cc.ToAddressListString(), mime.MainEntity.Bcc == null ? string.Empty : mime.MainEntity.Bcc.ToAddressListString(), Sql.ToInteger(info.UID)); } } }
2.使用imap接收郵件
添加引用
using LumiSoft.Net.IMAP.Client; using LumiSoft.Net.IMAP; using LumiSoft.Net.Mail;
if (String.Compare(sSERVICE, "imap", true) == 0) { IMAP_Client client = new IMAP_Client(); try { //與imap服務器建立連接 client.Connect(sSERVER_URL, nPORT, false); //驗證身份 client.Login(sEMAIL_USER, sEMAIL_PASSWORD); client.SelectFolder("INBOX"); IMAP_Client_FetchHandler fetchHandler = new IMAP_Client_FetchHandler(); //獲取未讀郵件的UID int[] unseen_ids = client.Search(false, "UTF-8", "unseen"); IMAP_t_SeqSet sequence = IMAP_t_SeqSet.Parse(string.Join(",", unseen_ids)); var items = new IMAP_t_Fetch_i[] { new IMAP_t_Fetch_i_Envelope(), new IMAP_t_Fetch_i_Uid(), new IMAP_t_Fetch_i_Flags(), new IMAP_t_Fetch_i_InternalDate(), new IMAP_t_Fetch_i_Rfc822() }; //遍歷未讀郵件 client.Fetch(false, sequence, items, (s, e) => { try { var email = e.Value as IMAP_r_u_Fetch; if (email.Rfc822 != null) { email.Rfc822.Stream.Position = 0; var mine = Mail_Message.ParseFromStream(email.Rfc822.Stream); email.Rfc822.Stream.Close(); SaveEmail(email.Envelope.Subject, email.InternalDate.Date, mine.BodyText, mine.BodyHtmlText, mine.From.ToString(), mine.To.ToString(), mine.Cc == null ? string.Empty : mine.Cc.ToString(), mine.Bcc == null ? string.Empty : mine.Bcc.ToString(), Sql.ToInteger(email.UID.UID)); } } catch (Exception ex) { lblError.Text = "Handle-Err:" + ex.Message; } }); } catch (Exception ex) { lblError.Text = ex.Message; } }
注:
網易163免費郵箱相關服務器信息: