首页 > 代码库 > C#使用QQ发送邮件

C#使用QQ发送邮件

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Net;
 6 using System.Net.Mail;
 7 using System.Security.Cryptography;
 8 using System.Text;
 9 using System.Threading.Tasks;
10 
11 namespace ConsoleApplication1
12 {
13     public class CommonHelper
14     {
15         /// <summary>
16         /// 使用qq发邮件
17         /// </summary>
18         /// <param name="title">邮件标题</param>
19         /// <param name="body">邮件内容</param>
20         /// <param name="toWhom">发送给谁</param>
21         public static void SendEmai(string title, string body, string toWhom)
22         {
23             using (MailMessage mailMessage = new MailMessage())
24             using (SmtpClient smtpClient = new SmtpClient("smtp.qq.com", 587))
25             {
26                 mailMessage.To.Add(toWhom);
27                 mailMessage.Body = body;
28                 //设置邮件内容是否是 HTML 格式
29                 mailMessage.IsBodyHtml = true;
30                 mailMessage.BodyEncoding = Encoding.UTF8;
31                 mailMessage.From = new MailAddress("from@qq.com", "显示名称");
32                 mailMessage.Subject = title;
33                 mailMessage.SubjectEncoding = Encoding.UTF8;
34                 smtpClient.EnableSsl = true;
35                 //qq启用了“客户端授权码”,要用授权码代替密码
36                 smtpClient.Credentials = new NetworkCredential("from@qq.com", "授权密码");
37                 smtpClient.Send(mailMessage);
38             }
39         }
40     }
41 }

 

QQ邮箱授权码如何获取?

  在邮箱设置-帐户页面下开启SMTP服务,再生成授权码。

  技术分享

技术分享

 

C#使用QQ发送邮件