첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

▶ WebMailManager.cs

using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;

/// <summary>
/// 웹 메일 관리자
/// </summary>
public class WebMailManager
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Field
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region Field

    /// <summary>
    /// 호스트
    /// </summary>
    private string host = "smtp.gmail.com";

    /// <summary>
    /// 송신자 메일 주소
    /// </summary>
    private string fromMailAddress;

    /// <summary>
    /// 송신자명
    /// </summary>
    private string fromName;

    /// <summary>
    /// 송신자 패스워드
    /// </summary>
    private string fromPassword;

    /// <summary>
    /// 참조 메일 주소 리스트
    /// </summary>
    private List<MailAddress> bccMailAddressList;

    /// <summary>
    /// 첨부 리스트
    /// </summary>
    private List<Attachment> attachmentList;

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 호스트 - Host

    /// <summary>
    /// 호스트
    /// </summary>
    public string Host
    {
        get
        {
            return this.host;
        }
        set
        {
            this.host = value;
        }
    }

    #endregion
    #region 송신자 메일 주소 - SenderMailAddress

    /// <summary>
    /// 송신자 메일 주소
    /// </summary>
    public string SenderMailAddress
    {
        get
        {
            return this.fromMailAddress;
        }
        set
        {
            this.fromMailAddress = value;
        }
    }

    #endregion
    #region 송신자명 - SenderName

    /// <summary>
    /// 송신자명
    /// </summary>
    public string SenderName
    {
        get
        {
            return this.fromName;
        }
        set
        {
            this.fromName = value;
        }
    }

    #endregion
    #region 송신자 패스워드 - SenderPassword

    /// <summary>
    /// 송신자 패스워드
    /// </summary>
    public string SenderPassword
    {
        get
        {
            return this.fromPassword;
        }
        set
        {
            this.fromPassword = value;
        }
    }

    #endregion
    #region 참조 메일 주소 리스트 - BCCMailAddressList

    /// <summary>
    /// 참조 메일 주소 리스트
    /// </summary>
    public List<MailAddress> BCCMailAddressList
    {
        get
        {
            return this.bccMailAddressList;
        }
        set
        {
            this.bccMailAddressList = value;
        }
    }

    #endregion
    #region 첨부 리스트 - BCCMailAddressList

    /// <summary>
    /// 첨부 리스트
    /// </summary>
    public List<Attachment> AttachmentList
    {
        get
        {
            return this.attachmentList;
        }
        set
        {
            this.attachmentList = value;
        }
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 생성자 - WebMailManager(fromMailAddress, fromName, fromPassword)

    /// <summary>
    /// 생성자
    /// </summary>
    /// <param name="fromMailAddress">송신자 메일 주소</param>
    /// <param name="fromName">송신자명</param>
    /// <param name="fromPassword">송신자 패스워드</param>
    public WebMailManager(string fromMailAddress, string fromName, string fromPassword)
    {
        this.fromMailAddress = fromMailAddress;
        this.fromName        = fromName;
        this.fromPassword    = fromPassword;

        this.bccMailAddressList = new List<MailAddress>();
        this.attachmentList     = new List<Attachment>();
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 참조 메일 주소 리스트 지우기 - ClearBCCMailAddressList()

    /// <summary>
    /// 참조 메일 주소 리스트 지우기
    /// </summary>
    public void ClearBCCMailAddressList()
    {
        this.bccMailAddressList.Clear();
    }

    #endregion
    #region 참조 추가하기 - AddBCC(mailAddress)

    /// <summary>
    /// 참조 추가하기
    /// </summary>
    /// <param name="mailAddress">메일 주소</param>
    public void AddBCC(MailAddress mailAddress)
    {
        this.bccMailAddressList.Add(mailAddress);
    }

    #endregion
    #region 참조 추가하기 - AddBCC(mailAddress, displayName)

    /// <summary>
    /// 참조 추가하기
    /// </summary>
    /// <param name="mailAddress">메일 주소</param>
    /// <param name="displayName">표시명</param>
    public void AddBCC(string mailAddress, string displayName)
    {
        AddBCC(new MailAddress(mailAddress, displayName));
    }

    #endregion
    #region 첨부 리스트 지우기 - ClearAttachmentList()

    /// <summary>
    /// 첨부 리스트 지우기
    /// </summary>
    public void ClearAttachmentList()
    {
        this.attachmentList.Clear();
    }

    #endregion
    #region 첨부 추가하기 - AddAttachment(attachment)

    /// <summary>
    /// 첨부 추가하기
    /// </summary>
    /// <param name="attachment">첨부</param>
    public void AddAttachment(Attachment attachment)
    {
        this.attachmentList.Add(attachment);
    }

    #endregion
    #region 첨부 추가하기 - AddAttachment(filePath, contentType)

    /// <summary>
    /// 첨부 추가하기
    /// </summary>
    /// <param name="filePath">파일 경로</param>
    /// <param name="contentType">컨텐트 타입</param>
    public void AddAttachment(string filePath, ContentType contentType)
    {
        AddAttachment(new Attachment(filePath, contentType));
    }

    #endregion
    #region 보내기 - Send(receiveMailAddress, receiveName, subject, isHTEMLContent, content)

    /// <summary>
    /// 보내기
    /// </summary>
    /// <param name="receiveMailAddress">수신자 메일 주소</param>
    /// <param name="receiveName">수신자명</param>
    /// <param name="subject">주제</param>
    /// <param name="isHTEMLContent">HTML 내용 여부</param>
    /// <param name="content">내용</param>
    public void Send(string receiveMailAddress, string receiveName, string subject, bool isHTEMLContent, string content)
    {
        MailAddress fromMailAddress = new MailAddress(this.fromMailAddress, this.fromName);
        MailAddress toMailAddress   = new MailAddress(receiveMailAddress  , receiveName  );

        SmtpClient smtpClient = new SmtpClient();

        smtpClient.Host           = this.host;
        smtpClient.Port           = 587;
        smtpClient.EnableSsl      = true;
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.Credentials    = new NetworkCredential(fromMailAddress.Address, this.fromPassword);
        smtpClient.Timeout        = 20000;

        using(MailMessage mailMessage = new MailMessage(fromMailAddress, toMailAddress))
        {
            mailMessage.Subject    = subject;
            mailMessage.IsBodyHtml = isHTEMLContent;
            mailMessage.Body       = content;

            foreach(MailAddress mailAddress in this.bccMailAddressList)
            {
                mailMessage.Bcc.Add(mailAddress);
            }

            foreach(Attachment attachment in this.attachmentList)
            {
                mailMessage.Attachments.Add(attachment);
            }

            smtpClient.Send(mailMessage);
        }
    }

    #endregion
}

 

728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요