Examples of. NET Mailbox Posting mailbox Information _ Practical Tips

Source: Internet
Author: User
Tags datetime httpcontext mailmessage smtpclient ssl connection

Copy Code code as follows:

#region Send Mailbox method
<summary>
Send Mailbox method
</summary>
<param name= "UserEmail" > sent to the email address </param>

<param name= "username" > name </param>
<returns> Send successfully return true otherwise return false</returns>
public bool Getemail (string useremail, string username)
{
DateTime dt = DateTime.Now;
String gettime = dt. Tolongdatestring (). ToString ();
String _mailaddress = "Mailbox Address";
string _mailnickname = "xxx";
String _mailpassword = "Mailbox Password";
String _mailsmtphost = "smtp.163.com";
String _mailsmtpport = "25";
string _to = UserEmail;
String _title = "xxxxxx";
String _body = "Dear" "+ Username +" "User:<br><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 Get host Name
<summary>
Get host name returned as www.myWeb.com or www.myWeb.com:8080 note 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", "to", "Mail Content", "message title", "Sender", "Sender name", "Password", "SMTP Host", "Port") isbodyhtml indicates that the message used is HTML format. Or is the text format Enablessl enabled SSL connection, GMail is needed, 163 do not need ...
</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);/server sending mail
Clint. Credentials = new System.Net.NetworkCredential (Mailfrom, mypwd);
Clint. Enablessl = True;//gmail Necessary
Clint. Timeout = 10000;//Necessary
Try
{
Clint. Send (Onemail);
Savesuclog (UserInfo, MailTo, Mailfrom, Mailfromname, smtphost); Save the correct log
Flag = true;
}
catch (Exception ex)
{
Saveerrlog (UserInfo, MailTo, Mailfrom, Mailfromname, SMTPHost, ex. message);//Save 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 receive the letter name:" + UserInfo);
Sw. WriteLine ("t addressee:" + MailTo);
Sw. WriteLine ("\TSMTP server:" + mailsmtphost);
Sw. WriteLine ("T sender:" + mailfromname + "<" + Mailfrom + ">");
Sw. WriteLine ("---------------------------------------------------------------------------------------------------" );
Sw. Close ();
Sw. Dispose ();
}
<summary>
Save 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 receive the letter name:" + UserInfo);
Sw. WriteLine ("t addressee:" + MailTo);
Sw. WriteLine ("\TSMTP server:" + mailsmtphost);
Sw. WriteLine ("T sender:" + mailfromname + "<" + Mailfrom + ">");
Sw. WriteLine ("\ t error message: \ r \ n" + errmsg);
Sw. WriteLine ("---------------------------------------------------------------------------------------------------" );
Sw. Close ();
Sw. Dispose ();
}

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.