一、核心代码如下
protected void Page_Load(object sender, EventArgs e)
{
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Test Message";
myMessage.Body = "Hello world, from planet wrox";
myMessage.From = new MailAddress("***********@qq.com");
myMessage.To.Add(new MailAddress("***********@qq.com"));
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMessage);
}
二、对邮件服务的配置
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="smtp.qq.com" userName="*************@qq.com" password="*****************" port="587" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
其中userName为邮箱名称,password为邮箱密码,但是最新qq邮箱,要用授权码
生成授权码,然后邮箱密码用授权码替换
注意上图相关的邮箱服务要开启
通过上面步骤即可完成邮件的发送