C # mail sending method [webMail method]

Source: Internet
Author: User
Tags email account mailmessage

There are two ways to send emails in C #: webmail and netmail, when using these two methods to send mail, if you use a public mail server (such as 126 mail server, Sina's Mail Server), authorization authentication is required for sending, if Gmail is used, the number of mails sent each day is also limited. These two methods are the code that I tested. You only need to change the email user name and password to your own. You can also modify the email server, change to your own email server.
/// <Summary>
/// Send Email (with verification, using the new Microsoft recommendation method)
/// </Summary>
/// <Param name = "strTo"> Email </param>
/// <Param name = "strCc"> CC Email </param>
/// <Param name = "strSubject"> title </param>
/// <Param name = "strBody"> content </param>
/// <Param name = "UserName"> email verification account (same as the account configured in web. config) </param>
/// <Param name = "from"> sender's email address, which corresponds to UserName </param>
/// <Param name = "strErrorMsg"> error message </param>
/// <Returns> </returns>
Public static bool WebSendEmail (string strTo, string strCc, string strSubject, string strBody, ref string strErrorMsg)
{
System. Web. Mail. MailMessage message = new System. Web. Mail. MailMessage ();
System. text. regularExpressions. regex reg = new System. text. regularExpressions. regex (@ "\ w + ([-+. '] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w + )*");

Bool bState = false;
String strSMTPServer = "";

Try
{
StrSMTPServer = Convert. ToString (System. Configuration. ConfigurationManager. deleettings ["SMTP"]);
StrSMTPServer = ""? "Localhost": strSMTPServer;

String strFromAddr = Convert. ToString (System. Configuration. ConfigurationManager. etettings ["FromAddress"]);
If (reg. IsMatch (strFromAddr ))
{
Message. From = strFromAddr;
}
Else
{
Throw new Exception ("The Email Address is wrong, Please reset the Email Address in the web. config file! ");
}

String strTemp = "";
Foreach (string str in strTo. Split (';'))
{
If (reg. IsMatch (str ))
If (! StrTemp. Contains (str ))
StrTemp + = str + ";";
}

Message. Cc = "";
Foreach (string str in strCc. Split (';'))
{
If (reg. IsMatch (str ))
If (! Message. Cc. Contains (str ))
Message. Cc + = str + ";";
}

Message. Subject = strSubject;
Message. BodyFormat = System. Web. Mail. MailFormat. Html;

Message. Body = "// The following figure shows how to load the attachment.
MailAttachment attachment1 = new MailAttachment (@ "d: \ My Documents ents \ test1.doc ");
MailAttachment attachment2 = new MailAttachment ("d: \ Documents \ test2.doc ");
Message. Attachments. Add (attachment1 );
Message. Attachments. Add (attachment2 );

Message. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true ");
Message. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1 ");
// The email account and password here must be consistent with the account and password set in the following configuration file
Message. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", "xxxxxxxxx"); // email account, such as the Test11@126.com account: Test11
Message. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", "xxxxxxxx"); // Email Password
// This indicates the port of the email server. You can leave it unspecified.
// Message. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "25 ");

Foreach (string str in strTemp. Split (';'))
{
If (reg. IsMatch (str ))
{
Message. To = str;
Message. BodyEncoding = System. Text. Encoding. UTF8;
System. Web. Mail. SmtpMail. SmtpServer = strSMTPServer;

System. Web. Mail. SmtpMail. Send (message );
}
}

BState = true;
}
Catch (Exception ex)
{
System. IO. file. appendAllText ("C: \ Mail_Log.ini", string. format ("{0: yyyy/MM/dd HH: mm: ss} \ r \ n {1} \ r \ n", DateTime. now, ex. message ));
BState = false;
StrErrorMsg = ex. Message;
}

Return bState;
}
// Test email sending
Protected void btnSend_Click (object sender, EventArgs e)
{
Try
{

Email. SendEmail ("xxxxxx@163.com", "", "Test Email", "Test Send Email ");

}
Catch (Exception ex)
{
Response. Write (ex. Message );
}
}
Configure the email in the webconfig file as follows:

From weizhiai12's column
 

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.