发送邮件

流程说明

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

关键代码

.net与.net core相似git

//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);

示例代码

.net版本
.net core版本github

参考资料

How to add smtp hotmail account to send mail
Sending Email In .NET Core 2.0c#

相关文章
相关标签/搜索