当前位置:首页 > 新闻中心 >

.net发送电子邮件关键代码

编辑:北京聚贤贵都宾馆有限公司时间:2017-09-08 19:04:53阅读次数:2
.net发送电子邮件关键代码

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;

///
/// SendMail 的摘要说明
///
public class SendMail
{

///
/// 发送邮件方法
///
/// 接收邮件的邮箱
/// 1:审核通过 2:审核不通过 3:找回密码
///
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();//本机IP
switch (shValue)
{
#region
case "1":
title = "某网站注册审核结果--审核通过";
emailContent = GetSucessInfo(loginid);
break;
#endregion
case "2":
title = "某网站注册审核结果--审核未通过";
emailContent = GetFiledInfo(loginid);
break;
case "3":
title = "找回密码";
emailContent = getFindPassContent(loginid, toEmail, ServerPaths);
break;
case "4":
title = "某网站注册邮箱激活帐号";
emailContent = ActiveAccount(loginid, toEmail, ServerPaths);
break;
default:
break;
}
string smtpServer = System.Configuration.ConfigurationManager.AppSettings["smtpServer"].ToString();
string user = System.Configuration.ConfigurationManager.AppSettings["MailDisplayName"].ToString();
string fromEmail = System.Configuration.ConfigurationManager.AppSettings["MainId"].ToString();
string password = System.Configuration.ConfigurationManager.AppSettings["MainPwd"].ToString();

SmtpClient smtp = new SmtpClient(); //实例化一个SmtpClient
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //将smtp的出站方式设为 Network
smtp.EnableSsl = false;//smtp服务器是否启用SSL加密
smtp.Host = smtpServer;//指定 smtp 服务器
smtp.Credentials = new NetworkCredential(fromEmail, password);
MailMessage mm = new MailMessage(); //实例化一个邮件类
mm.Priority = MailPriority.High; //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可
mm.From = new MailAddress(fromEmail, user, Encoding.GetEncoding(936));
mm.CC.Add(new MailAddress(toEmail, "", Encoding.GetEncoding(936)));
mm.Subject = title; //邮件标题
mm.SubjectEncoding = Encoding.GetEncoding(936);
mm.IsBodyHtml = true; //邮件正文是否是HTML格式mm.BodyEncoding = Encoding.GetEncoding(936);
mm.Body = emailContent;
smtp.Send(mm);
return true;
}
catch
{
return false;
}
}
///
/// 发送邮件方法
///
/// 接收邮件的邮箱
/// 1:审核通过 2:审核不通过 3:找回密码
///
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();//本机IP
switch (shValue)
{
case "5":
title = "某网站报名成功邮件提醒";
emailContent = SuccessNotice(loginid, toEmail, ServerPaths, item_list);
break;
default:
break;
}
string smtpServer = System.Configuration.ConfigurationManager.AppSettings["smtpServer"].ToString();
string user = System.Configuration.ConfigurationManager.AppSettings["MailDisplayName"].ToString();
string fromEmail = System.Configuration.ConfigurationManager.AppSettings["MainId"].ToString();
string password = System.Configuration.ConfigurationManager.AppSettings["MainPwd"].ToString();

SmtpClient smtp = new SmtpClient(); //实例化一个SmtpClient
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //将smtp的出站方式设为 Network
smtp.EnableSsl = false;//smtp服务器是否启用SSL加密
smtp.Host = smtpServer;//指定 smtp 服务器
smtp.Credentials = new NetworkCredential(fromEmail, password);
MailMessage mm = new MailMessage(); //实例化一个邮件类
mm.Priority = MailPriority.High; //邮件的优先级,分为 Low, Normal, High,站群软件,通常用 Normal即可
mm.From = new MailAddress(fromEmail, user, Encoding.GetEncoding(936));
mm.CC.Add(new MailAddress(toEmail, "", Encoding.GetEncoding(936)));
mm.Subject = title; //邮件标题
mm.SubjectEncoding = Encoding.GetEncoding(936);
mm.IsBodyHtml = true; //邮件正文是否是HTML格式mm.BodyEncoding = Encoding.GetEncoding(936);
mm.Body = emailContent;
smtp.Send(mm);
return true;
}
catch
{
return false;
}
}
#region #某网站报名成功邮件提醒
///
/// 某网站报名成功邮件提醒
///
///
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");
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("

    ");
sbContent.Append("    "+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;&nbsp;&nbsp;&nbsp;" + dt_item.Rows[0]["MouldName"].ToString() + "(" + dt_item.Rows[0]["Price"].ToString() + "元)

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

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

");
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("

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:恩施网站建设 http://enshi.45qun.com

上一篇:Python笔记之入门 下一篇:最后一页

相关阅读