自分で書いたメール送信クラス

3655 ワード

テストメールアドレス:網易、QQ
using System;

using System.Collections;

using System.Net.Sockets;

using System.IO;

using System.Text;

using System.Net.Mail;



namespace HuLangCms

{

    /// <summary>

    ///  

    /// </summary>

    public class MailSender

    {

        private string setUserEmail;

        private string setUserEmailPass;

        private string setServerName;

        private int setServerPort;

        private string setSendMailName;

        private string setSubject;

        private string setHtmlBody;



        /// <summary>

        ///  

        /// </summary>

        public string UserEmail

        {

            get

            {

                return setUserEmail;

            }

            set

            {

                setUserEmail = value;

            }

        }

        /// <summary>

        ///  

        /// </summary>

        public string UserEmailPass

        {

            get

            {

                return setUserEmailPass;

            }

            set

            {

                setUserEmailPass = value;

            }

        }

        /// <summary>

        ///  

        /// </summary>

        public string ServerName

        {

            get

            {

                return setServerName;

            }

            set

            {

                setServerName = value;

            }

        }

        /// <summary>

        ///  

        /// </summary>

        public int ServerPort

        {

            get

            {

                return setServerPort;

            }

            set

            {

                setServerPort = value;

            }

        }

        /// <summary>

        ///  

        /// </summary>

        public string SendMailName

        {

            get

            {

                return setSendMailName;

            }

            set

            {

                setSendMailName = value;

            }

        }

        /// <summary>

        ///  

        /// </summary>

        public string Subject

        {

            get

            {

                return setSubject;

            }

            set

            {

                setSubject = value;

            }

        }

        /// <summary>

        ///  

        /// </summary>

        public string HtmlBody

        {

            get

            {

                return setHtmlBody;

            }

            set

            {

                setHtmlBody = value;

            }

        }

        /// <summary>

        ///  

        /// </summary>

        public void SendMail()

        {

            string guidStr = Guid.NewGuid().ToString();

            try

            {

                MailMessage mm = new MailMessage(setUserEmail, setSendMailName);

                mm.Subject = setSubject;

                mm.Body = setHtmlBody;

                mm.IsBodyHtml = true;

                SmtpClient client = new SmtpClient();

                client.Host = setServerName;

                client.Port = setServerPort;

                client.Credentials = new System.Net.NetworkCredential(setUserEmail, setUserEmailPass);

                client.Send(mm);

            }

            catch (Exception ex)

            {

                if (ex is SmtpException)

                {

                    ex.ToString();

                }

                else

                {

                }

            }

        }



    }

}