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

首頁 > 編程 > C# > 正文

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

2020-01-24 01:20:03
字體:
來源:轉載
供稿:網友

本文實例講述了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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 曲沃县| 浙江省| 青田县| 固阳县| 临清市| 黑水县| 舞阳县| 平乡县| 佳木斯市| 伊金霍洛旗| 清原| 错那县| 蕉岭县| 什邡市| 旬阳县| 蚌埠市| 高淳县| 读书| 龙州县| 舟曲县| 镇平县| 惠东县| 两当县| 桐城市| 天长市| 南投县| 定边县| 平塘县| 菏泽市| 方山县| 徐州市| 黎平县| 仁寿县| 古丈县| 曲阳县| 尼勒克县| 阳山县| 新巴尔虎左旗| 陆河县| 滦平县| 嘉鱼县|