Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Lumisoft.NET component POP3 mail receive and delete operation

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you about how to use the Lumisoft.NET component POP3 email receive and delete operation. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Lumisoft.NET component is a very powerful open source component with mail sending, mail receiving and other functions, which is generally used to deal with mail-related operations, is very appropriate.

1. POP3 login and header information acquisition

To use POP3 first, you must create an object of POP3_Client, then connect and log in through Connect and Login, as shown in the following code.

The copy code is as follows:

Using (POP3_Client popClient = new POP3_Client ())

{

PopClient.Logger = new Logger ()

PopClient.Logger.WriteLog + = new EventHandler (WriteLog)

PopClient.Connect (pop3Server, pop3Port, pop3UseSsl)

PopClient.Login (username, password)

The email download of POP3 is carried out through the property Messages object of the POP3_Client object, and each POP3_ClientMessage represents a complete email message. At first, you should just get some simple email information (including the unique ID UID of the email), so as to improve the processing speed of the POP3 protocol, as shown in the following code.

The copy code is as follows:

Foreach (POP3_ClientMessage message in popClient.Messages)

In order to further obtain the message header information, the following conversion is required

The copy code is as follows:

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

The converted Mail_Message carries a lot of necessary information of the mail header file, such as sender, sender name, receiving address, CC address, email title, email date and so on.

The information of these e-mail addresses is recorded through the Mail_t_Mailbox object, which generally contains the Address of the e-mail address and the display name DisplayName, which is very convenient to display, such as we can escape and record it in the database.

The copy 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)

}

}

The copy 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 will have a unique Id within the scope of the Pop3 server. Check whether this Id exists to know if you have received this email before.

The copy code is as follows:

Info.MailUid = message.UID

The header information of each email will contain a date, which can be obtained as follows

The copy code is as follows:

Info.Date = mime_header.Date

The title information can be obtained from the following code

The copy code is as follows:

Info.Title = mime_header.Subject;/

2. Access to email body information and attachment information.

If you need to further get the body content of the message, you need to further transform the information, MessageToByte the message object, and then use the function Mail_Message.ParseFromByte to transform.

The copy 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

{

/ / masking the problem of coding error. When the error exists in BodyText but BodyHtmlText does not exist, accessing BodyHtmlText will occur.

}

The attachment of the email carries the information through MIME_Entity, so we need to obtain the object through mime_message.GetAttachments (true, true) and convert it into attachment information.

The copy 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 get the file byte stream in the attachment, then you need to further convert to a MIME_b_SinglepartBase object.

The copy 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 distinguish whether the attachment in the message is an embedded picture attachment or a real attachment, then you can judge by the following code, if it is MIME_DispositionTypes.Attachment, it is a common attachment, and MIME_DispositionTypes.Inline is an attachment embedded in the body.

The copy code is as follows:

Entity.ContentDisposition.DispositionType = = MIME_DispositionTypes.Attachment

3. Delete mail

The mail on the server can be deleted by POP3 protocol. The deletion operation is very simple. It is mainly identified through mail.MarkForDeletion. The example operation code is shown below.

The copy 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)

}

}

}

}

Thank you for reading! On the "Lumisoft.NET component POP3 mail receiving and deleting operation how to use" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report