asp.net如何实现的群发邮件功能详解

黄舟
Release: 2017-06-04 09:50:37
Original
1292 people have browsed it

这篇文章主要介绍了asp.net实现的群发邮件功能,结合具体实例形式分析了asp.net基于SMTP服务群发QQ邮件的相关操作技巧与注意事项,需要的朋友可以参考下

本文实例讲述了asp.net实现的群发邮件功能。分享给大家供大家参考,具体如下:

前段时间在帮老师开发的网站中需要用到一个群发邮件的功能,而自己之前学习cms系统的时候用的we7的群发邮件功能也有一些问题,于是乎便自己去网上查了一下资料,自己总结了一下,并且封装成了一个类,亲测有用,支持附件,希望对大家也有些帮助,我测试时用的是qq邮箱,成功的前提是要在邮箱设置中打开smtp服务功能,如qq邮箱设置里面的账户选项卡里面把需要的功能勾上:

其他邮箱的话原理一样~~~~~~

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
namespace MailSend
{
  public class SendEmail
  {
    /// 
    /// 群发邮件函数
    /// 
    /// 发送的主题
    /// 发送的内容
    /// 收件人列表,通过|来分割开来,可以自己定义
    /// 发送附件的地址,获取了路径后上传
    public static void Send(string subject, string body, string email_list, string File_Path)
    {
      string MailUser = "[email protected]";//我测试的是qq邮箱,其他邮箱一样的道理
      string MailPwd = "你的密码";//邮箱密码
      string MailName = "测试";
      string MailHost = "smtp.exmail.qq.com";//根据自己选择的邮箱来查询smtp的地址
      MailAddress from = new MailAddress(MailUser, MailName); //邮件的发件人
      MailMessage mail = new MailMessage();
      //设置邮件的标题
      mail.Subject = subject;
      //设置邮件的发件人
      //Pass:如果不想显示自己的邮箱地址,这里可以填符合mail格式的任意名称,真正发mail的用户不在这里设定,这个仅仅只做显示用
      mail.From = from;
      //设置邮件的收件人
      string address = "";
      //传入多个邮箱,用“|”分割开,可以自己自定义,再通过mail.To.Add()添加到列表
      string[] email = email_list.Split('|');
      foreach (string name in email)
      {
        if (name != string.Empty)
        {
          address = name;
          mail.To.Add(new MailAddress(address));
        }
      }
      //设置邮件的抄送收件人
      //这个就简单多了,如果不想快点下岗重要文件还是CC一份给领导比较好
      //mail.CC.Add(new MailAddress("[email protected]", "尊敬的领导");
      //设置邮件的内容
      mail.Body = body;
      //设置邮件的格式
      mail.BodyEncoding = System.Text.Encoding.UTF8;
      mail.IsBodyHtml = true;
      //设置邮件的发送级别
      mail.Priority = MailPriority.Normal;
      //设置邮件的附件,将在客户端选择的附件先上传到服务器保存一个,然后加入到mail中
      if (File_Path != "")
      {
        mail.Attachments.Add(new Attachment(File_Path));
        mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
      }
      SmtpClient client = new SmtpClient();
      //设置用于 SMTP 事务的主机的名称,填IP地址也可以了
      client.Host = MailHost;
      //设置用于 SMTP 事务的端口,默认的是 25
      client.Port = 25;
      client.UseDefaultCredentials = false;
      //这里才是真正的邮箱登陆名和密码, 我的用户名为 MailUser ,我的密码是 MailPwd
      client.Credentials = new System.Net.NetworkCredential(MailUser, MailPwd);
      client.DeliveryMethod = SmtpDeliveryMethod.Network;
      ////如果发送失败,SMTP 服务器将发送 失败邮件告诉我
      mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
      //都定义完了,正式发送了,很是简单吧!
      client.Send(mail);
    }
  }
}
Copy after login

The above is the detailed content of asp.net如何实现的群发邮件功能详解. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!