Share an asynchronous mail class

Source: Internet
Author: User
Tags mailmessage smtpclient

First, define a base class for mail information, as shown below:

/// <Summary>
/// Base message class used for emails
/// </Summary>
Public class message
{
# Region Constructor
/// <Summary>
/// Constructor
/// </Summary>
Public message ()
{
}
# Endregion

# Region Properties
/// <Summary>
/// Whom the message is
/// </Summary>
Public Virtual string to {Get; set ;}

/// <Summary>
/// The subject of the email
/// </Summary>
Public Virtual string subject {Get; set ;}

/// <Summary>
/// Whom the message is from
/// </Summary>
Public Virtual string from {Get; set ;}

/// <Summary>
/// Body of the text
/// </Summary>
Public Virtual string body {Get; set ;}

# Endregion
}

Then define a mail sending class and use NetMail to send the mail. The thread pool self-contained in. NET is used to send the mail,

The code for asynchronous sending is as follows:

/// <Summary>
/// Utility for sending an email
/// </Summary>
Public class emailsender: Message
{
# Region Constructors

/// <Summary>
/// Default constructor
/// </Summary>
Public emailsender ()
{
Attachments = new list <attachment> ();
Embeddedresources = new list <resourceresource> ();
Priority = mailpriority. normal;
}

# Endregion

# Region Public Functions

/// <Summary>
/// Sends an email
/// </Summary>
/// <Param name = "message"> the body of the message </param>
Public void Sendmail (string message)
{
Body = message;
Sendmail ();
}

/// <Summary>
/// Sends a piece of mail asynchronous
/// </Summary>
/// <Param name = "message"> message to be sent </param>
Public void sendmailasync (string message)
{
Body = message;
Threadpool. queueuserworkitem (delegate {Sendmail ();});
}

/// <Summary>
/// Sends an email
/// </Summary>
Public void Sendmail ()
{
Using (system. net. Mail. mailmessage message = new system. net. Mail. mailmessage ())
{
Char [] splitter = {',',';'};
String [] addresscollection = to. Split (splitter );
For (INT x = 0; x <addresscollection. length; ++ X)
{
If (! String. isnullorempty (addresscollection [X]. Trim ()))
Message. to. Add (addresscollection [x]);
}
If (! String. isnullorempty (CC ))
{
Addresscollection = cc. Split (splitter );
For (INT x = 0; x <addresscollection. length; ++ X)
{
If (! String. isnullorempty (addresscollection [X]. Trim ()))
Message. CC. Add (addresscollection [x]);
}
}
If (! String. isnullorempty (BCC ))
{
Addresscollection = BCC. Split (splitter );
For (INT x = 0; x <addresscollection. length; ++ X)
{
If (! String. isnullorempty (addresscollection [X]. Trim ()))
Message. bcc. Add (addresscollection [x]);
}
}
Message. Subject = subject;
Message. From = new system. net. Mail. mailaddress (from ));
Alternateview bodyview = alternateview. createalternateviewfromstring (body, null, mediatypenames. Text. html );
Foreach (receivresource resource in embeddedresources)
{
Bodyview. reflect resources. Add (Resource );
}
Message. alternateviews. Add (bodyview );
// Message. Body = body;
Message. Priority = priority;
Message. subjectencoding = system. Text. encoding. getencoding ("ISO-8859-1 ");
Message. bodyencoding = system. Text. encoding. getencoding ("ISO-8859-1 ");
Message. isbodyhtml = true;
Foreach (Attachment tempattachment in attachments)
{
Message. attachments. Add (tempattachment );
}
System. net. Mail. smtpclient SMTP = new system. net. Mail. smtpclient (server, Port );
If (! String. isnullorempty (username )&&! String. isnullorempty (password ))
{
SMTP. Credentials = new system. net. networkcredential (username, password );
}
If (usessl)
SMTP. enablessl = true;
Else
SMTP. enablessl = false;
SMTP. Send (Message );
}
}

/// <Summary>
/// Sends a piece of mail asynchronous
/// </Summary>
Public void sendmailasync ()
{
Threadpool. queueuserworkitem (delegate {Sendmail ();});
}

# Endregion

# Region Properties

/// <Summary>
/// Any attachments that are encoded with this
/// Message.
/// </Summary>
Public list <attachment> attachments {Get; set ;}

/// <Summary>
/// Any attachment (usually images) that need to be embedded in the message
/// </Summary>
Public list <shortresource> embeddedresources {Get; set ;}

/// <Summary>
/// The priority of this message
/// </Summary>
Public mailpriority priority {Get; set ;}

/// <Summary>
/// Server location
/// </Summary>
Public String server {Get; set ;}

/// <Summary>
/// User name for the server
/// </Summary>
Public String username {Get; set ;}

/// <Summary>
/// Password for the server
/// </Summary>
Public String password {Get; set ;}

/// <Summary>
/// Port to send the information on
/// </Summary>
Public int port {Get; set ;}

/// <Summary>
/// Decides whether we are using starttls (SSL) or not
/// </Summary>
Public bool usessl {Get; set ;}

/// <Summary>
/// Carbon copy send (seperate email addresses with a comma)
/// </Summary>
Public String CC {Get; set ;}

/// <Summary>
/// Blind carbon copy send (seperate email addresses with a comma)
/// </Summary>
Public String BCC {Get; set ;}

# Endregion
}

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.