2010/1/18

ASP.NET 的電子郵件傳輸工具程式

以下是我已經寫好的 Email 傳送程式, 稍為改一下就可以使用了:

using System;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Configuration;

public class Mails
{
    private string _sendFrom = "YourId@YourDomain";
    private string _sendBy = "系統管理員";

    /// 

    /// 指定 SMTP Server; 預設值為 msa.hinet.net
    /// 

    public string Host = "msa.hinet.net";

    /// 

    /// 指定 SMTP Server Port; 預設值為 25
    /// 

    public int HostPort = 25;

    /// 

    /// 指定寄件人的 Email 位址
    /// 

    public string SendFrom
    {
        get
        {
            return _sendFrom;
        }
        set
        {
            _sendFrom = value;
        }
    }

    /// 

    /// 指定寄件人的名字
    /// 

    public string SendBy
    {
        get
        {
            return _sendBy;
        }
        set
        {
            _sendBy = value;
        }
    }

    /// 

    /// 建立寄件人 MailAddress 物件
    /// 

    private MailAddress From
    {
        get
        {
            MailAddress _from = new MailAddress(SendFrom, SendBy);
            return _from;
        }
    }

    /// 

    /// 指定收件人地址; 可以同時指定多個 Email, 以逗號區隔
    /// 

    public string To;

    /// 指定 CC 地址; 由於可以同時指定多個 Email, 以逗號區隔
    /// 
    public string Cc;

    /// 指定 BCC 地址; 由於可以同時指定多個 Email, 以逗號區隔
    /// 
    public string Bcc;

    /// 

    /// 指定該郵件的標題
    /// 

    public string Subject = null;

    /// 

    /// 指定郵件標題的編碼方式, 預設值為 UTF8
    /// 

    public Encoding SubjectEncoding = Encoding.UTF8;

    private string _body = null;

    /// 

    /// 指定該郵件的內容
    /// 

    public string Body
    {
        get
        {
            return _body;
        }
        set
        {
            _body = (IsBodyHtml) ?
                string.Format("<div style='{0}'>{1}</div>", Style, value) :
                value;
        }
    }

    /// <summary>
    /// 指定郵件內容的編碼方式, 預設值為 UTF8
    /// </summary>
    public Encoding BodyEncoding = Encoding.UTF8;

    /// <summary>
    /// 指定該郵件是否為 HTML 格式或純文字格式; 預設值為 true
    /// </summary>
    public bool IsBodyHtml = true;

    /// <summary>
    /// 指定整體信件的樣式; 只有當信件格式為 HTML 時有效
    /// </summary>
    public string Style = "font-family:'微軟正黑體, Verdana,Arial'; width: 90%; font-size: 0.9em;";

    private MailMessage mail;

    public Mails() // Constructor
    {
        mail = new MailMessage();
    }

    /// <summary>
    /// Send mail directly with static data
    /// </summary>
    public static void QuickSend(string _Subject, string _Message, string _To, string _Cc = null, string _Bcc = null)
    {
        Mails m = new Mails();
        m.To = _To;
        m.Cc = _Cc;
        m.Bcc = _Bcc;
        m.Subject = _Subject;
        m.Body = _Message;
        m.Send();
    }

    /// <summary>
    /// 寄出信件
    /// </summary>
    public void Send()
    {
        if (To == null) // 防呆
            return;
        mail.From = this.From;
        string a = mail.From.Address;
        string u = mail.From.User;
        foreach (string to in this.To.Split(','))
            if (!string.IsNullOrEmpty(to))
                mail.To.Add(to);
        foreach (string cc in this.Cc.Split(','))
            if (!string.IsNullOrEmpty(cc))
                mail.To.Add(cc);
        foreach (string bcc in this.Bcc.Split(','))
            if (!string.IsNullOrEmpty(bcc))
                mail.To.Add(bcc);
        mail.IsBodyHtml = this.IsBodyHtml;
        mail.Subject = this.Subject;
        mail.SubjectEncoding = this.SubjectEncoding;
        mail.Body = this.Body;
        mail.BodyEncoding = this.BodyEncoding;

        SmtpClient client = new SmtpClient(this.Host, this.HostPort);
        client.Send(mail);
    }
}

要使用這個類別, 最簡單的方法如下:

string subject = txtMailSubject.Text.Trim();
string message = txtMailMessage.Text.Trim();
string to = txtMailTo.Text.Trim();
string cc = txtMailCc.Text.Trim();
string bcc = txtMailBcc.Text.Trim();
Mails.QuickSend(subject, message, to, cc, bcc);

在程式中, Mails.QuickSend 是一個靜態方法, 把相關字串填上去即可進行郵件傳送; 凡是未指定的欄位或屬性, 都採用預設值。如果你不想使用預設值, 則可以改用 Send() 方法, 但是可以自行修改各欄位或屬性的值。

沒有留言:

張貼留言