栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#简单邮件群发通用类

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#简单邮件群发通用类

本文实例为大家介绍了C#邮件群发通用类,供大家参考,具体内容如下

public static class Email
  {
    /// 
    /// 发件人
    /// 
    public static string mailFrom { get; set; }

    /// 
    /// 收件人
    /// 
    public static string[] mailToArray { get; set; }

    /// 
    /// 抄送
    /// 
    public static string[] mailCcArray { get; set; }

    /// 
    /// 标题
    /// 
    public static string mailSubject { get; set; }

    /// 
    /// 正文
    /// 
    public static string mailBody { get; set; }

    /// 
    /// 发件人密码
    /// 
    public static string mailPwd { get; set; }

    /// 
    /// SMTP邮件服务器
    /// 
    public static string host { get; set; }  

    /// 
    /// 邮件服务器端口
    /// 
    public static int port { get; set; }

    /// 
    /// 正文是否是html格式
    /// 
    public static bool isbodyHtml { get; set; }

    /// 
    /// 附件
    /// 
    public static string[] attachmentsPath { get; set; }

    public static bool Send()
    {
      //使用指定的邮件地址初始化MailAddress实例
      MailAddress maddr = new MailAddress(mailFrom);

      //初始化MailMessage实例
      MailMessage myMail = new MailMessage();

      //向收件人地址集合添加邮件地址
      if (mailToArray != null)
      {
 for (int i = 0; i < mailToArray.Length; i++)
 {
   myMail.To.Add(mailToArray[i].ToString());
 }
      }

      //向抄送收件人地址集合添加邮件地址
      if (mailCcArray != null)
      {
 for (int i = 0; i < mailCcArray.Length; i++)
 {
   myMail.CC.Add(mailCcArray[i].ToString());
 }
      }
      //发件人地址
      myMail.From = maddr;

      //电子邮件的标题
      myMail.Subject = mailSubject;

      //电子邮件的主题内容使用的编码
      myMail.SubjectEncoding = Encoding.UTF8;

      //电子邮件正文
      myMail.Body = mailBody;

      //电子邮件正文的编码
      myMail.BodyEncoding = Encoding.Default;

      //电子邮件优先级
      myMail.Priority = MailPriority.High;

      //电子邮件不是html格式
      myMail.IsBodyHtml = isbodyHtml;

      //在有附件的情况下添加附件
      try
      {
 if (attachmentsPath != null && attachmentsPath.Length > 0)
 {
   Attachment attachFile = null;
   foreach (string path in attachmentsPath)
   {
     attachFile = new Attachment(path);
     myMail.Attachments.Add(attachFile);
   }
 }
      }
      catch (Exception err)
      {
 throw new Exception("在添加附件时有错误:" + err.Message);
      }

      SmtpClient client = new SmtpClient();

      //指定发件人的邮件地址和密码以验证发件人身份
      client.Credentials = new NetworkCredential(mailFrom, mailPwd);

      //设置SMTP邮件服务器
      //client.Host = "smtp." + myMail.From.Host;
      client.Host = host;

      //SMTP邮件服务器端口
      client.Port = port;

      //是否使用安全连接
      //client.EnableSsl = true;

      try
      {
 //将邮件发送到SMTP邮件服务器
 client.Send(myMail);
 return true;
      }
      catch (SmtpException ex)
      {
 string msg = ex.Message;
 return false;
      }

    }

希望本文所述对大家学习C#程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/124396.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号