使用c#和QQ邮箱发送邮件

1.第一步,开启stmp服务,参考 https://jingyan.baidu.com/article/6079ad0eb14aaa28fe86db5a.html。记住授权码

2.邮件服务帮助类 参考 https://www.cnblogs.com/huage-1234/p/7850449.html。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net.Mail;

using System.Net.Mime;

using System.Text;

 

namespace Mail

{

    public class SendMail

    {

        private MailMessage mailMessage;

        private SmtpClient smtpClient;

        private string password;//发件人密码 

        ///

 

        /// 设置MailMessage的实例

        ///

 

        /// 收件人地址 

        /// 发件人地址 

        /// 邮件正文 

        /// 邮件的主题 

        /// 发件人密码 

        public SendMail(string[] To, string From, string Body, string Title, string Password)

        {

            mailMessage = new MailMessage();

            foreach (var item in To)

            {

                mailMessage.To.Add(item);

            }

            mailMessage.From = new System.Net.Mail.MailAddress(From);

            mailMessage.Subject = Title;

            mailMessage.Body = Body;

            mailMessage.IsBodyHtml = true;

            mailMessage.BodyEncoding = System.Text.Encoding.UTF8;

            mailMessage.Priority = System.Net.Mail.MailPriority.Normal;

            this.password = Password;

        }

        ///

 

        /// 添加附件 

        ///

 

        public void Attachments(string Path)

        {

            string[] path = Path.Split(',');

            Attachment data;

            ContentDisposition disposition;

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

            {

                data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化附件 

                disposition = data.ContentDisposition;

                disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取附件的创建日期 

                disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//获取附件的修改日期 

                disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附件的读取日期 

                mailMessage.Attachments.Add(data);//添加到附件中 

            }

        }

        ///

 

        /// 异步发送邮件 

        ///

 

        ///  

        public void SendAsync(SendCompletedEventHandler CompletedMethod)

        {

            if (mailMessage != null)

            {

                smtpClient = new SmtpClient();

                smtpClient.EnableSsl = true;// 指定 System.Net.Mail.SmtpClient 是否使用安全套接字层 (SSL) 加密连接,必须在实例身份前面设置

                smtpClient.UseDefaultCredentials = false

                smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据 

                smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

                smtpClient.Host = "smtp." + mailMessage.From.Host;

                smtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//注册异步发送邮件完成时的事件 

                smtpClient.SendAsync(mailMessage, mailMessage.Body);

            }

        }

        ///

 

        /// 发送邮件 

        ///

 

        public void Send()

        {

            if (mailMessage != null)

            {

                using (smtpClient = new SmtpClient())

                {

                    smtpClient.EnableSsl = true;// 指定 System.Net.Mail.SmtpClient 是否使用安全套接字层 (SSL) 加密连接,必须在实例身份前面设置

                    smtpClient.UseDefaultCredentials = false;   

                    smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据           

                    smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

                    smtpClient.Host = "smtp." + mailMessage.From.Host;

                    smtpClient.Send(mailMessage);

                    mailMessage.Attachments.Dispose();

                }

            }

        }

    }

}

3. 调用邮件服务帮助类发送邮件

        string[] toArr = { "收件邮箱" };     
        SendMail sendmail = new SendMail(toArr, "发送者邮箱", "邮件内容", "邮箱主题", "邮件授权码");
        string path = Server.MapPath("/UploadFile/News/201503/");//附件上传路径
        string fileName =  "20150317100810664.txt";//附件名称
        string fileAll = path + fileName;
        sendmail.Attachments(fileAll);//添加附件
        sendmail.Send();//发送邮件

你可能感兴趣的:(使用c#和QQ邮箱发送邮件)