发送邮件

it2022-06-26  95

流程说明

以QQ邮箱为例,在设置-账户一栏中,找到“POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务” 根据提示,开启POP3/SMTP服务,最后会得到一串授权码 之后查询QQ邮箱的服务器,关键词是 qq email host 得到信息:https://service.mail.qq.com/cgi-bin/help?subtype=1&&no=167&&id=28 至此得到所有需要的信息,开始写代码

关键代码

.net与.net core类似

//host和端口号,根据服务类型查询对应邮件的设置 SmtpClient SmtpServer = new SmtpClient("smtp.qq.com"); var mail = new MailMessage(); mail.From = new MailAddress("sender@qq.com"); //这里可以添加多个 mail.To.Add("receiver@qq.com"); //标题 mail.Subject = "Test Mail - 1"; mail.IsBodyHtml = true; string htmlBody; htmlBody = "Write some HTML code here"; //内容 mail.Body = htmlBody; //端口号 SmtpServer.Port = 587; SmtpServer.UseDefaultCredentials = false; //身份认证 //这里的密码是授权码,而非账号密码,在开启POP3/SMTP服务服务之后获得 SmtpServer.Credentials = new System.Net.NetworkCredential("sender@qq.com", "password"); SmtpServer.EnableSsl = true; SmtpServer.Send(mail);

示例代码

https://github.com/zLulus/NotePractice/blob/dev3/Console/CodeLibrary/SendEmail/SendEmailDemo.cs https://github.com/zLulus/NotePractice/blob/dev3/Console/CodeLibraryForDotNetCore/SendEmail/SendEmailDemo.cs

参考资料

https://stackoverflow.com/questions/9851319/how-to-add-smtp-hotmail-account-to-send-mail https://dotnetcoretutorials.com/2017/08/20/sending-email-net-core-2-0/

转载于:https://www.cnblogs.com/Lulus/p/9816381.html


最新回复(0)