An instance of. net mailbox publishing mailbox Information

Source: Internet
Author: User
Tags mailmessage smtpclient

Copy codeThe Code is as follows:
# Region email sending Method
/// <Summary>
/// Email sending Method
/// </Summary>
/// <Param name = "useremail"> email address to be sent </param>

/// <Param name = "username"> name </param>
/// <Returns> if the message is sent successfully, true is returned. Otherwise, false is returned. </returns>
Public bool GetEmail (string useremail, string username)
{
DateTime dt = DateTime. Now;
String gettime = dt. ToLongDateString (). ToString ();
String _ MailAddress = "email address ";
String _ MailNickName = "xxx ";
String _ MailPassword = "email password ";
String _ MailSmtpHost = "smtp.163.com ";
String _ MailSmtpPort = "25 ";
String _ To = useremail;
String _ Title = "xxxxxx ";
String _ Body = "dear" + username + "" User: <br> hello! ";
String strXmlFile = HttpContext. Current. Server. MapPath ("/config/mail. config ");
XmlControl XmlTool = new XmlControl (strXmlFile );
XmlTool. Update ("Root/Address", _ MailAddress );
XmlTool. Update ("Root/NickName", _ MailNickName );
XmlTool. Update ("Root/Password", _ MailPassword );
XmlTool. Update ("Root/SmtpHost", _ MailSmtpHost );
XmlTool. Update ("Root/SmtpPort", _ MailSmtpPort );
XmlTool. Update ("Root/ToAddress", useremail );
XmlTool. Update ("Root/UserInfo", username );
XmlTool. Save ();
XmlTool. Dispose ();
If (GmailHelp. GmailSendMail (username, _ To, _ Body, _ Title, _ MailAddress, _ MailNickName, _ MailPassword, _ MailSmtpHost, int. Parse (_ MailSmtpPort )))
{
Return true;
}
Else
{
Return false;
}
}
# Endregion

# Region obtain the Host Name
/// <Summary>
/// Obtain the host name. The returned result is www.myWeb.com or www.myWeb.com: 8080. Note that no http ://
/// </Summary>
/// <Returns> </returns>
Public static string GetHttpHost ()
{
Int Port = HttpContext. Current. Request. Url. Port;
If (Port = 80)
{
Return HttpContext. Current. Request. Url. Host;
}
Else
{
Return HttpContext. Current. Request. Url. Host + ":" + Port;
}

}
# Endregion

/// <Summary>
/// Call GmailSendMail ("Recipient Name", "recipient", "email content", "email title", "sender", "sender name", "password ", "smtp host", "Port") IsBodyHtml indicates whether the used mail is in HTML format or whether the EnableSsl in Text format enables SSL connections. GMail requires, 163 is not needed...
/// </Summary>
/// <Param name = "MailTo"> </param>
/// <Param name = "StrBody"> </param>
/// <Param name = "strSubjec"> </param>
/// <Param name = "MailFrom"> </param>
/// <Param name = "MailFromName"> </param>
/// <Param name = "myPwd"> </param>
/// <Param name = "smtpHost"> </param>
/// <Param name = "smtpPort"> </param>
/// <Returns> </returns>
Public static bool GmailSendMail (string UserInfo, string MailTo, string StrBody, string strSubjec, string MailFrom, string MailFromName, string myPwd, string smtpHost, int smtpPort)
{

Bool flag = true;
String [] _ mail = MailTo. Split (',');
System. Net. Mail. MailMessage onemail = new System. Net. Mail. MailMessage (MailFrom, MailTo, strSubjec, StrBody );
Onemail. BodyEncoding = System. Text. Encoding. UTF8;
Onemail. IsBodyHtml = true;
// Onemail. From = new System. Net. Mail. MailAddress (MailFrom );
Onemail. From = new MailAddress (MailFrom, "xxx", System. Text. Encoding. UTF8 );
Onemail. To. Add (new System. Net. Mail. MailAddress (MailTo ));
Onemail. Subject = strSubjec;
Onemail. Body = StrBody;
System. Net. Mail. SmtpClient clint = new System. Net. Mail. SmtpClient (smtpHost, smtpPort); // the server that sends the email
Clint. Credentials = new System. Net. NetworkCredential (MailFrom, myPwd );
Clint. EnableSsl = true; // required by Gmail
Clint. Timeout = 10000; // required
Try
{
Clint. Send (onemail); // Send
SaveSucLog (UserInfo, MailTo, MailFrom, MailFromName, smtpHost); // Save the correct log
Flag = true;
}
Catch (Exception ex)
{
SaveErrLog (UserInfo, MailTo, MailFrom, MailFromName, smtpHost, ex. Message); // Save the Error Log
Flag = false;
}
Return flag;
}
/// <Summary>
/// Save the correct log
/// </Summary>
/// <Param name = "MailFrom"> </param>
/// <Param name = "MailFromName"> </param>
/// <Param name = "MailSmtpHost"> </param>
Private static void SaveSucLog (string UserInfo, string MailTo, string MailFrom, string MailFromName, string MailSmtpHost)
{
System. IO. streamWriter sw = new System. IO. streamWriter (HttpContext. current. server. mapPath ("/log/mailsuccess _" + DateTime. now. toString ("yyyyMMdd") + ". txt "), true, System. text. encoding. UTF8 );
Sw. WriteLine (System. DateTime. Now. ToString ());
Sw. WriteLine ("\ t Recipient Name:" + UserInfo );
Sw. WriteLine ("\ t recipient:" + MailTo );
Sw. WriteLine ("\ tSMTP server:" + MailSmtpHost );
Sw. WriteLine ("\ t Sender:" + MailFromName + "<" + MailFrom + "> ");
Sw. WriteLine ("succeeded ("---------------------------------------------------------------------------------------------------");
Sw. Close ();
Sw. Dispose ();
}
/// <Summary>
/// Save the Error Log
/// </Summary>
/// <Param name = "MailFrom"> </param>
/// <Param name = "MailFromName"> </param>
/// <Param name = "MailSmtpHost"> </param>
/// <Param name = "ErrMsg"> </param>
Private static void SaveErrLog (string UserInfo, string MailTo, string MailFrom, string MailFromName, string MailSmtpHost, string ErrMsg)
{
System. IO. streamWriter sw = new System. IO. streamWriter (HttpContext. current. server. mapPath ("/log/mailerror _" + DateTime. now. toString ("yyyyMMdd") + ". txt "), true, System. text. encoding. UTF8 );
Sw. WriteLine (System. DateTime. Now. ToString ());
Sw. WriteLine ("\ t Recipient Name:" + UserInfo );
Sw. WriteLine ("\ t recipient:" + MailTo );
Sw. WriteLine ("\ tSMTP server:" + MailSmtpHost );
Sw. WriteLine ("\ t Sender:" + MailFromName + "<" + MailFrom + "> ");
Sw. WriteLine ("\ t error message: \ r \ n" + ErrMsg );
Sw. WriteLine ("succeeded ("---------------------------------------------------------------------------------------------------");
Sw. Close ();
Sw. Dispose ();
}

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.