The Lumisoft. NET component is an open-source component that provides powerful functions such as sending and receiving emails. It is generally used to process mail-related operations and is very suitable. I have written some essays on this component before, but most of them are sent via email. Recently, due to project requirements, this component is needed to receive emails, the mail is received locally through the POP3 protocol, so this component is fully understood and used. This article mainly introduces the use of the POP3 protocol processing class of this component. The Lumisoft. NET component was updated in 2013 and some problems were fixed. This article was developed and used based on the latest version of the component.
1. POP3 logon and Header Information Retrieval
To use POP3, you must create a POP3_Client object and Connect to and log on to Login through Connect. The related code is as follows.
Copy codeThe Code is as follows:
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 mail downloads are carried out through the Messages object of the POP3_Client object. Each POP3_ClientMessage indicates a complete mail message, in the beginning, it should only obtain some simple mail information (including the unique identity UID of the mail), in order to improve the processing speed of POP3 protocol, as shown in the following code.
Copy codeThe Code is as follows:
Foreach (POP3_ClientMessage message in popClient. Messages)
To further obtain the mail header information, you need to perform the following conversion:
Copy codeThe Code is as follows:
Mail_Message mime_header = Mail_Message.ParseFromByte (message. HeaderToByte ());
After the conversion, Mail_Message carries many necessary information about the mail header file, such as the sender, sender name, recipient address, CC address, email title, and email date.
The mail Address information is recorded through the Mail_t_Mailbox object. Generally, it contains the Address of the mail Address and the display name DisplayName, which is very convenient for display. For example, we can escape it, record to the database.
Copy codeThe Code is as follows:
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 );
}
}
Copy codeThe Code is as follows:
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 has a unique Id within the scope of the Pop3 server. You can check whether this Id exists to see if you have received this Email before.
Copy codeThe Code is as follows:
Info. MailUid = message. UID;
The header information of each mail contains a date, which can be obtained as follows:
Copy codeThe Code is as follows:
Info. Date = mime_header.Date;
The title information can be obtained through the following code:
Copy codeThe Code is as follows:
Info. Title = mime_header.Subject ;/
2. Obtain the body information and attachment information of the email
If you need to further obtain the body of the email, you need to further convert the information, perform the MessageToByte operation on the message object, and then use the Mail_Message.ParseFromByte function to convert the message.
Copy codeThe Code is as follows:
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
{
// Block encoding errors. If BodyText does not exist and BodyHtmlText does not exist, access BodyHtmlText
}
The attachment of an email carries information through MIME_Entity. Therefore, we need to obtain the object through mime_message.GetAttachments (true, true) and convert it to the attachment information.
Copy codeThe Code is as follows:
# Region email attachment content
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 further obtain the file byte stream in the attachment, You need to further convert it to the MIME_ B _SinglepartBase object.
Copy codeThe Code is as follows:
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 identify whether the attachments in the email are embedded image attachments or real attachments, you can use the following code to determine whether the attachments in the email are normal attachments in MIME_DispositionTypes.Inline and the attachments in the embedded text.
Copy codeThe Code is as follows:
Entity. ContentDisposition. DispositionType = MIME_DispositionTypes.Attachment
3. delete emails
The mail on the server can be deleted through the POP3 protocol. The delete operation is very simple. It is mainly identified by mail. MarkForDeletion. The instance operation code is as follows:
Copy codeThe Code is as follows:
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 );
}
}
}
}