.net学习总结(9)邮件群发

 
  1. #region 电子邮件发送-----------------------------------------------------------------------------
  2.         /// <summary> 
  3.         /// 邮件发送类(请先对Subject主题、 BodyText正文、toEmail收件箱赋值、EAddress邮件地址,Esmtp邮箱的smtp,Epass邮箱密码) 
  4.         /// </summary> 
  5.         /// <returns></returns> */
  6.         public bool SendMails(string ESubject, string EtoEmail, string EBodyText,string userName, string EAddress, string Esmtp, string Epass)
  7.         {
  8.             //把群发的邮件存入数组中
  9.             string toEmail = EtoEmail.Trim();
  10.             toEmail = toEmail.Replace(","",");
  11.             toEmail = toEmail.Replace(" "",");
  12.             toEmail = toEmail.Replace("  "",");
  13.             toEmail = toEmail.Replace("|"",");
  14.             toEmail = toEmail.Replace("//", ",");
  15.             toEmail = toEmail.Replace("、"",");
  16.             toEmail = toEmail.Replace("/"",");
  17.             string[] MtoEmail = toEmail.Split(',');
  18.             try
  19.             {
  20.                 for (int i = 0; i < MtoEmail.Length; i++)
  21.                 {
  22.                     if (MtoEmail[i].Trim() != "")
  23.                     {
  24.                         Mails(ESubject, MtoEmail[i], EBodyText, userName, EAddress, Esmtp, Epass);//发送邮件
  25.                     }
  26.                 }
  27.                 return true;
  28.             }
  29.             catch
  30.             {
  31.                 return false;
  32.             }
  33.         
  34.         }
  35.         public void Mails(string ESubject, string toEmail, string EBodyText, string userName, string EAddress, string Esmtp, string Epass)
  36.         {
  37.             try
  38.             {
  39.                 MailMessage m_message = new MailMessage();
  40.                 m_message.Subject = ESubject;
  41.                 m_message.SubjectEncoding = System.Text.Encoding.UTF8;
  42.                 m_message.Body = EBodyText;
  43.                 m_message.BodyEncoding = System.Text.Encoding.UTF8;
  44.                 m_message.IsBodyHtml = true;
  45.                 m_message.From = new MailAddress(EAddress);
  46.                 m_message.To.Add(new MailAddress(toEmail));
  47.                 SmtpClient smtp = new SmtpClient(Esmtp);
  48.                 smtp.UseDefaultCredentials = true;
  49.                 smtp.Credentials = new System.Net.NetworkCredential(userName, Epass);
  50.                 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
  51.                 smtp.Timeout = 10000;
  52.                 smtp.Send(m_message);
  53.             }
  54.             catch (Exception ee)
  55.             {
  56.                 throw ee;
  57.             }
  58.         }
  59.         #endregion
  60.         #region 电子邮件发送,以网页形式-----------------------------------------------------------------
  61.         /// <summary> 
  62.         /// 邮件发送类(请先对Subject主题、 url网址、toEmail收件箱赋值、EAddress邮件地址,Esmtp邮箱的smtp,Epass邮箱密码) 
  63.         /// </summary> 
  64.         /// <returns></returns> */
  65.         public bool SendMailsWeb(string ESubject, string EtoEmail, string url, string userName, string EAddress, string Esmtp, string Epass)
  66.         {
  67.             string toEmail = EtoEmail.Trim();
  68.             toEmail = toEmail.Replace(","",");
  69.             toEmail = toEmail.Replace(" "",");
  70.             toEmail = toEmail.Replace("  "",");
  71.             toEmail = toEmail.Replace("|"",");
  72.             toEmail = toEmail.Replace("//", ",");
  73.             toEmail = toEmail.Replace("、"",");
  74.             toEmail = toEmail.Replace("/"",");
  75.             string[] MtoEmail = toEmail.Split(',');
  76.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  77.             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  78.             System.IO.StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
  79.             string mailContent = sr.ReadToEnd();
  80.             try
  81.             {
  82.                 for (int i = 0; i < MtoEmail.Length; i++)
  83.                 {
  84.                     if (MtoEmail[i].Trim() != "")
  85.                     {
  86.                         Mails(ESubject, MtoEmail[i], mailContent, userName, EAddress, Esmtp, Epass);
  87.                     }
  88.                 }
  89.                 return true;
  90.             }
  91.             catch
  92.             {
  93.                 return false;
  94.             }
  95.         }
  96.         #endregion 
  97. 本文标签: .net 邮件 群发
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值