Email sending method (not based on Template)

Source: Internet
Author: User
Tags mailmessage smtpclient
Code

Email sending class
# Region send e-mail

/// <Summary>
/// Use SMTP settings to send mailmessage messages
/// </Summary>
Public static void sendmailmessage (mailmessage message)
{
If (Message = NULL)
Throw new argumentnullexception ("message ");

Try
{
Message. isbodyhtml = true;
Message. bodyencoding = encoding. utf8;
Smtpclient SMTP = new smtpclient (blogsettings. instance. smtpserver );
// Don't send credentials if a server doesn't require it,
// Linux SMTP servers don't like that
If (! String. isnullorempty (blogsettings. instance. smtpusername )){
SMTP. Credentials = new system. net. networkcredential (smtpusername, smtppassword );
}
SMTP. Port = smtpserverport;
SMTP. enablessl = enablessl; // bool type
SMTP. Send (Message );
Onemailsent (Message );
}
Catch (smtpexception)
{
Onemailfailed (Message );
}
Finally
{
// Disable thread destruction.
Message. Dispose ();
Message = NULL;
}
}

/// <Summary>
/// Send the email object asynchronously.
/// </Summary>
/// <Param name = "message"> the message to send. </param>
Public static void sendmailmessageasync (mailmessage message)
{
Threadpool. queueuserworkitem (delegate {sendmailmessage (Message );});
}

/// <Summary>
/// The email is successfully sent.
/// </Summary>
Public static event eventhandler <eventargs> emailsent;
Private Static void onemailsent (mailmessage message)
{
If (emailsent! = NULL)
{
Emailsent (message, new eventargs ());
}
}

/// <Summary>
/// Email sending failed
/// </Summary>
Public static event eventhandler <eventargs> emailfailed;
Private Static void onemailfailed (mailmessage message)
{
If (emailfailed! = NULL)
{
Emailfailed (message, new eventargs ());
}
}

# Endregion

Method call
# Region send e-mail

Private bool sendemail (string email, string name, string subject, string message)
{
Try
{
Using (mailmessage mail = new mailmessage ())
{
Mail. From = new mailaddress (blogsettings. instance. Email, name); // The default email address of blogsettings
Mail. replyto = new mailaddress (email, name );

Mail. to. Add (blogsettings. instance. Email); // The default email address of blogsettings
Mail. Subject = "e-mail-" + subject;

Mail. Body = "<Div style = \" Font: 11px verdana, Arial \ "> ";
Mail. body + = server. htmlencode (Message ). replace ("\ n", "<br/>") + "<br/>"; // html conversion of content
Mail. Body + = "<HR/> <br/> ";
Mail. Body + = "Mail. Body + = "<Div style = \" font-size: 10px; line-Height: 16px \ "> ";
Mail. Body + = "<strong> name: </strong>" + server. htmlencode (name) + "<br/> ";
Mail. Body + = "<strong> E-mail: </strong>" + server. htmlencode (email) + "<br/> ";
If (httpcontext. Current! = NULL)
{
Mail. Body + = "<strong> ip address: </strong>" + httpcontext. Current. Request. userhostaddress + "<br/> ";
Mail. Body + = "<strong> User-Agent: </strong>" + httpcontext. Current. Request. useragent;
}

If (txtattachment. hasfile) // txtattachment is the Server Control for uploading attachments.
{
Attachment attachment = new attachment (txtattachment. postedfile. inputstream, txtattachment. filename );
Mail. attachments. Add (Attachment );
}

Sendmailmessage (Mail); // send an email
}

Return true;
}
Catch (exception ex)
{
Return false;
}
}

# 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.