1、首先在QQ邮箱当中开启“POP3/SMTP服务”
.2、获取“授权码
刚开始一直使用邮箱的密码。后面才发现,原来QQ邮箱通过第三方发送邮件应该使用授权码 o(≧口≦)o
附上腾讯的说明http://service.mail.qq.com/cgi-bin/help?subtype=1&&no=1001256&&id=28
3、编码
使用SmtpClient来解决
EnableSsl设置为true
UseDefaultCredentials设置为false
Credentials = new System.Net.NetworkCredential("QQ邮箱", "授权码");
附上代码,做个备忘
- public static void Send(Email mail,string email)
- {
- SmtpClient client = new SmtpClient("smtp.qq.com");
- client.EnableSsl = true;
- client.UseDefaultCredentials = false;
- client.Credentials = new System.Net.NetworkCredential("QQ邮箱", "授权码");
- MailAddress from = new MailAddress("QQ邮箱", "显示名称", Encoding.UTF8);//初始化发件人
- MailAddress to = new MailAddress(email, "", Encoding.UTF8);//初始化收件人
- //设置邮件内容
- MailMessage message = new MailMessage(from, to);
- message.Body = mail.Body;
- message.BodyEncoding = mail.BodyEncoding;
- message.Subject = mail.Subject;
- message.SubjectEncoding = mail.SubjectEncoding;
- message.IsBodyHtml = mail.IsBodyHtml;
- //发送邮件
- try
- {
- client.Send(message);
- }
- catch (InvalidOperationException iex)
- { }
- catch (Exception ex)
- { }
- }
注意:一定要先设置 EnableSsl和UseDefaultCredentials,再实例化Credentials
原文地址:https://www.cnblogs.com/mapzy/p/9035836.html
时间: 2024-10-09 22:29:34