国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

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

2019-10-29 21:30:53
字體:
供稿:網(wǎng)友
這篇文章主要為大家分享了簡(jiǎn)單的C#郵件群發(fā)通用類,代碼簡(jiǎn)單實(shí)用,感興趣的小伙伴們可以參考一下
 

本文實(shí)例為大家介紹了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>    /// 標(biāo)題    /// </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實(shí)例      MailAddress maddr = new MailAddress(mailFrom);      //初始化MailMessage實(shí)例      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;      //電子郵件的標(biāo)題      myMail.Subject = mailSubject;      //電子郵件的主題內(nèi)容使用的編碼      myMail.SubjectEncoding = Encoding.UTF8;      //電子郵件正文      myMail.Body = mailBody;      //電子郵件正文的編碼      myMail.BodyEncoding = Encoding.Default;      //電子郵件優(yōu)先級(jí)      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("在添加附件時(shí)有錯(cuò)誤:" + err.Message);      }      SmtpClient client = new SmtpClient();      //指定發(fā)件人的郵件地址和密碼以驗(yàn)證發(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;      }    }

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



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 连城县| 周口市| 沙河市| 扶沟县| 霍邱县| 绥化市| 会理县| 盐亭县| 孟连| 安溪县| 陆河县| 东乡族自治县| 琼结县| 合山市| 上犹县| 丹阳市| 汾阳市| 黄梅县| 定南县| 喜德县| 读书| 阿拉善右旗| 专栏| 台东县| 五原县| 青岛市| 长垣县| 喀什市| 永年县| 赤峰市| 蓬莱市| 利川市| 瓦房店市| 溧水县| 买车| 周口市| 潮州市| 出国| 外汇| 宁夏| 巧家县|