SmtpMail.cs

出自 ProgWiki
前往: 導覽搜尋

SmtpMail,使用 System.Net.Mail.SmtpClient 去寄email

  • .NET Framework 2.0 版以上適用

SmtpMail.cs

using System;
using System.Net.Mail;
using System.Net.Mime;
 
//連線到SMTP Server的相關設定, 定義在 web.config 的 
/*
<system.net>
<mailSettings>
    <smtp deliveryMethod="Network" from="寄件者的email">
        <network defaultCredentials="false" host="SMTP主機的IP" port="25" userName="帳號" password="密碼" />
    </smtp>
</mailSettings>
</system.net>
*/
 
/// <summary>
/// SmtpMail 的摘要描述
/// </summary>
public class SmtpMail : IDisposable
{
    public SmtpMail()
    {
 
    }
 
    private SmtpClient smtpClient = new SmtpClient();
    private MailMessage message = new MailMessage();
    private string errorMessage = "";
 
    #region 寄件者
    public void From(string address)
    {
         message.From = new MailAddress(address);
    }
 
    public void From(string address, string displayName)
    {
         message.From = new MailAddress(address, displayName);
    }
 
    public void Sender(string address)
    {
         message.Sender = new MailAddress(address);
    }
 
    public void Sender(string address, string displayName)
    {
        message.Sender = new MailAddress(address, displayName);
    }
    #endregion
 
    #region 收件者
    public void To(string address)
    {
         message.To.Add(address);
    }
 
    public void To(string address, string displayName)
    {
         message.To.Add(new MailAddress(address, displayName));
    }
    #endregion
 
    #region 副本 (CC)
    public void CC(string address)
    {
         message.CC.Add(address);
    }
 
    public void CC(string address, string displayName)
    {
         message.CC.Add(new MailAddress(address, displayName));
    }
    #endregion
 
    #region 密件副本 (BCC)
    public void Bcc(string address)
    {
         message.Bcc.Add(address);
    }
 
    public void Bcc(string address, string displayName)
    {
         message.Bcc.Add(new MailAddress(address, displayName));
    }
    #endregion
 
    #region 郵件訊息回覆時的地址
    public void ReplyTo(string address)
    {
        message.ReplyToList.Add(address);
    }
 
    public void ReplyTo(string address, string displayName)
    {
        message.ReplyToList.Add(new MailAddress(address, displayName));
    }
    #endregion
 
    #region 主旨
    public string Subject
    {
        get {
            return message.Subject;
        }
        set {
             message.Subject = value;
        }
    }
    #endregion
 
    #region 訊息主體
    public string Body
    {
        get {
            return message.Body;
        }
        set {
             message.Body = value;
        }
    }
    #endregion
 
    #region 郵件訊息主體是否採用 Html 格式
    public bool IsBodyHtml
    {
        get {
            return message.IsBodyHtml;
        }
        set {
             message.IsBodyHtml = value;
        }
    }
    #endregion
 
    #region 優先權
    public MailPriority Priority
    {
        get {
            return message.Priority;
        }
        set {
            message.Priority = value;
        }
    }
    #endregion
 
    #region 檔案附件
    public void Attachments(string file)
    {
        Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
        // Add time stamp information for the file.
        ContentDisposition disposition = data.ContentDisposition;
        disposition.CreationDate = System.IO.File.GetCreationTime(file);
        disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
        disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
        // Add the file attachment to this e-mail message.
        message.Attachments.Add(data);
    }
 
    public void Attachments(System.IO.Stream contentStream,	ContentType contentType, string fileName)
    {
        // Attach the log file stream to the e-mail message.
        Attachment data = new Attachment(contentStream, contentType);
        ContentDisposition disposition = data.ContentDisposition;
        disposition.FileName = fileName;
        // Add the attachment to the message.
        message.Attachments.Add(data);
    }
    #endregion
 
    public bool Send()
    { 
        try
        {
             smtpClient.Send(message);
             return true;
        }
        catch(Exception ex)
        {
            errorMessage = ex.Message;
            return false;
        }
    }
 
    public string GetLastErrorMessage()
    {
        return errorMessage;
    }
 
    public void Dispose()
    {
        this.Dispose(true);
    }
 
    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            this.message.Dispose();
            this.smtpClient.Dispose();
        }
    }
 
}


用法

using (SmtpMail mail = new SmtpMail())
{
    mail.From("寄件者的email");
    mail.To("收件者的email");
    mail.IsBodyHtml = false;
 
    mail.Subject = "email標題";
    mail.Body = "email內容";
    bool isOK = mail.Send();
}