.NET后台代码利用QQ邮箱服务器发送邮件遇到的问题:"mail from address must be same as authorization user"
首先,看下我的代码实现
后台代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
public class EmailHelper { /// <summary> /// 发送邮件 /// </summary> /// <param name="subject">主题</param> /// <param name="body">内容</param> public static void SendEmail( string subject, string body) { var mailFrom = ConfigurationManager.AppSettings[ "mailFrom" ]; var mailTo = ConfigurationManager.AppSettings[ "mailTo" ]; var mailCC = ConfigurationManager.AppSettings[ "mailCC" ]; var smtp = ConfigurationManager.AppSettings[ "smtp" ]; var smtpPort = Convert.ToInt32(ConfigurationManager.AppSettings[ "smtpPort" ]); var emailAccount = ConfigurationManager.AppSettings[ "emailAccount" ]; var emailPassword = ConfigurationManager.AppSettings[ "emailPassword" ]; //邮件内容信息 MailMessage mailMessage = new MailMessage { From = new MailAddress(mailFrom), Subject = subject, Body = body, }; mailMessage.To.Add(mailTo); mailMessage.CC.Add(mailCC); //邮箱服务器配置 SmtpClient smtpClient = new SmtpClient(smtp, smtpPort) { Credentials = new NetworkCredential(emailAccount, emailPassword), EnableSsl = true , }; //发送邮件 smtpClient.Send(mailMessage); } } |
配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
< appSettings > <!--发送人、收件人、抄件人--> < add key = "mailFrom" value = "53xxx1314@qq.com" /> < add key = "mailTo" value = "89xxxx734@qq.com" /> < add key = "mailCC" value = "89xxx734@qq.com" /> <!--邮箱服务器主机地址和端口号--> < add key = "smtp" value = "smtp.qq.com" /> < add key = "smtpPort" value = "25" /> <!--发送人邮箱账号密码--> < add key = "emailAccount" value = "53xxx1314" /> <!--此密码非QQ密码,而是邮箱生成的密码--> < add key = "emailPassword" value = "stxxxxxxxxtscbbb" /> </ appSettings > |
代码已经搞定,剩下的就是跑起来了
第一次跑起来遇到问题就是开头所说的"mail from address must be same as authorization user"
具体为什么会这样呢,我的账号密码没错啊,看了几篇博友的文章后确定是QQ邮箱密码出问题了。但是我用QQ密码登入邮箱是正常的,然后尝试开启POP/SMTP服务,发现了主要问题
QQ邮箱的POP/SMTP 服务没开启
操作步骤:
进入QQ邮箱->设置->账户->滚动到“POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务”
如果是没有开启,那就开启它
我没有开启,点击后需要发送短信,配置邮件客户端
发送短信完毕后,获得了授权码,将授权码替换我在代码中写的qq密码,然后重新跑起来,错误消失,主要是未授权问题
注意,授权码会在QQ密码或独立密码修改后而失效 ,需要重新获得。
重新运行,没有问题
问题虽然不是很大,也没有太多技术含量,但是能节省我们再次遇到这种问题,却想不起怎么解决的,回顾起来,总比重新重视要节省时间。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/CKExp/archive/2017/10/22/7709310.html