Net SMTP QQ 发送邮件

调用DEMO

      var currUser = new List<string> { "123@qq.com" , "123@qq.com" , "123@qq.com" };// 单个
            var title = "test";
            var content = "hello word";
            mh.SendSMTPEMail(currUser, title, content);

  

方法:

    public class MailHelper
    {
        private string emailAcount = ConfigurationManager.AppSettings["EmailAcount"];
        private string emailPassword = ConfigurationManager.AppSettings["EmailPassword"];
        private string emailSmart = ConfigurationManager.AppSettings["EmailSmart"];

        public void SendSMTPEMail(string strto, string strSubject, string strBody)
        {
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(emailSmart);
            client.UseDefaultCredentials = false;
            client.EnableSsl = true;
            client.Credentials = new System.Net.NetworkCredential(emailAcount, emailPassword);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(emailAcount, strto, strSubject, strBody);
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;

            //使用文件路径发送附件
            //AlternateView item = new AlternateView(@"D:\软件\(软电话)eyebeam1.5.rar", "application/x-rar-compressed");
            //message.AlternateViews.Add(item);
            //message.AlternateViews.Dispose();

            //使用Stream发送附件 
            //Attachment letter = new Attachment(FileUploadLetter.FileContent, FileUploadLetter.PostedFile.ContentType);
            //letter.ContentDisposition.Inline = true;
            //letter.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
            ////inline.ContentId = "1";
            //letter.ContentType.MediaType = FileUploadLetter.PostedFile.ContentType;
            //letter.ContentType.Name = Path.GetFileName(FileUploadLetter.PostedFile.FileName);
            //letter.Name = Path.GetFileName(FileUploadLetter.PostedFile.FileName);
            //message.Attachments.Add(letter);
            //message.Attachments.Dispose();

            client.Send(message);
        }

        public void SendSMTPEMail(List<string> strto, string strSubject, string strBody)
        {
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(emailSmart);
            client.UseDefaultCredentials = false;
            client.EnableSsl = true;
            client.Credentials = new System.Net.NetworkCredential(emailAcount, emailPassword);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            foreach (var str in strto)
            {
                try
                {
                    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(emailAcount, str, strSubject, strBody);
                    message.BodyEncoding = System.Text.Encoding.UTF8;
                    message.IsBodyHtml = true;
                    client.Send(message);
                }
                catch
                {

                }

            }


        }
    }

  

1.补充知识

(1)POP3和SMTP服务器是什么?

简单点来说:POP3 用于接收电子邮件 ,SMTP 用于发送电子邮件。

(1)POP3具体指什么?

POP3(Post Office Protocol 3)即邮局协议的第3个版本,它是规定个人计算机如何连接到互联网上的邮件服务器进行收发邮件的协议。它是因特网电子邮件的第一个离线协议标准,POP3协议允许用户从服务器上把邮件存储到本地主机(即自己的计算机)上,同时根据客户端的操作删除或保存在邮件服务器上的邮件,而POP3服务器则是遵循POP3协议的接收邮件服务器,用来接收电子邮件的。POP3协议是TCP/IP协议族中的一员,,由RFC 1939 定义

(2)SMTP具体是指什么?

SMTP的全称是"Simple Mail Transfer Protocol",即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式。SMTP 协议属于 TCP/IP 协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。SMTP 服务器就是遵循 SMTP 协议的发送邮件服务器。

 

2.System.Net.Mail

使用ASP.NET发送电子邮件,需要引用System.Net.Mail命名空间。System.Net.Mail 命名空间包含用于将电子邮件发送到简单邮件传输协议 (SMTP) 服务器进行传送的类。

(1)命名空间下有三个比较主要的类:

MailMessage:提供属性和方法来创建一个邮件消息对象,即邮件内容。

Attachment:提供属性和方法来创建一个邮件附件对象,即邮件附件。

SmtpClient:将电子邮件传输到您指定用于邮件传送的 SMTP 主机。

(2)MailMessage类:

From:发送邮件的地址 
To:接收邮件的地址 
Subject:邮件的标题 
Priority:邮件的优先级(分别为为High,Low,Normal) 
Attachments:电子邮件的数据的附件集合
Bcc:密送地址 
Cc:抄送地址 
Body:邮件正文
SubjectEncoding:电子邮件的主题内容使用的编码

IsBodyHtml:邮件正文是否为 Html 格式的值

详细参考:MailMessage

(3)Attachment类:

详细参考:Attachment

(4)SmtpClient类:

DeliveryMethod:指定如何处理待发的电子邮件

Host:SMTP 事务的主机的名称或 IP 地址

Credentials:设置用于验证发件人身份的凭据

详细参考:SmtpClient

 

 3.ASP.NET发送邮件两种方式

(1)通过邮件服务提供商的SMTP来发送邮件

首先需要注册对应服务提供商免费邮箱,因为你要使用邮件服务提供商的SMTP,他们需要对身份进行验证,这样可以避免产生大量的垃圾邮件。

有三个是重要的信息:SMTP服务器、用户名、密码。从网上收集了几个类,可以参考下。

原文地址:https://www.cnblogs.com/0to9/p/7986815.html