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

        private void button12_Click(object sender, EventArgs e)
        {
            SmtpMail smtpMail = new SmtpMail();
            smtpMail.SendComplete += SendComplete;
            smtpMail
                .Subject("hello world")
                .Body("testing mail")
                .From(@"123456@qq.com")
                .To(@"123456@qq.com")
                .CC(@"123456@qq.com")
                .Priority(System.Net.Mail.MailPriority.High)
                .Encoding(Encoding.UTF8)
                .User("123456")
                .Passord("123456")
                .Attachment(new System.Net.Mail.Attachment(@"D:\xxx.db"))
                .Attachment(new System.Net.Mail.Attachment(@"D:\xxx"))
                .SendAsync();
        }
        private void SendComplete(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show($"send failure-->{e.Error.Message}");
            }
            else
            {
                MessageBox.Show("Send successful");
            }
        }

使用起来也很简单

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 = System.Net.Mail.SmtpDeliveryMethod.Network,
                EnableSsl = true,
                Credentials = new System.Net.NetworkCredential(_User, _Password),
            };
        }
        SmtpClient smtpclient;
        /// <summary>
        /// Set Port,for qq mail not need to change
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail Port(int value)
        {
            smtpclient.Port = value;
            return this;
        }
        /// <summary>
        /// Set UseDefaultCredentials,for qq mail not need to change
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail UseDefaultCredentials(bool value)
        {
            smtpclient.UseDefaultCredentials = value;
            return this;
        }
        /// <summary>
        /// Set SmtpDeliveryMethod,for qq mail not need to change
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail SmtpDeliveryMethod(SmtpDeliveryMethod value)
        {
            smtpclient.DeliveryMethod = value;
            return this;
        }
        /// <summary>
        /// Set Host,for qq mail not need to change
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail Host(string value)
        {
            smtpclient.Host = value;
            return this;
        }
        /// <summary>
        /// Set EnableSsl,for qq mai net need to change
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail EnableSsl(bool value)
        {
            smtpclient.EnableSsl = value;
            return this;
        }
        string _User = "123456";
        string _Password = "123456";
        /// <summary>
        /// Set user name,for examp 123456
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public SmtpMail User(string value)
        {
            _User = value;
            smtpclient.Credentials = new System.Net.NetworkCredential(_User, _Password);
            return this;
        }
        /// <summary>
        /// Set send mail password
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public SmtpMail Passord(string value)
        {
            _Password = value;
            smtpclient.Credentials = new System.Net.NetworkCredential(_User, _Password);
            return this;
        }
        MailMessage mail = new MailMessage() { From = new MailAddress(@"154556@qq.com") };
        /// <summary>
        /// Set mail from address
        /// </summary>
        /// <param name="from"></param>
        /// <returns></returns>
        public SmtpMail From(string value)
        {
            mail.From = new MailAddress(value);
            return this;
        }
        /// <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 To(string value)
        {
            mail.To.Add(value);
            return this;
        }
        /// <summary>
        /// clear CC mail list
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail ClearCC(string value)
        {
            mail.CC.Clear();
            return this;
        }
        /// <summary>
        /// Add mail address to CC list
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail CC(string value)
        {
            mail.CC.Add(value);
            return this;
        }
        /// <summary>
        /// Set mail subject
        /// </summary>
        /// <param name="subject"></param>
        /// <returns></returns>
        public SmtpMail Subject(string value)
        {
            mail.Subject = value;
            return this;
        }
        /// <summary>
        /// Set mail body
        /// </summary>
        /// <param name="body"></param>
        /// <returns></returns>
        public SmtpMail Body(string value)
        {
            mail.Body = value;
            return this;
        }
        /// <summary>
        /// Clear all attachment
        /// </summary>
        /// <returns></returns>
        public SmtpMail ClearAttachment()
        {
            mail.Attachments.Clear();
            return this;
        }
        /// <summary>
        /// Add a attachmen
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail Attachment(Attachment value)
        {
            mail.Attachments.Add(value);
            return this;
        }
        /// <summary>
        /// Mail encoding
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail Encoding(Encoding value)
        {
            mail.BodyEncoding = value;
            mail.SubjectEncoding = value;
            mail.HeadersEncoding = value;
            return this;
        }
        /// <summary>
        /// Set mail priority
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public SmtpMail Priority(MailPriority value)
        {
            mail.Priority = value;
            return this;
        }
        /// <summary>
        /// Send finally return ture if send successful
        /// </summary>
        /// <returns></returns>
        public bool Send()
        {
            SendSuccessful = false;
            try
            {
                smtpclient.Send(mail);
                SendSuccessful = true;
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 发送成功标记
        /// </summary>
        public bool SendSuccessful
        {
            get;set;
        }
        /// <summary>
        /// Send Async
        /// </summary>
        public async void SendAsync()
        {
            SendSuccessful = false;
            if (mail is null) return;
            try
            {
                await smtpclient.SendMailAsync(mail);
                SendSuccessful = true;
            }
            catch (Exception)
            {
                SendSuccessful = false;
            }
        }
        /// <summary>
        /// Send complete event
        /// </summary>
        public event SendCompletedEventHandler SendComplete
        { 
            add 
            {
                smtpclient.SendCompleted += value;             
            }
            remove
            {
                smtpclient.SendCompleted -= value;
            }
        }
    }
}

  如果是163需要使用mailkit+mimekt,用下面的代码发送

            using (var client = new MailKit.Net.Smtp.SmtpClient())
            {
                client.Connect("smtp.163.com", 465, true);
                client.Authenticate(@"123456@163.com", "123456");
                MimeMessage mimeMessage = new MimeMessage();
                mimeMessage.From.Add(new MailboxAddress("123456", "123456@163.com"));
                mimeMessage.To.Add(new MailboxAddress("friend", @"123456@qq.com"));
                mimeMessage.Subject = "C# testing mail";
                mimeMessage.Body = new TextPart(MimeKit.Text.TextFormat.Plain) {Text="Test email" };
                client.Send(FormatOptions.Default, mimeMessage);
                client.Disconnect(true);
            }

 

posted @ 2021-09-11 19:58  冬日厦语  阅读(93)  评论(1)    收藏  举报