本文實例講述了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#程序設計有所幫助。
新聞熱點
疑難解答