From:http://www.cnblogs.com/wuhuacong/archive/2013/05/06/3063093.html
The Lumisoft.net component is a very powerful open source component for sending and receiving mail, and it is very appropriate to use it to handle the related operations of the message. Before also wrote some of the components of the essay, but mainly use to send mail mostly, recently because of the project needs, need to use this component to receive mail, the message through the POP3 protocol to receive local, so the component is fully understood and used. This article is mainly on this background, introduces the use of POP3 protocol processing class of this component. Lumisoft.net Component 2013 The author has made some updates, fixed some issues, this article is based on the latest version of the component for development use.
1, POP3 Login and head information acquisition
First, using POP3, you must create a Pop3_client object, and then connect and log on with connect and login, and the associated 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 's mail download is done through the properties of the Pop3_client object messages object, each pop3_clientmessage represents a complete message, and at first it should just get some simple message information (which includes the unique UID of the message). This will improve the processing speed of the POP3 protocol, as shown in the following code.
foreach (Pop3_clientmessage message in popclient.messages)
In order to get the message header information further, the following conversion is required
Mail_message Mime_header = Mail_message.parsefrombyte (Message. Headertobyte ());
After conversion, Mail_message hosts a lot of necessary information such as sender, sender name, receiving address, CC person address, message title, mail date and so on.
The information of these e-mail addresses is recorded by the Mail_t_mailbox object, which generally contains the address and display name DisplayName of the e-mail addresses, which is very convenient to display, as we can escape and log into the database.
if (Mime_header. From! = null) { //Wu Huacong ([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 (';'); }
Each email will have a unique ID in the POP3 server scope, check if this ID exists and know if you have received this message before.
Info. MAILUID = message. UID;
The header information for each message will contain a date, which can be obtained from the following date
Info. Date = Mime_header. Date;
Header information can be obtained from the following code
Info. Title = Mime_header. subject;/
2. Access to message body information and attachment information
If you need to further get the body content of the message, you need to further convert the message, messagetobyte the Message object, and then use the function Mail_message.parsefrombyte to convert.
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 error problem, error occurs when BodyText exists and Bodyhtmltext does not exist, Access Bodyhtmltext will appear}
The attachment of the message is to carry the information through the mime_entity, so we need to put the object through Mime_message. Getattachments (True, true) to obtain and convert to attachment information.
#region Message 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, then further conversion to the Mime_b_singlepartbase object is required.
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 the attachment inside the message is an inline picture attachment or a real attachment, then you can be judged by the following code, if it is mime_dispositiontypes.attachment is ordinary attachment, mime_ Dispositiontypes.inline is an attachment to the embedded body.
Entity. Contentdisposition.dispositiontype = = Mime_dispositiontypes.attachment
3, delete the message operation
The message on the server can be deleted by POP3 protocol, the deletion is simple, mainly through mail. The markfordeletion is identified, and the instance operation code is shown below
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); } }}
POP3 Mail receive and delete operations based on the Lumisoft.net component