码迷,mamicode.com
首页 > Web开发 > 详细

asp.net 客户邮件群发功能 SendMail 发送静态化html

时间:2015-02-13 10:06:31      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

背景:现在几乎每个企业都要用到邮箱,而在大客户量情况下,为我们的不同等级的客户送上节日关怀,以及把我们的喜讯、新品通知到他们是我们急需解决的问题。效果如图

技术分享

思路:

1、静态化网页模版,首先考虑需要发送什么,把需要的东西做成小而精美的html静态化页面以待发送时候用到;

2、关于发送问题,点击“发送邮件”弹出群发邮件的框,填写主题、html的服务器所在地址、收件人(多个);

3、群发的实现。

代码C#:

/// <summary>
/// 发送失败的邮箱地址搜集参数
/// </summary>
private string errorMail;

群发邮件点击事件

protected void BtnSendMail_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txTopic.Value))
        {
            MessageShow("邮件主题必填");
            return;
        }
        if (string.IsNullOrEmpty(txUrl.Value))
        {
            MessageShow("URL内容必填");
            return;
        }
        if (string.IsNullOrEmpty(txTo.Value))
        {
            MessageShow("收件人必填");
            return;
        }

        string email = ConfigurationManager.AppSettings["ManagerSendMailUserName"].ToString();  //登录用户邮箱
        string emailpwd = ConfigurationManager.AppSettings["ManagerSendMailUserPwd"].ToString(); //登录用户邮箱密码


        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(‘全部发送成功!‘);</script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "",
            "<script>alert(‘已发送,但以下地址发送失败:" + errorMail + "可能是地址不存在!‘);</script>");
        }
    }

 

    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <param name="from">发送人邮件地址</param>
    /// <param name="to">接收人邮件地址</param>
    /// <param name="subject">邮件主题</param>
    /// <param name="isBodyHtml">是否是Html</param>
    /// <param name="body">邮件体</param>
    /// <param name="smtpHost">SMTP服务器地址,例如:smtp.163.com</param>
    /// <param name="userName">用户名</param>
    /// <param name="password">密码</param>
    /// <returns>是否成功</returns>
    public bool Sendmail(string from, string to, string subject, bool isBodyHtml, string body, string smtpHost, string userName, 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>
    /// 获取指定路径下的模板的HTML源代码
    /// </summary>
    /// <param name="TemplatePath">模板的路径</param>
    /// <param name="EncodingType">网页类型(有些是UTF8,有些是GB2312)</param>
    /// <returns>源代码</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();

                //设置网络凭证为系统凭证
                webClient.Credentials = CredentialCache.DefaultCredentials;

                //获取指定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);
        }
    }

 

总结:这个功能还是非常有用的,大家可以扩展,定时发送什么,实现各种烂漫的功能。最后说一声,祝大家羊年大吉,心想事成!

asp.net 客户邮件群发功能 SendMail 发送静态化html

标签:

原文地址:http://www.cnblogs.com/chenyjccxy/p/4286402.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!