2010年2月4日 星期四

實作ASP.NET MVC SendMail功能


今天研究在ASP.NET MVC於Action作完後,發mail到指定之User的方法,
在網路上Google一下,參考此篇文章:如何發送內嵌圖片的 E-mail ( Inline Attachment ) ,將其實作出來。

1.實作一個SendMail類別,程式碼如下:
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net.Mail;

using System.IO;



namespace IPRO.Ext

{

    public class SendMail

    {

        #region private fields

        private string _fromName;

        private string _fromAddress;

        private string _toName;

        private string _toAddress;

        private string _smtpServer;

        private int _smtpPort;

        private string _subject;

        private string _body;

        private string[] _filePath;

        private string _mailEncoding = "utf-8";

        #endregion



        #region construtor

        public SendMail(string fromName, string fromAddress, string toName,

                        string toAddress, string subject, string body,

                        string[] filePath, string smtpServer, int smtpPort)

        {

            this._fromName = fromName;

            this._fromAddress = fromAddress;

            this._toName = toName;

            this._toAddress = toAddress;

            this._subject = subject;

            this._body = body;

            this._filePath = filePath;

            this._smtpServer = smtpServer;

            this._smtpPort = smtpPort;

        }

        #endregion



        #region public method

        public void Send()

        {

            MailAddress from;

            MailAddress to;

            if (string.IsNullOrEmpty(_fromName))

            {

                from = new MailAddress(_fromAddress);

            }

            else

            {

                from = new MailAddress(_fromAddress, _fromName, Encoding.GetEncoding(_mailEncoding));

            }



            if (string.IsNullOrEmpty(_toName))

            {

                to = new MailAddress(_toAddress);

            }

            else

            {

                to = new MailAddress(_toAddress, _toName, Encoding.GetEncoding(_mailEncoding));

            }



            MailMessage message = new MailMessage(from, to);

            if(string.IsNullOrEmpty(_subject))

            {

                _subject = "郵件主旨";

            }

            message.Subject = _subject;

            message.SubjectEncoding = Encoding.GetEncoding(_mailEncoding);



            if(string.IsNullOrEmpty(_body))

            {

                _body = "郵件內容";

            }

            message.Body = _body;

            message.BodyEncoding = Encoding.GetEncoding(_mailEncoding);

            message.IsBodyHtml = true;

            message.Priority = MailPriority.High;



            //設定附件檔案(Attachment)

            if (_filePath.Length > 0)

            {

                for (int i = 0; i < _filePath.Length; i++)

                {

                    Attachment attachment = new Attachment(_filePath[i]);

                    attachment.Name = Path.GetFileName(_filePath[i]);

                    attachment.NameEncoding = Encoding.GetEncoding(_mailEncoding);

                    attachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

                    // 設定該附件為一個內嵌附件(Inline Attachment)

                    attachment.ContentDisposition.Inline = true;

                    attachment.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;

                    message.Attachments.Add(attachment);

                }

            }



            if (_smtpPort == 0)

            {

                _smtpPort = 25;

            }



            if (string.IsNullOrEmpty(_smtpServer))

            {

                _smtpServer = "";

            }



            SmtpClient smtpClient = new SmtpClient(_smtpServer, _smtpPort);

            try

            {

                smtpClient.Send(message);

                System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString() + " 寄信成功!!!");

            }

            catch

            {

                System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString() + " 寄信失敗!!!");

            }

        }

        #endregion

    }

}


2.於Action中加入SendMail功能:
                SendMail mail = new SendMail("haha", "haha@gmail.com",
                                             "haha", "haha@yahoo.com.tw", "主旨",
                                             "內容", new string[] { },
                                             "smtp.gmail.com", 25);
                mail.Send();

沒有留言:

張貼留言