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

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

C#教程

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

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

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

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

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
namespace ConsoleApplication1
{
 /// <summary>
 /// 發(fā)送郵件類 的摘要說明
 /// </summary>
 class SendMail
 {
  #region 數(shù)據(jù)成員
  //收件人地址
  private string m_To = "";
  //發(fā)件人地址
  private string m_From = "";
  //郵件標(biāo)題
  private string m_Subject = "";
  //郵件正文
  private string m_Body = "";
  //發(fā)送服務(wù)器名或地址
  private string m_Host = "";
  //發(fā)件人用戶名
  private string m_UserName = "";
  //發(fā)件人密碼
  private string m_Password = "";
  //郵件附件
  private string m_File = "";
  #endregion
  #region 構(gòu)造函數(shù)
  /// <summary>
  /// 構(gòu)造函數(shù)重載
  /// </summary>
  /// <param name="to">收件人地址</param>
  /// <param name="from">發(fā)件人地址</param>
  /// <param name="subject">郵件標(biāo)題</param>
  /// <param name="body">郵件正文</param>
  /// <param name="host">發(fā)送郵件服務(wù)器名或地址</param>
  /// <param name="userName">發(fā)件人用戶名</param>
  /// <param name="password">發(fā)件人密碼</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 數(shù)據(jù)屬性
  //收件人地址
  public string TO
  {
   get
   {
    return m_To;
   }
   set
   {
    m_To = value;
   }
  }
  //發(fā)件人地址
  public string From
  {
   get
   {
    return m_From;
   }
   set
   {
    m_From = value;
   }
  }
  //郵件標(biāo)題
  public string Subject
  {
   get
   {
    return m_Subject;
   }
   set
   {
    m_Subject = value;
   }
  }
  //郵件正文
  public string Body
  {
   get
   {
    return m_Body;
   }
   set
   {
    m_Body = value;
   }
  }
  //服務(wù)器地址(名)
  public string Host
  {
   get
   {
    return m_Host;
   }
   set
   {
    m_Host = value;
   }
  }
  //發(fā)件人的用戶名
  public string UserName
  {
   get
   {
    return m_UserName;
   }
   set
   {
    m_UserName = value;
   }
  }
  //發(fā)件人的密碼
  public string Password
  {
   get
   {
    return m_Password;
   }
   set
   {
    m_Password = value;
   }
  }
  //郵件附件
  public string File
  {
   get
   {
    return m_File;
   }
   set
   {
    m_File = value;
   }
  }
  #endregion
  /// <summary>
  /// 發(fā)送郵件
  /// </summary>
  /// <returns>發(fā)送是否成功</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);
     //發(fā)件人地址
     MailAddress fromAddress = new MailAddress(m_From);
     //定義郵件消息
     MailMessage msg = new MailMessage(fromAddress, toAddress);
     //郵件標(biāo)題
     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);
      }
     }
     //使用簡單郵件傳輸協(xié)議來傳送郵件
     SmtpClient sendMail = new SmtpClient();
     //發(fā)送郵件的服務(wù)器名或地址
     sendMail.Host = m_Host;
     //驗證發(fā)件人的身份
     sendMail.Credentials = new NetworkCredential(m_UserName, m_Password);
     //處理待發(fā)送郵件的方法
     sendMail.DeliveryMethod = SmtpDeliveryMethod.Network;
     //發(fā)送郵件
     sendMail.Send(msg);     
    }
    return true;
   }
   catch (Exception ex)
   {
    return false;
   }
  }
 }
}

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

上一篇:C# Console利用mspaint打開圖像并保存的方法

欄    目:C#教程

下一篇:C#實例代碼之抽獎升級版可以經(jīng)表格數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫,抽獎設(shè)置,補抽

本文標(biāo)題:C#實現(xiàn)的自定義郵件發(fā)送類完整實例(支持多人多附件)

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

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

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

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

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