Asp.net client mail group function SendMail sends static html, mail group sendmail

Source: Internet
Author: User

Asp.net client mail group function SendMail sends static html, mail group sendmail

Background: At present, almost every enterprise needs to use their mailboxes. In the case of large customers, we provide holiday care for our customers of different levels, it is an urgent problem for us to notify us of our news and new products. Effect

Ideas:

1. Static web page templates: first, consider what needs to be sent, and make the required things into small and exquisite html static pages to be used for sending;

2. For sending questions, click "send mail" to pop up the group email box, and fill in the subject, html server address, and recipient (multiple );

3. Implementation of group sending.

Code C #:

/// <Summary>
/// Email address collection parameters for failed sending
/// </Summary>
Private string errorMail;

Group email Click Event

Protected void BtnSendMail_Click (object sender, EventArgs e)
{
If (string. IsNullOrEmpty (txTopic. Value ))
{
MessageShow ("subject required ");
Return;
}
If (string. IsNullOrEmpty (txUrl. Value ))
{
MessageShow ("URL content required ");
Return;
}
If (string. IsNullOrEmpty (txTo. Value ))
{
MessageShow ("recipient required ");
Return;
}

String email = ConfigurationManager. receivettings ["ManagerSendMailUserName"]. ToString (); // log on to the user's email address
String emailpwd = ConfigurationManager. receivettings ["ManagerSendMailUserPwd"]. ToString (); // log on to the user's email password


String s = GetHtmlCode (txUrl. Value, "gb2312 ");
Bool restr = Sendmail (email, txTo. Value, txTopic. Value, true, s, "smtp.exmail.qq.com", email, emailpwd );
If (restr)
{
ClientScript. RegisterStartupScript (this. GetType (), "", "<script> alert ('all sent successfully! '); </Script> ");
}
Else
{
ClientScript. RegisterStartupScript (this. GetType (),"",
"<Script> alert ('sent, but the following address failed to send:" + errorMail + "may be because the address does not exist! '); </Script> ");
}
}

 

/// <Summary>
/// Send an email
/// </Summary>
/// <Param name = "from"> sender's email address </param>
/// <Param name = "to"> recipient's email address </param>
/// <Param name = "subject"> subject </param>
/// <Param name = "isBodyHtml"> whether it is Html </param>
/// <Param name = "body"> body </param>
/// <Param name = "smtpHost"> SMTP server address, for example, smtp.163.com </param>
/// <Param name = "userName"> User name </param>
/// <Param name = "password"> password </param>
/// <Returns> Successful </returns>
Public bool Sendmail (string from, string to, string subject, bool isBodyHtml, string body, string smtpHost, string userName, string password)
{
Bool isSuccess = true;
String [] ts = null;
If (! String. IsNullOrEmpty ())
{
If (to. Contains (";"))
Ts = to. Split (';');
Else
Ts = new string [] {};
Foreach (string t in ts)
{
If (! String. IsNullOrEmpty (t ))
{
MailMessage mm = new MailMessage ();
Mm. From = new MailAddress (from );
Mm. To. Add (new MailAddress (t ));
Mm. Subject = subject;
Mm. IsBodyHtml = isBodyHtml;
Mm. Body = body;
SmtpClient SC = new SmtpClient ();
SC. Host = smtpHost;
SC. Port = 25;
SC. EnableSsl = false;
SC. Credentials = new NetworkCredential (userName, password );
SC. usedefacrecredentials = true;
SC. Credentials = new System. Net. NetworkCredential (userName, password );
SC. DeliveryMethod = SmtpDeliveryMethod. Network;
Try
{
SC. Send (mm );
}
Catch (Exception x)
{
ErrorMail + = t + ";";
IsSuccess = false;
Continue;
}
}
}
}
Return isSuccess;
}

 

/// <Summary>
/// Obtain the HTML source code of the template in the specified path
/// </Summary>
/// <Param name = "TemplatePath"> template path </param>
/// <Param name = "EncodingType"> webpage type (UTF8 and GB2312) </param>
/// <Returns> Source Code </returns>
Private string GetHtmlCode (string TemplatePath, string EncodingType)
{
Try
{
If (TemplatePath! = String. Empty)
{
String ForesideUriPath = string. Empty;

If (! TemplatePath. ToLower (). StartsWith ("http ://"))
ForesideUriPath = HttpContext. Current. Server. MapPath ("~ /") + TemplatePath;
Else
ForesideUriPath = TemplatePath;

WebClient webClient = new WebClient ();

// Set the network credential as the system credential
WebClient. Credentials = CredentialCache. DefaultCredentials;

// Obtain the source code of the webpage with the specified URI
Byte [] byteDataBuffer = webClient. DownloadData (ForesideUriPath );

String htmlCode = "";
If (EncodingType = "UTF8 ")
{
HtmlCode = Encoding. UTF8.GetString (byteDataBuffer );
}
Else
{
HtmlCode = Encoding. GetEncoding (EncodingType). GetString (byteDataBuffer );
}

HtmlCode = Regex. Replace (htmlCode, @ "<! DOCTYPE \ s * HTML \ s * PUBLIC [^>] +> "," ", RegexOptions. Singleline );
HtmlCode = Regex. Replace (htmlCode, @ "\ s +", "", RegexOptions. Singleline );

Return htmlCode;
}
Else
{
Return "";
}
}
Catch (Exception ee)
{
Throw (ee );
}
}

 

Summary: this function is still very useful. You can extend it and regularly send messages to implement various lazy functions. Finally, I wish you all the best in the future!

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.