- #region 电子邮件发送-----------------------------------------------------------------------------
- /// <summary>
- /// 邮件发送类(请先对Subject主题、 BodyText正文、toEmail收件箱赋值、EAddress邮件地址,Esmtp邮箱的smtp,Epass邮箱密码)
- /// </summary>
- /// <returns></returns> */
- public bool SendMails(string ESubject, string EtoEmail, string EBodyText,string userName, string EAddress, string Esmtp, string Epass)
- {
- //把群发的邮件存入数组中
- string toEmail = EtoEmail.Trim();
- toEmail = toEmail.Replace(",", ",");
- toEmail = toEmail.Replace(" ", ",");
- toEmail = toEmail.Replace(" ", ",");
- toEmail = toEmail.Replace("|", ",");
- toEmail = toEmail.Replace("//", ",");
- toEmail = toEmail.Replace("、", ",");
- toEmail = toEmail.Replace("/", ",");
- string[] MtoEmail = toEmail.Split(',');
- try
- {
- for (int i = 0; i < MtoEmail.Length; i++)
- {
- if (MtoEmail[i].Trim() != "")
- {
- Mails(ESubject, MtoEmail[i], EBodyText, userName, EAddress, Esmtp, Epass);//发送邮件
- }
- }
- return true;
- }
- catch
- {
- return false;
- }
- }
- public void Mails(string ESubject, string toEmail, string EBodyText, string userName, string EAddress, string Esmtp, string Epass)
- {
- try
- {
- MailMessage m_message = new MailMessage();
- m_message.Subject = ESubject;
- m_message.SubjectEncoding = System.Text.Encoding.UTF8;
- m_message.Body = EBodyText;
- m_message.BodyEncoding = System.Text.Encoding.UTF8;
- m_message.IsBodyHtml = true;
- m_message.From = new MailAddress(EAddress);
- m_message.To.Add(new MailAddress(toEmail));
- SmtpClient smtp = new SmtpClient(Esmtp);
- smtp.UseDefaultCredentials = true;
- smtp.Credentials = new System.Net.NetworkCredential(userName, Epass);
- smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
- smtp.Timeout = 10000;
- smtp.Send(m_message);
- }
- catch (Exception ee)
- {
- throw ee;
- }
- }
- #endregion
- #region 电子邮件发送,以网页形式-----------------------------------------------------------------
- /// <summary>
- /// 邮件发送类(请先对Subject主题、 url网址、toEmail收件箱赋值、EAddress邮件地址,Esmtp邮箱的smtp,Epass邮箱密码)
- /// </summary>
- /// <returns></returns> */
- public bool SendMailsWeb(string ESubject, string EtoEmail, string url, string userName, string EAddress, string Esmtp, string Epass)
- {
- string toEmail = EtoEmail.Trim();
- toEmail = toEmail.Replace(",", ",");
- toEmail = toEmail.Replace(" ", ",");
- toEmail = toEmail.Replace(" ", ",");
- toEmail = toEmail.Replace("|", ",");
- toEmail = toEmail.Replace("//", ",");
- toEmail = toEmail.Replace("、", ",");
- toEmail = toEmail.Replace("/", ",");
- string[] MtoEmail = toEmail.Split(',');
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- System.IO.StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
- string mailContent = sr.ReadToEnd();
- try
- {
- for (int i = 0; i < MtoEmail.Length; i++)
- {
- if (MtoEmail[i].Trim() != "")
- {
- Mails(ESubject, MtoEmail[i], mailContent, userName, EAddress, Esmtp, Epass);
- }
- }
- return true;
- }
- catch
- {
- return false;
- }
- }
- #endregion
- 本文标签: .net 邮件 群发
.net学习总结(9)邮件群发
最新推荐文章于 2019-03-28 23:27:29 发布