实战篇之实现 OutLook 中以 EDM 形式发送通知邮件

1.写 Html5 的 EDM 模板

EDM 源代码示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Html5-代码示例</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
        img {
            border: none;
        }
        table {
          border-spacing: 0;
          border-collapse: collapse;
        }
        td {
          word-break: break-word;
          -webkit-hyphens: auto;
          -moz-hyphens: auto;
          hyphens: auto;
          border-collapse: collapse !important;
        }
        table, tr, td {
          padding: 0;
        }
        .auto-style1 {
            height: 28px;
        }
    </style>
</head>
<body>
    <table bgcolor="#f3f4e6" width="900" align="center" border="0" cellspacing="0" cellpadding="0" class="body">
        <tbody>
            <tr>
                <td width="100%">
                    <table width="100%" height="55" align="center" border="0" cellspacing="0" cellpadding="0">
                        <tbody>
                            <tr>
                                <td height="55" align="right" bgcolor="#ffc439">
                                    <td>#UserNsame#</td>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>
</body>
</html>

2.邮件发送核心代码

 public class MyEmailCode
 {

       #region  模板获取
        /// 获取邮件模板
        public static string GetMailTemp(string tempPath, string UserName)
        {
            StringBuilder content = new StringBuilder();
            if (File.Exists(tempPath))
            {
              
                using (StreamReader sr = new StreamReader(tempPath, Encoding.GetEncoding("utf-8")))
                {
                    String srLine;
                    while ((srLine = sr.ReadLine()) != null)
                    {
                        content.AppendLine(srLine);
                    }
                    content.Replace("#UserName#", UserName);
                }
                return content.ToString();
            }
            return null;
        }
        #endregion

         static bool SendEmail(string from, string to, string content, string title, out string message, IList<string> ccs = null, IList<string> bccs = null)
        {
            Encoding encoding = Encoding.GetEncoding("utf-8");
            string strHost = host;
            string strAcount = account;
            string strPwd = pwd;
            string strForm = from;
            string strTo = to;
            SmtpClient _smtpClient = new SmtpClient();
            _smtpClient.Host = strHost;
            _smtpClient.Credentials = new System.Net.NetworkCredential(strAcount,strPwd);

            _smtpClient.Port = 587;            

            _smtpClient.EnableSsl = true;            

             MailMessage _mailMessage = new MailMessage(new MailAddress(strForm, "", encoding),                                                      

             new MailAddress(strTo, "", encoding));            

             _mailMessage.Subject = title;            

             _mailMessage.Body = content;            

             _mailMessage.BodyEncoding = encoding;            

             _mailMessage.IsBodyHtml = true;            

            _mailMessage.Priority = MailPriority.Normal;            

             if(ccs!=null)                

            foreach (string cc in ccs)                

            {  

                    if (!string.IsNullOrEmpty(cc)) 

                   {   _mailMessage.CC.Add(cc);    }                

              }     

        if(bccs!=null)
                foreach (string bcc in bccs)
                {
                    if (!string.IsNullOrEmpty(bcc))
                    {
                        _mailMessage.CC.Add(bcc);
                    }
                }       

        try            

        {   

            var sendThread = new Thread(SendMailCore); 

           sendThread.Start(_mailMessage);

          _smtpClient.Send(_mailMessage);                

            message = null;                

               return true;            

           }            

           catch (Exception ex)            

          {                

               message = ex.Message;                

               return false;

            }

        }

       

   private static void SendMailCore(object message)        

  {       

      //邮件配置 Host,账户和密码     

       string mailHost = ConfigurationManager.AppSettings["MailHost"].Trim().ToString();            

       string mailAccount = ConfigurationManager.AppSettings["MailAccount"].Trim().ToString();            

       string mailPassword = ConfigurationManager.AppSettings["MailPassword"].Trim().ToString();            

        MailMessage msg = message as MailMessage;             SmtpClient smtpClient = new SmtpClient            

        {                

             Host = mailHost,                 Port =587,                 EnableSsl = true,                

             Credentials = new System.Net.NetworkCredential { UserName = mailAccount, Password = mailPassword }           

           };

          smtpClient.Send(msg);        

      }

   public static bool SendEmail(string mailTitle,string toEmail, string content, out string message,IList<string> ccEmail=null, IList<string> bccEmail=null)        

  {                  

           string mailFrom= ConfigurationManager.AppSettings["MailAccount"].Trim().ToString();          

           return SendEmail( mailFrom, toEmail, content, mailTitle, out message, ccEmail, bccEmail);      

   }

3.方法调用

string _pathTemp = Server.MapPath("");//html 路径
string   _mailTitle = "";//邮件文件头

string strContent = EmailCode.GetMailTemp(_pathTemp, GuRuAcount, GuRuPassword);
MyEmailCode.SendEmail(_mailTitle, email, strContent, out message);