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