做一个发送QQ邮箱的SMTP Mail类。猜测其他邮箱也可以

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class SmtpMail
    {
        public SmtpMail()
        {
            smtpclient = new SmtpClient()
            {
                Port = 587,
                Host = "smtp.qq.com",
                UseDefaultCredentials = false,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                EnableSsl = true,
                Credentials = new System.Net.NetworkCredential(User, Password),
            };
        }
        SmtpClient smtpclient;

        private int Port 
        {
            get
            {
                return smtpclient.Port;
            }
            set
            {
                smtpclient.Port = value;
            }
        }
        /// <summary>
        /// Set Port,for qq mail not need to change
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail SetPort(int value)
        {
            Port = value;
            return this;
        }
        private bool UseDefaultCredentials
        {
            get
            {
                return smtpclient.UseDefaultCredentials;
            }
            set
            {
                smtpclient.UseDefaultCredentials = value;
            }
        }
        /// <summary>
        /// Set UseDefaultCredentials,for qq mail not need to change
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail SetUseDefaultCredentials(bool value)
        {
            UseDefaultCredentials = value;
            return this;
        }
        private SmtpDeliveryMethod DeliveryMethod
        {
            get
            {
                return smtpclient.DeliveryMethod;
            }
            set
            {
                smtpclient.DeliveryMethod = value;
            }
        }
        /// <summary>
        /// Set SmtpDeliveryMethod,for qq mail not need to change
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail SetSmtpDeliveryMethod(SmtpDeliveryMethod value)
        {
            DeliveryMethod = value;
            return this;
        }
        private string Host
        {
            get
            {
                return smtpclient.Host;
            }
            set
            {
                smtpclient.Host = value;
            }
        }
        /// <summary>
        /// Set Host,for qq mail not need to change
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail SetHost(string value)
        {
            Host = value;
            return this;
        }
        private bool EnableSsl
        {
            get
            {
                return smtpclient.EnableSsl;
            }
            set
            {
                smtpclient.EnableSsl = value;
            }
        }
        /// <summary>
        /// Set EnableSsl,for qq mai net need to change
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail SetEnableSsl(bool value)
        {
            EnableSsl = value;
            return this;
        }
        string _User = "123456";
        private string User
        {
            get
            {
                return _User;
            }
            set
            {
                _User = value;
                smtpclient.Credentials = new System.Net.NetworkCredential(User, Password);
            }
        }
        /// <summary>
        /// Set user name,for examp 123456
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public SmtpMail SetUser(string user)
        {
            User = user;
            return this;
        }
        string _Password = "123456";
        private string Password
        {
            get
            {
                return _Password;
            }
            set
            {
                _Password = value;
                smtpclient.Credentials = new System.Net.NetworkCredential(User, Password);
            }
        }
        /// <summary>
        /// Set send mail password
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public SmtpMail SetPassord(string password)
        {
            Password = password;
            return this;
        }
        MailMessage mail = new MailMessage() { From = new MailAddress(@"123456@qq.com") };
        private string From
        {
            get
            {
                return mail.From.ToString();
            }
            set
            {
                mail.From = new MailAddress(value); 
            }
        }
        /// <summary>
        /// Set mail from address
        /// </summary>
        /// <param name="from"></param>
        /// <returns></returns>
        public SmtpMail SetFrom(string from)
        {
            From = from;
            return this;
        }
        public MailAddressCollection To
        {
            get
            {
                return mail.To;
            }
        }
        /// <summary>
        /// clear To mail list
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail ClearTo(string value)
        {
            mail.To.Clear();
            return this;
        }
        /// <summary>
        /// Add mail address to To list
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail AddTo(string value)
        {
            mail.To.Add(value);
            return this;
        }
        private string Subject
        {
            get
            {
                return mail.Subject;
            }
            set
            {
                mail.Subject = value;
            }
        }
        /// <summary>
        /// Set mail subject
        /// </summary>
        /// <param name="subject"></param>
        /// <returns></returns>
        public SmtpMail SetSubject(string subject)
        {
            Subject = subject;
            return this;
        }
        private string Body
        {
            get
            {
                return mail.Body;
            }
            set
            {
                mail.Body = value;
            }
        }
        /// <summary>
        /// Set mail body
        /// </summary>
        /// <param name="body"></param>
        /// <returns></returns>
        public SmtpMail SetBody(string body)
        {
            Body = body;
            return this;
        }
        /// <summary>
        /// Send finally return ture if send successful
        /// </summary>
        /// <returns></returns>
        public bool Send()
        {                
            try
            {
                smtpclient.Send(mail);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}

发送的方法很简单

            SmtpMail smtpMail = new SmtpMail();
            var a = smtpMail
                .SetBody("testing mail")
                .SetSubject("hello world")
                .AddTo(@"123456@qq.com")
                .AddTo(@"123456@qq.com")
                .SetUser("123456")
                .SetPassord("123456")
                .Send();

 

上一篇:Python-zmail发送简单邮件


下一篇:syslog的使用