ASP SendMail send static HTML

Source: Internet
Author: User
Tags mailmessage smtpclient

Background: Now almost every enterprise has to use the mailbox, and in the case of large customers, for our different levels of customers to send the holiday care, as well as our good news, new product notice to them is the problem we urgently need to solve. Effect

Ideas:

1, static Web template, first consider what needs to send, the need to make small and beautiful HTML static page to be sent when used;

2, about the issue of sending, click on the "Send mail" pop-up message box, fill in the theme, HTML server address, the recipient (multiple);

3, the realization of mass.

Code C #:

<summary>
Send failed email Address collection parameters
</summary>
private string Errormail;

Mass Mail Click event

protected void Btnsendmail_click (object sender, EventArgs e)
{
if (string. IsNullOrEmpty (Txtopic.value))
{
Messageshow ("Mail subject required");
Return
}
if (string. IsNullOrEmpty (Txurl.value))
{
Messageshow ("URL content required");
Return
}
if (string. IsNullOrEmpty (Txto.value))
{
Messageshow ("required by the addressee");
Return
}

string email = configurationmanager.appsettings["Managersendmailusername"].  ToString (); Login User Mailbox
String emailpwd = configurationmanager.appsettings["Managersendmailuserpwd"]. ToString (); Login User Mailbox 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 the address does not exist!) ');</script> ");
}
}

<summary>
Send mail
</summary>
<param name= "from" > Sender email Address </param>
<param name= "to" > Recipient email Address </param>
<param name= "Subject" > Email subject </param>
<param name= "isbodyhtml" > whether it is html</param>
<param name= "Body" > Mail body </param>
<param name= "SMTPHost" &GT;SMTP server address, for example:smtp.163.com</param>
<param name= "UserName" > Username </param>
<param name= "password" > Password </param>
<returns> is successful </returns>
public bool Sendmail (string from, string to, string subject, bool isbodyhtml, string body, String smtphost, String Usernam E, string password)
{
bool issuccess = true;
string[] ts = null;
if (!string. IsNullOrEmpty (To))
{
if (to. Contains (";"))
ts = To. Split (';');
Else
ts = new string[] {to};
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. useDefaultCredentials = 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>
Gets the HTML source code for the template under the specified path
</summary>
<param name= "TemplatePath" > Template path </param>
<param name= "Encodingtype" > Web page type (some are UTF8, some are 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 network credentials to system credentials
Webclient.credentials = CredentialCache.DefaultCredentials;

Gets the source code of the Web page for 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, we can expand, the timing of what to send, to achieve a variety of brilliant features. Finally say, I wish everyone the year of the goat, I hope things become!

ASP SendMail send static HTML

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.