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

首頁 > 編程 > C# > 正文

C#實現的自定義郵件發送類完整實例(支持多人多附件)

2019-10-29 21:34:18
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了C#實現的自定義郵件發送類,具有支持多人多附件的功能,涉及C#郵件操作的相關技巧,需要的朋友可以參考下
 

本文實例講述了C#實現的自定義郵件發送類。分享給大家供大家參考,具體如下:

using System;using System.Collections.Generic;using System.Text;using System.Net;using System.Net.Mail;using System.Net.Mime;namespace ConsoleApplication1{ /// <summary> /// 發送郵件類 的摘要說明 /// </summary> class SendMail {  #region 數據成員  //收件人地址  private string m_To = "";  //發件人地址  private string m_From = "";  //郵件標題  private string m_Subject = "";  //郵件正文  private string m_Body = "";  //發送服務器名或地址  private string m_Host = "";  //發件人用戶名  private string m_UserName = "";  //發件人密碼  private string m_Password = "";  //郵件附件  private string m_File = "";  #endregion  #region 構造函數  /// <summary>  /// 構造函數重載  /// </summary>  /// <param name="to">收件人地址</param>  /// <param name="from">發件人地址</param>  /// <param name="subject">郵件標題</param>  /// <param name="body">郵件正文</param>  /// <param name="host">發送郵件服務器名或地址</param>  /// <param name="userName">發件人用戶名</param>  /// <param name="password">發件人密碼</param>  public SendMail(string to, string from, string subject, string body, string host, string userName, string password, string fileName)  {   m_To = to;   m_From = from;   m_Subject = subject;   m_Body = body;   m_Host = host;   m_UserName = userName;   m_Password = password;   m_File = fileName;  }  #endregion  #region 數據屬性  //收件人地址  public string TO  {   get   {    return m_To;   }   set   {    m_To = value;   }  }  //發件人地址  public string From  {   get   {    return m_From;   }   set   {    m_From = value;   }  }  //郵件標題  public string Subject  {   get   {    return m_Subject;   }   set   {    m_Subject = value;   }  }  //郵件正文  public string Body  {   get   {    return m_Body;   }   set   {    m_Body = value;   }  }  //服務器地址(名)  public string Host  {   get   {    return m_Host;   }   set   {    m_Host = value;   }  }  //發件人的用戶名  public string UserName  {   get   {    return m_UserName;   }   set   {    m_UserName = value;   }  }  //發件人的密碼  public string Password  {   get   {    return m_Password;   }   set   {    m_Password = value;   }  }  //郵件附件  public string File  {   get   {    return m_File;   }   set   {    m_File = value;   }  }  #endregion  /// <summary>  /// 發送郵件  /// </summary>  /// <returns>發送是否成功</returns>  public bool Send()  {   try   {    //獲取所有的收件人地址    char[] ch = { ',' };    string[] address = m_To.Split(ch);    MailAddressCollection allAddress = new MailAddressCollection();    for (int i = 0; i < address.Length; i++)    {     //收件人地址     MailAddress toAddress = new MailAddress(address[i]);     allAddress.Add(toAddress);     //發件人地址     MailAddress fromAddress = new MailAddress(m_From);     //定義郵件消息     MailMessage msg = new MailMessage(fromAddress, toAddress);     //郵件標題     msg.Subject = m_Subject;     //郵件正文     msg.Body = m_Body;     //獲取所有郵件附件     char[] cr = { ';' };     string[] file = m_File.Split(cr);     for (int n = 0; n < file.Length; n++)     {      if (file[n] != "")      {       //附件對象       Attachment data = new Attachment(file[n], MediaTypeNames.Application.Octet);       //附件資料       ContentDisposition disposition = data.ContentDisposition;       disposition.CreationDate = System.IO.File.GetCreationTime(file[n]);       disposition.ModificationDate = System.IO.File.GetLastWriteTime(file[n]);       disposition.ReadDate = System.IO.File.GetLastAccessTime(file[n]);       //加入郵件附件       msg.Attachments.Add(data);      }     }     //使用簡單郵件傳輸協議來傳送郵件     SmtpClient sendMail = new SmtpClient();     //發送郵件的服務器名或地址     sendMail.Host = m_Host;     //驗證發件人的身份     sendMail.Credentials = new NetworkCredential(m_UserName, m_Password);     //處理待發送郵件的方法     sendMail.DeliveryMethod = SmtpDeliveryMethod.Network;     //發送郵件     sendMail.Send(msg);         }    return true;   }   catch (Exception ex)   {    return false;   }  } }}

希望本文所述對大家C#程序設計有所幫助。



注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 颍上县| 辛集市| 巴里| 临沭县| 花垣县| 郎溪县| 陕西省| 石景山区| 繁峙县| 屏东市| 三原县| 五华县| 万盛区| 芦溪县| 济南市| 保亭| 屯留县| 临潭县| 光泽县| 垦利县| 奈曼旗| 新竹县| 延寿县| 济源市| 霍山县| 汉寿县| 罗山县| 北票市| 阜宁县| 鄂托克旗| 山东| 法库县| 丹江口市| 探索| 沧州市| 文昌市| 邵武市| 九江市| 东光县| 平和县| 东至县|