C # Send email (asynchronous)

Source: Internet
Author: User
Tags mailmessage smtpclient

[Csharp]
/// Verify the regular expression of the email
[Csharp] view plaincopyprint?
String emailStr = @ "^ ([\ w-\.] +) @ (\ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\.) | ([\ w-] + \.) +) ([a-zA-Z] {2, 4} | [0-9] {1, 3}) (\]?) $ ";
[Csharp]
/// <Summary>
/// Send an email Asynchronously
/// </Summary>
/// <Param name = "fromEmail"> sender's email </param>
/// <Param name = "fromPwd"> sender's email password </param>
/// <Param name = "toEmail"> recipient's email </param>
/// <Param name = "subject"> email title </param>
/// <Param name = "body"> the email is empty. </param>
/// <Param name = "emailType"> mail type </param> smtp.163.com.cn; smtp.qq.com.cn; smtp.126.com.cn; smtp.sina.com.cn
Private void SendEmailAsync (string fromEmail, string fromPwd, string toEmail, string subject, string body, string emailType)
{
MailAddress addrFrom = new MailAddress (fromEmail, fromEmail );
MailAddress addrTo = new MailAddress (toEmail, toEmail );
MailMessage mm = new MailMessage (addrFrom, addrTo );
Mm. BodyEncoding = Encoding. UTF8;
Mm. IsBodyHtml = true;
Mm. Subject = subject;
Mm. Body = body;
 
If (! String. IsNullOrEmpty (attFile ))
{
Attachment att = new Attachment (attFile, MediaTypeNames. Application. Octet );
ContentDisposition cd = att. ContentDisposition;
Cd. CreationDate = File. GetCreationTime (attFile );
Cd. ModificationDate = File. GetLastWriteTime (attFile );
Cd. ReadDate = File. GetLastAccessTime (attFile );
Mm. Attachments. Add (att); // Add an attachment.
}
 
NetworkCredential nc = new NetworkCredential (fromEmail, fromPwd );
SmtpClient smtp = new SmtpClient (emailType );
Smtp. usedefacrecredentials = false;
Smtp. Credentials = nc;
Smtp. EnableSsl = false; www.2cto.com
Smtp. DeliveryMethod = SmtpDeliveryMethod. Network;
Smtp. SendCompleted + = new SendCompletedEventHandler (smtp_SendCompleted );
Smtp. SendAsync (mm, "OK ");
}
 
Void smtp_SendCompleted (object sender, AsyncCompletedEventArgs e)
{
If (e. UserState. ToString () = "OK ")
MessageBox. Show ("sent successfully! "," Prompt ", MessageBoxButtons. OK, MessageBoxIcon. Information );
}

Related Article

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.