雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

C#簡單郵件群發(fā)通用類

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊:

本文實例為大家介紹了C#郵件群發(fā)通用類,供大家參考,具體內(nèi)容如下

public static class Email
  {
    /// <summary>
    /// 發(fā)件人
    /// </summary>
    public static string mailFrom { get; set; }

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

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

    /// <summary>
    /// 標題
    /// </summary>
    public static string mailSubject { get; set; }

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

    /// <summary>
    /// 發(fā)件人密碼
    /// </summary>
    public static string mailPwd { get; set; }

    /// <summary>
    /// SMTP郵件服務(wù)器
    /// </summary>
    public static string host { get; set; }  

    /// <summary>
    /// 郵件服務(wù)器端口
    /// </summary>
    public static int port { get; set; }

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

    /// <summary>
    /// 附件
    /// </summary>
    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());
        }
      }
      //發(fā)件人地址
      myMail.From = maddr;

      //電子郵件的標題
      myMail.Subject = mailSubject;

      //電子郵件的主題內(nèi)容使用的編碼
      myMail.SubjectEncoding = Encoding.UTF8;

      //電子郵件正文
      myMail.Body = mailBody;

      //電子郵件正文的編碼
      myMail.BodyEncoding = Encoding.Default;

      //電子郵件優(yōu)先級
      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();

      //指定發(fā)件人的郵件地址和密碼以驗證發(fā)件人身份
      client.Credentials = new NetworkCredential(mailFrom, mailPwd);

      //設(shè)置SMTP郵件服務(wù)器
      //client.Host = "smtp." + myMail.From.Host;
      client.Host = host;

      //SMTP郵件服務(wù)器端口
      client.Port = port;

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

      try
      {
        //將郵件發(fā)送到SMTP郵件服務(wù)器
        client.Send(myMail);
        return true;
      }
      catch (SmtpException ex)
      {
        string msg = ex.Message;
        return false;
      }

    }

希望本文所述對大家學(xué)習(xí)C#程序設(shè)計有所幫助。

上一篇:C#實現(xiàn)無限級聯(lián)下拉列表框

欄    目:C#教程

下一篇:C#程序中使用LINQ to XML來查詢XML格式數(shù)據(jù)的實例

本文標題:C#簡單郵件群發(fā)通用類

本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6643.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有