[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 );
}