Deep Lumisoft.net Component POP3 Mail receive and delete operation of the use of detailed _ practical skills

Source: Internet
Author: User
Tags unique id

The Lumisoft.net component is a very powerful open source component of the function of sending and receiving mail, and it is very suitable to handle the related operation of mail. has also written some of the components of the essay article, but mainly used to send mail mostly, recently because of the project needs, the need to use this component to receive mail, mail through the POP3 protocol to receive local, so the component has been fully understood and used. This paper mainly describes the use of the POP3 protocol processing class for this component. Lumisoft.net Component 2013 The author has made some updates to fix some problems, this article is based on the latest version of the component for development and use.

1, POP3 Login and head information access

Using POP3 First, you must create a Pop3_client object and then connect and login through connect and login, as shown in the relevant code.

Copy Code code 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 performed by Pop3_client object's properties messages object, each pop3_clientmessage represents a complete message, and should initially be simply to get some simple message information (including the unique ID UID of the message), This will improve the processing speed of the POP3 protocol, as shown in the following code.
Copy Code code as follows:

foreach (Pop3_clientmessage message in popclient.messages)

To further get the message header information, you need to convert the following
Copy Code code as follows:

Mail_message Mime_header = mail_message.parsefrombyte (message. Headertobyte ());

After the conversion, Mail_message hosts a lot of essential information about the header file, such as sender, sender name, receiving address, CC address, message title, mail date, etc.

These email addresses are recorded through the Mail_t_mailbox object, typically containing the address and display name DisplayName of the mailing addresses, which are very handy to display, such as we can escape and log into the database.

Copy Code code 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 Code code 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 will have a unique ID on the POP3 server and check if the ID exists to see if the email has been received before
Copy Code code as follows:

Info. MAILUID = message. UID;

The header information for each message will contain a date that can be obtained as follows
Copy Code code as follows:

Info. Date = Mime_header. Date;

Header information can be obtained from the following code
Copy Code code as follows:

Info. Title = Mime_header. subject;/

2, the message body information and attachment information acquisition

If you need to get the body content of the message further, you need to further transform the information, messagetobyte the Message object, and then use the function Mail_message.parsefrombyte to convert it.
Copy Code code 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
{
The problem of shielding coding error, error in BodyText existence and Bodyhtmltext does not exist, Access Bodyhtmltext will appear
}


The attachment of the message is mime_entity to carry the information, so we need to pass the object through the Mime_message. Getattachments (True, true) is obtained and converted to attachment information.
Copy Code code as follows:

#region Message Attachment Contents
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;

Further conversion to the Mime_b_singlepartbase object is required if further access to the file byte stream in the attachment is required.
Copy Code code 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 distinguish the message inside the attachment is an inline picture attachment or a real attachment, then you can use the following code to judge, if it is mime_dispositiontypes.attachment is the general attachment, mime_ Dispositiontypes.inline is the embedded body of the attachment.
Copy Code code as follows:

Entity. Contentdisposition.dispositiontype = = Mime_dispositiontypes.attachment

3, the deletion of the operation of the message

The message on the server can be deleted by POP3 protocol, the deletion is simple, mainly through mail. Markfordeletion is identified, the instance operation code is shown below
Copy Code code 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);
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.