A detailed explanation of the use of Lumisoft. NET component POP3 for message receiving and deletion operations

  • 2020-06-07 04:23:48
  • OfStack

Lumisoft. NET component is a very powerful open source component for mail sending, receiving, and so on. I have written some essays on this component before, but most of them are used to send mails. Recently, due to the needs of the project, this component is needed to receive mails, which are received locally through POP3 protocol, so I have a comprehensive understanding and use of this component. It is in this context that this article describes the use of the POP3 protocol processing class for this component. Lumisoft. NET component in 2013, the author made a definite update and fixed some problems. This article is based on the latest version of this component.

1. POP3 Login and header information acquisition

To start with POP3, you must create an object of POP3_Client, then connect and login through Connect and Login, as shown below.


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);

The mail download of POP3 is carried out through the property Messages object of POP3_Client object. Each POP3_ClientMessage represents 1 complete mail message. The first one should only get 1 simple mail message (including the only identification UID of the mail), so as to improve the processing speed of POP3 protocol, as shown in the following code.

foreach (POP3_ClientMessage message in popClient.Messages)

To take the next step to get the message header information, do the following transformation

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

After conversion, Mail_Message carries many of the necessary information of the message header file, such as sender, sender name, receiving address, CC address, message subject, message date, and so on.

The information of these email addresses is recorded by the Mail_t_Mailbox object. 1 generally contains the Address and display name of the email address, which is very convenient for display. For example, we can escape and record to the database.


if (mime_header.From != null)
                        {
                            //(wuhuacong@163.com)
                            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(';');
                        }

Each Email will have 1 Id, the only one within the scope of Pop3 server. Checking the existence of this Id will tell you whether this email has been received before

info.MailUid = message.UID;

The header information for each message contains a date, which can be obtained below

info.Date = mime_header.Date;

The header information can be obtained through the following code

info.Title = mime_header.Subject;/

2. Acquisition of message body information and attachment information

If you need to take a step further to get the body content of the message, you need to take a step further to transform the message by manipulating the message object MessageToByte and then converting it using the function ES65en_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
{
     // Mask coding error problem, error in BodyText There is, BodyHtmlText Does not exist when accessing BodyHtmlText There will be a 
}

The attachment of the mail is carried by MIME_Entity, so we need to get the object through ES73en_ES74en. GetAttachments(true, true) and convert it into attachment information.

#region  Attachment contents of mail 
                        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;

If you need to take one step to get the file byte stream in the attachment, you need to take another step to convert to the MIME_b_SinglepartBase object.

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

If you want to distinguish whether the attachment in an email is an embedded image attachment or a real attachment, you can tell by the following code: if it is MIME_DispositionTypes.Attachment is a normal attachment, and ES90en_DispositionTypes.Inline is an embedded body attachment.

entity.ContentDisposition.DispositionType == MIME_DispositionTypes.Attachment

3. Delete the mail

The mail on the server can be deleted through the protocol of POP3. The deletion operation is very simple, and it is mainly identified through ES99en.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);
                        }
                    }
                }
            }


Related articles: