Key code for. net email sending

Source: Internet
Author: User

Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Net. Mail;
Using System. Text;
Using System. Net;

///


/// Summary of SendMail
///
Public class SendMail
{

///


/// Email sending Method
///
/// Email Address for receiving emails
/// 1: approved; 2: rejected; 3: Password retrieved
///
Public bool SendMessage (string toEmail, string shValue, string loginid)
{
Try
{
String title = string. Empty;
String emailContent = string. Empty;
String ServerPaths = System. Configuration. ConfigurationManager. AppSettings ["ServerPath"]. ToString (); // local IP Address
Switch (shValue)
{
# Region
Case "1 ":
Title = "registration review result of a website -- approved ";
EmailContent = GetSucessInfo (loginid );
Break;
# Endregion
Case "2 ":
Title = "registration review result of a website -- not approved ";
EmailContent = GetFiledInfo (loginid );
Break;
Case "3 ":
Title = "retrieve password ";
EmailContent = getFindPassContent (loginid, toEmail, ServerPaths );
Break;
Case "4 ":
Title = "activating an account by registering an email address for a website ";
EmailContent = ActiveAccount (loginid, toEmail, ServerPaths );
Break;
Default:
Break;
}
String smtpServer = System. Configuration. ConfigurationManager. receivettings ["smtpServer"]. ToString ();
String user = System. Configuration. ConfigurationManager. receivettings ["MailDisplayName"]. ToString ();
String fromEmail = System. Configuration. ConfigurationManager. receivettings ["MainId"]. ToString ();
String password = System. Configuration. ConfigurationManager. receivettings ["MainPwd"]. ToString ();

SmtpClient smtp = new SmtpClient (); // instantiate a SmtpClient
Smtp. DeliveryMethod = SmtpDeliveryMethod. Network; // set the smtp outbound method to Network
Smtp. EnableSsl = false; // whether SSL encryption is enabled on the smtp server
Smtp. Host = smtpServer; // specify the smtp server
Smtp. Credentials = new NetworkCredential (fromEmail, password );
MailMessage mm = new MailMessage (); // instantiate an email class
Mm. Priority = MailPriority. High; // mail Priority, which can be Low, Normal, or High. Normally, Normal is used.
Mm. From = new MailAddress (fromEmail, user, Encoding. GetEncoding (936 ));
Mm. CC. Add (new MailAddress (toEmail, "", Encoding. GetEncoding (936 )));
Mm. Subject = title; // mail title
Mm. SubjectEncoding = Encoding. GetEncoding (936 );
Mm. IsBodyHtml = true; // whether the email body is in HTML format mm. BodyEncoding = Encoding. GetEncoding (936 );
Mm. Body = emailContent;
Smtp. Send (mm );
Return true;
}
Catch
{
Return false;
}
}
///
/// Email sending Method
///
/// Email Address for receiving emails
/// 1: approved; 2: rejected; 3: Password retrieved
///
Public bool SendMessage (string toEmail, string shValue, string loginid, string item_list)
{
Try
{
String title = string. Empty;
String emailContent = string. Empty;
String ServerPaths = System. Configuration. ConfigurationManager. AppSettings ["ServerPath"]. ToString (); // local IP Address
Switch (shValue)
{
Case "5 ":
Title = "email notification for successful registration of a Website ";
EmailContent = SuccessNotice (loginid, toEmail, ServerPaths, item_list );
Break;
Default:
Break;
}
String smtpServer = System. Configuration. ConfigurationManager. receivettings ["smtpServer"]. ToString ();
String user = System. Configuration. ConfigurationManager. receivettings ["MailDisplayName"]. ToString ();
String fromEmail = System. Configuration. ConfigurationManager. receivettings ["MainId"]. ToString ();
String password = System. Configuration. ConfigurationManager. receivettings ["MainPwd"]. ToString ();

SmtpClient smtp = new SmtpClient (); // instantiate a SmtpClient
Smtp. DeliveryMethod = SmtpDeliveryMethod. Network; // set the smtp outbound method to Network
Smtp. EnableSsl = false; // whether SSL encryption is enabled on the smtp server
Smtp. Host = smtpServer; // specify the smtp server
Smtp. Credentials = new NetworkCredential (fromEmail, password );
MailMessage mm = new MailMessage (); // instantiate an email class
Mm. Priority = MailPriority. High; // mail Priority, which can be Low, Normal, or High. Normally, Normal is used.
Mm. From = new MailAddress (fromEmail, user, Encoding. GetEncoding (936 ));
Mm. CC. Add (new MailAddress (toEmail, "", Encoding. GetEncoding (936 )));
Mm. Subject = title; // mail title
Mm. SubjectEncoding = Encoding. GetEncoding (936 );
Mm. IsBodyHtml = true; // whether the email body is in HTML format mm. BodyEncoding = Encoding. GetEncoding (936 );
Mm. Body = emailContent;
Smtp. Send (mm );
Return true;
}
Catch
{
Return false;
}
}
# Region # email notification of successful registration for a website
///


/// Email notification of successful registration for a website
///
///
String SuccessNotice (string loginid, string mail, string ServerPaths, string item_list)
{
StringBuilder sbContent = new StringBuilder ();
SbContent. Append ("");
// If (1 = 1)
//{
// ServerPaths = "http: // localhost: 7562/PPT_Htmlweb/jftz /";
//}
SbContent. Append ("

");
SbContent. Append ("

");
DataTable dt_l = DB. GetCommandDataTable ("SELECT * from tb_laboratory where 1 = 1 and id =" + loginid + "");
String labname = "";
If (dt_l! = Null & dt_l.Rows.Count> 0)
{
Labname = dt_l.Rows [0] ["Names"]. ToString ();
}
DataTable dt_yj = DB. GetCommandDataTable ("select * from tb_jftz where 1 = 1 ");
If (dt_yj! = Null & dt_yj.Rows.Count> 0)
{
SbContent. Append (dt_yj.Rows [0] ["A1"]. ToString () + ":" + labname +"

");
SbContent. Append ("

& Nbsp ;");
SbContent. Append ("& nbsp;" + dt_yj.Rows [0] ["A2"]. ToString () + ":

");
Int Amount_Price = 0;
String [] itemlist = item_list.Split (new char [] {','});
If (item_list.Length> 0)
{
For (int I = 0; I <itemlist. Length; I ++)
{
DataTable dt_item = DB. GetCommandDataTable ("select * from tb_item where 1 = 1 and id =" + itemlist [I]. ToString () + "");
If (dt_item! = Null & dt_item.Rows.Count> 0)
{
String code = dt_item.Rows [0] ["code"]. ToString ();
SbContent. Append ("

"+ Code +" & nbsp; "+ dt_item.Rows [0] [" MouldName "]. toString () + "(" + dt_item.Rows [0] ["Price"]. toString () + "RMB)

");
Amount_Price + = Convert. ToInt32 (dt_item.Rows [0] ["Price"]. ToString ());
}
}
}
SbContent. Append ("

"+ Dt_yj.Rows [0] [" A3 "]. ToString () +": "+ Amount_Price +" & nbsp; yuan

");
SbContent. Append ("

");
SbContent. Append (dt_yj.Rows [0] ["A4"]. ToString () + ":
");
SbContent. Append (dt_yj.Rows [0] ["A5"]. ToString () + ":" + dt_yj.Rows [0] ["A6"]. ToString () +"
");
SbContent. Append (dt_yj.Rows [0] ["A7"]. ToString () + ":" + dt_yj.Rows [0] ["A8"]. ToString () +"
");
SbContent. Append (dt_yj.Rows [0] ["A9"]. ToString () + ":" + dt_yj.Rows [0] ["A10"]. ToString () +"
");
SbContent. Append (dt_yj.Rows [0] ["A11"]. ToString () + ":" + dt_yj.Rows [0] ["A12"]. ToString () +"
");
SbContent. Append (dt_yj.Rows [0] ["A13"]. ToString () + ":" + dt_yj.Rows [0] ["A14"]. ToString () +"

");
SbContent. Append ("");
SbContent. Append ("

");
SbContent. Append ("a website

");
SbContent. Append ("

");
SbContent. Append (DateTime. Now. ToString ("MM dd, yyyy "));
SbContent. Append ("

");
SbContent. Append ("Note:
"+ Dt_yj.Rows [0] [" A15 "]. ToString () + "");
SbContent. Append ("

");
SbContent. Append ("");
SbContent. Append (getInfo ());
}
Return sbContent. ToString ();
}
# Endregion
# Region retrieve password content
///
/// Obtain the password retrieval content
///
///
String getFindPassContent (string loginid, string mail, string ServerPaths)
{
StringBuilder sbContent = new StringBuilder ();
SbContent. Append ("Dear registered user of a Website :");
SbContent. Append ("to enable password resetting for your account, click the following link :");


String urls = UrlManage. Encrypt ("& LoginID =" + loginid + "& maile =" + mail );
SbContent. Append ("" + ServerPaths + "/resetpwdt. aspx? 1 = 1 "+ urls + "");//

SbContent. Append ("");

SbContent. Append (getInfo ());
Return sbContent. ToString ();
}
# Endregion
# Region email activation account
///


/// Email activation account
///
///
String ActiveAccount (string loginid, string mail, string ServerPaths)
{
StringBuilder sbContent = new StringBuilder ();
SbContent. Append ("Dear registered user of a Website :");
SbContent. Append ("click the following link to activate your registered account :");
String urls = UrlManage. Encrypt ("& LoginID =" + loginid + "& maile =" + mail );
SbContent. Append ("" + ServerPaths + "/UserLogin. aspx? 1 = 1 "+ urls + "");//

SbContent. Append ("");

SbContent. Append (getInfo ());
Return sbContent. ToString ();
}
# Endregion

String getInfo ()
{
StringBuilder sbContent = new StringBuilder ();
SbContent. Append ("thank you for your support! \ N ");
Return sbContent. ToString ();
}
String GetSucessInfo (string con)
{
StringBuilder sbContent = new StringBuilder ();
SbContent. Append ("

Dear registered user of a website:

Hello! Congratulations! Your registered account on a website has been approved!

You can click 2009.bjvvv.com to log on and perform related operations.

"+ Con +"

Thank you for your support!

Please note that this email address cannot receive a reply email. For more information, visit our help center at http: 192.168.1.102.

");
Return sbContent. ToString ();
}
String GetFiledInfo (string con)
{
StringBuilder sbContent = new StringBuilder ();
SbContent. Append ("

Dear registered user of a website:

Hello! Sorry, your account registered on a website has not been approved!

The specific reasons are as follows:

"+ Con +"

Thank you for your support!

Please note that this email address cannot receive a reply email. For more information, visit our help center at http: 192.168.1.102.

");
Return sbContent. ToString ();
}
}

Note: This function requires a SessionManage. dll, which can be down online.

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.