.NET電子メールの重要なコードを送信

システムを使用しました。
System.Dataを使用しました。
System.Configurationを使用しました。
System.Webのを使用して、
System.Web.Securityを使用しました。
たSystem.Web.UIを使用しました。
System.Web.UI.WebControlsを使用しました。
System.Web.UI.WebControls.WebPartsを使用しました。
System.Web.UI.HtmlControlsを使用しました。
System.Net.Mailを使用しました。
System.Textのを使用しました。
System.Netを使用しました。

/// <要約>
概要説明/// sendmailは
/// </要約>
パブリッククラスのSendMail
{

    /// <概要>
    メールを送信する///方法
    /// </要約>
    /// <PARAM NAME = "toEmail">メール</ param>の受信
    /// <PARAM NAME = "shValue">を。1 :承認2:監査が3ではない:パスワードを取得</ param>の
    /// <戻り値> </戻り値>
    公共BOOLのSendMessage(文字列toEmail、shValue文字列、文字列のログインID)
    {
        試み
        {
            文字列のタイトル= String.Emptyを。
            emailContent = String.Emptyを文字列、
            文字列ServerPaths System.Configuration.ConfigurationManager.AppSettings = [ "ServerPath"]のToString();//本机IP
            スイッチ(shValue)
            {
                #regionの
                場合"1":
                    タイトル= "サイト登録審査の結果-承認";
                    emailContent = GetSucessInfo(ログインID);
                    BREAK;
                #endregionの
                ケース"2":
                    タイトル= "サイト登録審査の結果-によって承認されていない";
                    emailContent = GetFiledInfo(ログインID );
                    BREAK;
                ケース"3":
                    タイトル= "パスワードを忘れました";
                    emailContent = getFindPassContent(ログインID、toEmail、ServerPaths);
                    BREAK;
                ケース"4":
                    タイトル= "ウェブサイトの登録メールアカウントのアクティベーション";
                    emailContent = ActiveAccount(ログインID、toEmail、ServerPaths)。
                    ブレーク;
                デフォルト:
                    休憩;
            }
            ストリングSMTPSERVER = System.Configuration.ConfigurationManager.AppSettings [ "SMTPSERVER"]のToString()。
            文字列のユーザ= System.Configuration.ConfigurationManager.AppSettings [ "MailDisplayName"]のToString();
            ストリングfromEmail = System.Configuration.ConfigurationManager.AppSettings [ "MainId"]のToString()。
            。文字列のパスワード= System.Configuration.ConfigurationManager.AppSettings [ "MainPwd"]のToString();
          
            SmtpClient SMTP =新しいSmtpClient(); //实例化一个SmtpClient
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network。//将SMTP的出站方式设为ネットワーク
            smtp.EnableSsl = falseは、SSL暗号化が有効になっているかどうか// SMTPサーバ
            smtp.Host = SMTPSERVER; //は、SMTPサーバーを指定した                   
            NetworkCredentialの新しい新=(fromEmail、パスワード)をsmtp.Credentials;
            (=新しいはMailMessageはMailMessageのミリメートルは新しい); //インスタンス化メッセージクラス
            mm.Priority = MailPriority.High; //ロー、ノーマル、ハイ、ノーマルに分けメッセージの優先度は、通常できる              
            ((936)Encoding.GetEncoding、fromEmail、ユーザー)MailAddress新しい新=をmm.From;
            ミリメートル.CC.Add(新しい新しいMailAddress(toEmail、 ""、Encoding.GetEncoding(936)));
            mm.Subject =タイトル; //メッセージヘッダ
            mm.SubjectEncoding = Encoding.GetEncoding(936);
            mm.IsBodyHtml = trueに; / / HTML形式のメッセージ本体はmm.BodyEncoding = Encoding.GetEncoding(936)を有しています。
            = emailContent mm.Body;
            smtp.Send(MM)
            ;真に戻り
        }
        キャッチ
        {
            falseに戻り;
        }
    }
    /// <まとめ>
    メールを送信する方法///
    /// </要約>
    /// <PARAM NAME = "toEmailは">メール</ param>の受信
    /// <PARAM NAME = "shValue">を1:2の監査によって:監査が3ではありません:パスワード</ param>の取得
    /// <戻り値> </戻り値>
    公共BOOLのSendMessage(toEmail文字、文字列shValue、ログインIDの文字列、文字列item_list)
    {
        試み
        {
            文字列のタイトル= String.Emptyを。
            文字列emailContent = String.Emptyを。
            ストリングServerPaths =のSystem.Configuration.ConfigurationManager.AppSettings [ "ServerPath"]のToString(); //本机IP。
            スイッチ(shValue)
            {
                場合"5":
                    タイトル= "某网站报名成功邮件提醒"。
                    emailContent = SuccessNotice(ログインID、toEmail、ServerPaths、item_list)。
                    ブレーク;
                デフォルト:
                    休憩;
            }
            ストリングSMTPSERVER = System.Configuration.ConfigurationManager.AppSettings [ "SMTPSERVER"]のToString()。
            文字列のユーザ= System.Configuration.ConfigurationManager.AppSettings [ "MailDisplayName"]のToString();
            ストリングfromEmail = System.Configuration.ConfigurationManager.AppSettings [ "MainId"]のToString()。
            。文字列のパスワード= 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。

おすすめ

転載: blog.csdn.net/love33jing2012/article/details/22408711