C#利用System.Net發送郵件(帶 抄送、密送、附件、html格式的郵件)

C#利用System.Net發送郵件(帶 抄送、密送、附件、html格式的郵件)

C#利用System.Net發送郵件(帶 抄送、密送、附件、html格式的郵件)

資料來源:http://www.cnblogs.com/lichunyang/p/System_Net_Send_Email.html

/**************************************
 * <copyright file=”SendEail.cs”>
 * Copyright(c)2014 LeeStudio All rights reserved.
 * CLR版本:4.0.30319.17929
 * 開發團隊:欺心伴月個人工作室
 * 技術博客:http://www.cnblogs.com/lichunyang/
 * 所屬工程:LeeStudio.Basic
 * 創 建 者:Frank Lee
 * 修改時間:2014-02-20 11:20
 * </copyright>
**************************************/

using System;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;

namespace LeeStudio.Basic
{
    /// <summary>
    /// 郵件發送類
    /// </summary>
    public class SendEmail
    {
        private MailMessage mailMessage = new MailMessage(); //產生實體一個郵件類

        #region 構造函數
        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name=”toAddresses”>收件人位址(多個以,號分開)</param>
        /// <param name=”fromAddress”>寄件者地址</param>
        /// <param name=”title”>主題</param>
        /// <param name=”body”>正文</param>
        public SendEmail(string toAddresses, string fromAddress, string title, string body)
            : this(toAddresses, fromAddress, “”, “”, title, body, false)
        {
        }

        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name=”toAddress”>收件人地址</param>
        /// <param name=”fromAddress”>寄件者地址</param>
        /// <param name=”toName”>收件人名字</param>
        /// <param name=”fromName”>寄件者姓名</param>
        /// <param name=”title”>主題</param>
        /// <param name=”body”>正文</param>
        /// <param name=”isBodyHtml”>正文是否為html格式</param>
        public SendEmail(string toAddress, string fromAddress, string toName, string fromName, string title, string body, bool isBodyHtml)
        {
            mailMessage.From = new MailAddress(fromAddress, fromName, Encoding.GetEncoding(936));
            if (toName.Equals(“”))
                mailMessage.To.Add(toAddress);
            else
                mailMessage.To.Add(new MailAddress(toAddress, toName, Encoding.GetEncoding(936)));

            mailMessage.Subject = title;
            mailMessage.SubjectEncoding = Encoding.GetEncoding(936);

            mailMessage.Body = body;
            mailMessage.IsBodyHtml = isBodyHtml;
            mailMessage.BodyEncoding = Encoding.GetEncoding(936);
        }
        #endregion

        /// <summary>
        /// 設置SMTP,並且將郵件發送出去
        /// 所有參數都設置完成後再調用該方法
        /// </summary>
        /// <param name=”password”>寄件者密碼</param>
        /// <param name=”smtpHost”>SMTP伺服器地址</param>
        public void SetSmtp(string password, string smtpHost)
        {
            SetSmtp(mailMessage.From.Address, password, smtpHost, 25, false, MailPriority.Normal);
        }

        /// <summary>
        /// 設置SMTP,並且將郵件發送出去
        /// 所有參數都設置完成後再調用該方法
        /// </summary>
        /// <param name=”address”>寄件者位址(必須為真實有效的email位址)</param>
        /// <param name=”password”>寄件者密碼</param>
        /// <param name=”smtpHost”>SMTP伺服器地址</param>
        /// <param name=”smtpPort”>SMTP伺服器的埠</param>
        /// <param name=”isEnableSsl”>SMTP伺服器是否啟用SSL加密</param>
        /// <param name=”priority”>郵件的優先順序</param>
        public void SetSmtp(string address, string password, string smtpHost, int smtpPort, bool isEnableSsl, MailPriority priority)
        {
            SmtpClient smtp = new SmtpClient();
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(address, password);
            smtp.Host = smtpHost;
            smtp.Port = smtpPort;
            smtp.EnableSsl = isEnableSsl;

            mailMessage.Priority = priority;
            smtp.Send(mailMessage); //發送郵件
        }

        #region //設置郵寄地址
        /// <summary>
        /// 設置更多收件人
        /// </summary>
        /// <param name=”toAddresses”>收件人地址</param>
        public void SetMoreToAddress(string toAddresses)
        {
            mailMessage.To.Add(toAddresses);
        }

        /// <summary>
        /// 設置更多收件人
        /// </summary>
        /// <param name=”toAddresses”>收件人地址</param>
        /// <param name=”toName”>收件人名字</param>
        public void SetMoreToAddress(string toAddress, string toName)
        {
            mailMessage.To.Add(new MailAddress(toAddress, toName, Encoding.GetEncoding(936)));
        }

        /// <summary>
        /// 設置抄送者(多個以,號分開)
        /// </summary>
        /// <param name=”ccAddresses”>抄送者地址</param>
        public void SetCarbonCopyFor(string ccAddresses)
        {
            mailMessage.CC.Add(ccAddresses);
        }

        /// <summary>
        /// 設置抄送者
        /// </summary>
        /// <param name=”ccAddress”>抄送者地址</param>
        /// <param name=”ccName”>抄送者名字</param>
        public void SetCarbonCopyFor(string ccAddress, string ccName)
        {
            mailMessage.Bcc.Add(new MailAddress(ccAddress, ccName, Encoding.GetEncoding(936)));
        }

        /// <summary>
        /// 設置密送者(多個以,號分開)
        /// </summary>
        /// <param name=”bccAddresses”>密送者</param>
        public void SetBlindCarbonCopyFor(string bccAddresses)
        {
            mailMessage.Bcc.Add(bccAddresses);
        }

        /// <summary>
        /// 設置密送者
        /// </summary>
        /// <param name=”bccAddress”>密送者</param>
        /// <param name=”bccName”>密送者名字</param>
        public void SetBlindCarbonCopyFor(string bccAddress, string bccName)
        {
            mailMessage.Bcc.Add(new MailAddress(bccAddress, bccName, Encoding.GetEncoding(936)));
        }
        #endregion

        #region 添加附件
        /// <summary>
        /// 添加附件(自動識別檔案類型)
        /// </summary>
        /// <param name=”fileName”>單個檔的路徑</param>
        public void Attachments(string fileName)
        {
            mailMessage.Attachments.Add(new Attachment(fileName));
        }

        /// <summary>
        /// 添加附件(預設為富文本RTF格式)
        /// </summary>
        /// <param name=”fileName”>單個檔的路徑</param>
        public void AttachmentsForRTF(string fileName)
        {
            mailMessage.Attachments.Add(new Attachment(fileName, MediaTypeNames.Application.Rtf));
        }
        #endregion
    }
}

調用是按照需求,自由設置參數。

using LeeStudio.Basic;

private void SendEmail()
{
    #region 最簡設置
    //SendEmail se = new SendEmail(“*****@****.com”, “****@163.com”, “title”, “body”);
    //se.SetSmtp(“password”, “smtp.163.com”); //必須在所有參數設置完後調用此方法
    #endregion

    #region 完全設置
    //string toAddresses = “***@**.**,***@**.**”;
    //string toNames = “frank,lee”;

    //SendEmail se = new SendEmail(“to@**.**”, “from@**.**”, “toname”, “fromname”,”title”, “body”, false);
    //se.SetMoreToAddress(“to@**.**”, “toName”);
    //se.SetCarbonCopyFor(“cc@**.**”);
    //se.SetBlindCarbonCopyFor(“bcc@**.**”);

    //string filePath = Request.PhysicalApplicationPath + “/file/SendEmail.txt”;
    //se.Attachments(filePath);
    //filePath = Request.PhysicalApplicationPath + “/file/RTF文件.docx”;
    //se.AttachmentsForRTF(filePath);
    //se.SetSmtp(“email”, “password”, “smtp”, 25, true, System.Net.Mail.MailPriority.Normal); //必須在所有參數設置完後調用此方法
    #endregion
}

遺留問題:

當有多個收件人/抄送者/密送者時,如何快速處理?之前的做法是讓使用者輸入字串,再將字串一一對應匹配,代碼如下:

/// <summary>
/// 將郵寄地址和名稱加入到鋸齒陣列中
/// </summary>
/// <param name=”addrs”>郵寄地址(多個以,號分開)</param>
/// <param name=”names”>名稱(多個以,號分開)</param>
/// <returns>位址和名稱陣列</returns>
private string[][] SetNameList(string addrs, string names)
{
    string[][] result = new string[2][];

    string[] addr = addrs.Split(‘,’);
    string[] name = names.Split(‘,’);
    result[0] = new string[addr.Length];
    result[1] = new string[name.Length];

    for (int i = 0; i < addr.Length; i++)
        result[0][i] = addr[i];
    for (int i = 0; i < name.Length; i++)
        result[1][i] = name[i];

    return result;
}

首先這個方法就很奇葩,其次,只要用戶輸入的個數不對應,又要做一堆的異常處理……

有好的方法望不吝賜教。
 

 

 

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *