asp.net 中 自定义方法 发送邮件
邮件内容中包含html标签
当我打开邮箱时 如何 让这段含有html标签的代码被解析
举个例子 发送时邮件内容:<table><tr><td>哈哈哈哈</td><td>呵呵呵呵</td></tr><tr><td>哈哈哈哈</td><td>呵呵呵呵</td></tr></table> 打开接受邮箱时呈现: 哈哈哈哈 呵呵呵呵
哈哈哈哈 呵呵呵呵
邮件内容中包含html标签
当我打开邮箱时 如何 让这段含有html标签的代码被解析
举个例子 发送时邮件内容:<table><tr><td>哈哈哈哈</td><td>呵呵呵呵</td></tr><tr><td>哈哈哈哈</td><td>呵呵呵呵</td></tr></table> 打开接受邮箱时呈现: 哈哈哈哈 呵呵呵呵
哈哈哈哈 呵呵呵呵
MailMessage mess = new MailMessage();
mess.IsBodyHtml = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(smtpUid, smtpPwd);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage(mailFrom, "[email protected]", "标题", "HTML代码");
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
message.BodyEncoding = System.Text.Encoding.UTF8;试试GB2312
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "",
RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<[^>]+>", "",
RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim(); return Htmlstring;
}
{
MailMessage MyMail = new MailMessage();
MyMail.From = new MailAddress("", "");
MyMail.To.Add(new MailAddress(""));
MyMail.Subject = Mailtitle;
MyMail.Body = MailCon;
MyMail.IsBodyHtml = true;
SmtpClient smtpclient = new SmtpClient();
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpclient.Host = "";
smtpclient.Credentials = new System.Net.NetworkCredential(MailFrom, MailPwd);
smtpclient.Send(MyMail);
}
jmail.Message Jmail = new jmail.Message();
DateTime t = DateTime.Now;
String Subject = "";
String body = "";
String FromEmail = "";
String ToEmail = "";
Jmail.Charset = "GB2312";
Jmail.ContentType = "text/html";
Jmail.AddRecipient(ToEmail, "", "");
Jmail.From = FromEmail;
Jmail.MailServerUserName = "";
Jmail.MailServerPassWord = "";
Jmail.Subject = Subject;
Jmail.ContentType="text/html";
Jmail.Body = body + t.ToString();
Jmail.Send("", false);
Jmail.Close(); message.Body ="";
client.Send(message);
//第二个参数发件人的帐号
//第三个参数发件人密码
//第四个参数收件人帐号
//第五个参数主题
//第六个参数内容. public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
{
System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}