.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;

/// <summary>
/// SendMail 的摘要说明
/// </summary>
public class SendMail
{

    /// <summary>
    /// 发送邮件方法
    /// </summary>
    /// <param name="toEmail">接收邮件的邮箱</param>
    /// <param name="shValue">1:审核通过  2:审核不通过  3:找回密码 </param>
    /// <returns></returns>
    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;
        }
    }
    /// <summary>
    /// 发送邮件方法
    /// </summary>
    /// <param name="toEmail">接收邮件的邮箱</param>
    /// <param name="shValue">1:审核通过  2:审核不通过  3:找回密码 </param>
    /// <returns></returns>
    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 #某网站报名成功邮件提醒
    /// <summary>
    /// 某网站报名成功邮件提醒
    /// </summary>
    /// <returns></returns>
    string SuccessNotice(string loginid, string mail, string ServerPaths, string item_list)
    {
        StringBuilder sbContent = new StringBuilder();
        sbContent.Append("<div style=\"width:595px;height:1000px;padding-left:100px;padding-right:100px;margin:auto;text-align:left;border:1px solid #e3e3e3\">");
        //if (1 ==1)
        //{
        //    ServerPaths = "
http://localhost:7562/PPT_Htmlweb/jftz/";
        //}
        sbContent.Append("<p align=\"center\"><img src=\"" + ServerPaths + "/jftz/top.jpg\" /></p>");
        sbContent.Append("<p align=\"left\" style=\"margin-top:20px;font-weight:bold;color:#000;\">");
        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 + "</p>");
            sbContent.Append("<p style=\"margin-top:20px;line-height:35px;\">&nbsp;&nbsp;&nbsp;&nbsp;");
            sbContent.Append("&nbsp;&nbsp;&nbsp;&nbsp;"+dt_yj.Rows[0]["A2"].ToString()+":</p>");
            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("<p style=\"margin-top:17px;padding-left:45px;\">" + code + "&nbsp;&nbsp;&nbsp;&nbsp;" + dt_item.Rows[0]["MouldName"].ToString() + "(" + dt_item.Rows[0]["Price"].ToString() + "元)</p>");
                        Amount_Price += Convert.ToInt32(dt_item.Rows[0]["Price"].ToString());
                    }
                }
            }
            sbContent.Append("<p style=\"margin-top:17px;padding-left:45px;\">"+dt_yj.Rows[0]["A3"].ToString()+":" + Amount_Price + "&nbsp;元</p>");
            sbContent.Append("<p style=\"margin-top:10px;line-height:35px;\">");
            sbContent.Append(dt_yj.Rows[0]["A4"].ToString() + ":<br />");
            sbContent.Append(dt_yj.Rows[0]["A5"].ToString() + ":<span style=\"color: #000;font-weight: bold;\">"+dt_yj.Rows[0]["A6"].ToString() + "</span><br />");
            sbContent.Append(dt_yj.Rows[0]["A7"].ToString() + ":<span style=\"color: #000;font-weight: bold;\">" + dt_yj.Rows[0]["A8"].ToString() + "</span><br />");
            sbContent.Append(dt_yj.Rows[0]["A9"].ToString() + ":<span style=\"color: #000;font-weight: bold;\">" + dt_yj.Rows[0]["A10"].ToString() + "</span><br />");
            sbContent.Append(dt_yj.Rows[0]["A11"].ToString() + ":<span style=\"color: #000;font-weight: bold;\">" + dt_yj.Rows[0]["A12"].ToString() + "</span><br />");
            sbContent.Append(dt_yj.Rows[0]["A13"].ToString() + ":<span style=\"color: #000;font-weight: bold;\">" + dt_yj.Rows[0]["A14"].ToString() + "</span></p>");
            sbContent.Append("<div style=\"background:url(" + ServerPaths + "/jftz/" + dt_yj.Rows[0]["A16"].ToString() + ") no-repeat 320px top;height:160px;padding-top:30px;margin-top:-40px;\">");
            sbContent.Append("<p align=\"right\" style=\"margin-top:70px;color:#000;font-weight:bold\">");
            sbContent.Append("某网站</p>");
            sbContent.Append("<p align=\"right\" style=\"margin-top:20px;margin-right:120px;color:#000;font-weight:bold\">");
            sbContent.Append(DateTime.Now.ToString("yyyy 年 MM 月 dd 日"));
            sbContent.Append("</p></div><p style=\"font-size:14px;color:#000;margin-top:5px;\">");
            sbContent.Append("注:<br/>" + dt_yj.Rows[0]["A15"].ToString() + "</div>");
            sbContent.Append("<P/>");
            sbContent.Append("");
            sbContent.Append(getInfo());
        }
        return sbContent.ToString();
    }
    #endregion
    #region 获取找回密码的内容
    /// <summary>
    /// 获取找回密码的内容
    /// </summary>
    /// <returns></returns>
    string getFindPassContent(string loginid, string mail, string ServerPaths)
    {
        StringBuilder sbContent = new StringBuilder();
        sbContent.Append("尊敬的某网站注册用户:");
        sbContent.Append("要为您的帐户启动密码重设过程请点击以下链接:");


        string urls = UrlManage.Encrypt("&LoginID=" + loginid + "&maile=" + mail);
        sbContent.Append("<a href=‘" + ServerPaths + "/resetpwdt.aspx?1=1&2=" + urls + "‘ style=‘/color:blue‘>" + ServerPaths + "/resetpwdt.aspx?1=1" + urls + "</a>");//

        sbContent.Append("");

        sbContent.Append(getInfo());
        return sbContent.ToString();
    }
    #endregion
    #region 邮箱激活帐号
    /// <summary>
    /// 邮箱激活帐号
    /// </summary>
    /// <returns></returns>
    string ActiveAccount(string loginid, string mail, string ServerPaths)
    {
        StringBuilder sbContent = new StringBuilder();
        sbContent.Append("尊敬的某网站注册用户:");
        sbContent.Append("要为您的注册的帐户激活请点击以下链接:");
        string urls = UrlManage.Encrypt("&LoginID=" + loginid + "&maile=" + mail);
        sbContent.Append("<a href=‘" + ServerPaths + "/UserLogin.aspx?1=1&2=" + urls + "‘ style=‘/color:blue‘>" + ServerPaths + "/UserLogin.aspx?1=1" + urls + "</a>");//

        sbContent.Append("");

        sbContent.Append(getInfo());
        return sbContent.ToString();
    }
    #endregion

    string getInfo()
    {
        StringBuilder sbContent = new StringBuilder();
        sbContent.Append("谢谢支持!\n");
        return sbContent.ToString();
    }
    string GetSucessInfo(string con)
    {
        StringBuilder sbContent = new StringBuilder();
        sbContent.Append("<p>尊敬的某网站注册用户:</p><p style=‘/text-indent:20px‘>您好!恭喜您在某网站注册帐号被审核通过!</p><p style=‘/text-indent:20px‘>你可以点击链接2009.bjvvv.com登陆,进行相关操作。</p><p style=‘/text-indent:20px‘> " + con + " </p><p>谢谢您的支持!</p><p>请注意,该电子邮件地址不能接受回复邮件。更多信息请访问我们的帮助中心,网址为:<a href=‘http://192.168.1.102:95‘>http:192.168.1.102</a></p>");
        return sbContent.ToString();
    }
    string GetFiledInfo(string con)
    {
        StringBuilder sbContent = new StringBuilder();
        sbContent.Append("<p>尊敬的某网站注册用户:</p><p style=‘/text-indent:20px‘>您好!很遗憾,您在某网站注册帐号没有被审核通过!</p><p style=‘/text-indent:20px‘>具体原因如下:</p><p style=‘/text-indent:20px;color:red‘>" + con + "</p><p>谢谢您的支持!</p><p>请注意,该电子邮件地址不能接受回复邮件。更多信息请访问我们的帮助中心,网址为:<a href=‘http://192.168.1.102:95‘>http:192.168.1.102</a></p>");
        return sbContent.ToString();
    }
}

 

注意:此功能需要添加一个SessionManage.dll,这个在网上可以down。

.net发送电子邮件关键代码,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。