基於Lumisoft.NET組件的POP3郵件接收和刪除操作

來源:互聯網
上載者:User

標籤:應該   mis   擷取   pie   pat   記錄   error   parse   登入   

Lumisoft.NET組件是一個非常強大的郵件發送、郵件接收等功能的開源組件,一般用它來處理郵件的相關操作,是非常合適的。之前也寫過一些該組件的隨筆文章,不過主要是利用來發送郵件居多,最近由於項目需要,需要利用該組件來內送郵件,郵件通過POP3協議進行接收到本地,故對該組件進行了全面的瞭解和使用。本文主要是在此背景上,介紹該組件的POP3協議處理類的使用。Lumisoft.NET組件2013年作者有做了一定的更新,修複了一些問題,本文是基於該組件的最新版本進行開發使用。

1、POP3登入及頭部資訊擷取

首先使用POP3,必須建立一個POP3_Client的對象,然後通過Connect和Login進行串連和登入處理,相關的代碼如下所示。

            using (POP3_Client popClient = new POP3_Client())            {                popClient.Logger = new Logger();                popClient.Logger.WriteLog += new EventHandler<WriteLogEventArgs>(WriteLog);                popClient.Connect(pop3Server, pop3Port, pop3UseSsl);                popClient.Login(username, password);

POP3的的郵件下載通過POP3_Client 對象的屬性Messages對象進行,每個POP3_ClientMessage代表一份完整的郵件資訊,一開始應該是只是擷取一些簡單的郵件資訊(其中包括郵件的唯一標識UID),這樣才能提高POP3協議的處理速度,如下代碼所示。

foreach (POP3_ClientMessage message in popClient.Messages)

為了進一步擷取郵件標頭部資訊,那麼需要進行下面的轉換

Mail_Message mime_header = Mail_Message.ParseFromByte(message.HeaderToByte());

轉換後Mail_Message承載了郵件標頭部檔案的很多必備資訊,如發送人,發送人名稱,接收地址,抄送人地址,郵件標題,郵件日期等等資訊。

這些郵件地址的資訊,都是通過Mail_t_Mailbox對象來記錄,一般包含郵件地址的Address和顯示名稱DisplayName,這樣非常方便用來顯示,如我們可以進行轉義,記錄到資料庫裡面。

                        if (mime_header.From != null)                        {                            //伍華聰([email protected])                            string displayname = mime_header.From[0].DisplayName;                            string from = mime_header.From[0].Address;// DecodeString(mime_header.From[0].Address);                            if (!string.IsNullOrEmpty(displayname))                            {                                info.From = string.Format("{0}({1})", displayname, from);                            }                            else                            {                                info.From = string.Format("{0}", from);                            }                        }
                        if (mime_header.To != null)                        {                            StringBuilder sb = new StringBuilder();                            foreach (Mail_t_Mailbox recipient in mime_header.To.Mailboxes)                            {                                string displayname = recipient.DisplayName;                                string address = recipient.Address;                                if (!string.IsNullOrEmpty(displayname))                                {                                    sb.AppendFormat("{0}({1});", displayname, address);                                }                                else                                {                                    sb.AppendFormat("{0};", address);                                }                            }                            info.Senders = sb.ToString().Trim(‘;‘);                        }                        if (mime_header.Cc != null)                        {                            StringBuilder sb = new StringBuilder();                            foreach (Mail_t_Mailbox recipient in mime_header.Cc.Mailboxes)                            {                                string displayname = recipient.DisplayName;                                string address = recipient.Address;                                if (!string.IsNullOrEmpty(displayname))                                {                                    sb.AppendFormat("{0}({1});", displayname, address);                                }                                else                                {                                    sb.AppendFormat("{0};", address);                                }                            }                            info.Carboncopy = sb.ToString().Trim(‘;‘);                        }

每封Email會有一個在Pop3伺服器範圍內唯一的Id,檢查這個Id是否存在就可以知道以前有沒有接收過這封郵件

info.MailUid = message.UID;

每份郵件的頭部資訊,都會包含一個日期的,如下可以擷取到該日期

info.Date = mime_header.Date;

標題資訊可以通過下面代碼擷取

info.Title = mime_header.Subject;/
2、郵件內文資訊和附件資訊的擷取

如果需要進一步擷取郵件的本文內容,則需要對資訊進行進一步的轉換,把message對象進行MessageToByte操作,然後利用函數Mail_Message.ParseFromByte進行轉換。

byte[] messageBytes = message.MessageToByte();Mail_Message mime_message = Mail_Message.ParseFromByte(messageBytes);if (mime_message == null) continue;
info.Body = mime_message.BodyText;try{ if (!string.IsNullOrEmpty(mime_message.BodyHtmlText)) { info.Body = mime_message.BodyHtmlText; } }catch{ //屏蔽編碼出現錯誤的問題,錯誤在BodyText存在而BodyHtmlText不存在的時候,訪問BodyHtmlText會出現}

郵件的附件是通過MIME_Entity來承載資訊的,所以我們需要把對象通過mime_message.GetAttachments(true, true)進行擷取,轉換為附件資訊。

                        #region 郵件附件內容                        foreach (MIME_Entity entity in mime_message.GetAttachments(true, true))                        {                            if (entity.ContentDisposition != null &&                                entity.ContentDisposition.Param_FileName != null)                            {                                //Console.WriteLine("Attachment: " + entity.ContentDisposition.Param_FileName);                                string fileName = entity.ContentDisposition.Param_FileName;

如果需要進一步擷取附件裡面的檔案位元組流,那麼還需要進行進一步的轉換為MIME_b_SinglepartBase對象。

 MIME_b_SinglepartBase byteObj = (MIME_b_SinglepartBase)entity.Body; if (byteObj != null) {         FileUtil.CreateFile(filePath, byteObj.Data);         fileSize = byteObj.Data.Length;

如果要區分郵件裡面的附件是內嵌圖片附件還是真正的附件,那麼可以通過下面代碼進行判斷,如果是MIME_DispositionTypes.Attachment的就是普通附件,MIME_DispositionTypes.Inline的就是內嵌本文的附件。

entity.ContentDisposition.DispositionType == MIME_DispositionTypes.Attachment
3、郵件的刪除操作 

伺服器上的郵件,可以通過POP3的協議方式進行刪除,刪除操作很簡單,主要是通過mail.MarkForDeletion進行標識即可,執行個體作業碼如下所示

            using (POP3_Client c = new POP3_Client())            {                c.Connect(pop3Server, pop3Port, pop3UseSsl);                c.Login(username, password);                if (c.Messages.Count > 0)                {                    foreach (POP3_ClientMessage mail in c.Messages)                    {                        try                        {                            if (toDeleteMailUidList.Contains(mail.UID))                            {                                mail.MarkForDeletion();                                deletedList.Add(mail.UID);                            }                        }                        catch (Exception ex)                        {                            LogTextHelper.Error(ex);                        }                    }                }            }

 

基於Lumisoft.NET組件的POP3郵件接收和刪除操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.