我尝试使用Gmail SMTP服务器发送电子邮件,但邮件始终无法发送。浏览器窗口关闭,Express for Web闪烁,出现此错误。任何帮助将不胜感激。 代码已包含在下面
背后的代码
using System.Net.Mail;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Demos_email_demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Test Message";
myMessage.Body = "Hello World";
myMessage.From = new MailAddress("willrathlou3@gmail.com", "Will");
myMessage.To.Add(new MailAddress("willrathlou3@gmail.com", "Will"));
SmtpClient mySmtpClient = new SmtpClient("smtp.gmail.com");
mySmtpClient.Send(myMessage);
}
}
的Web.Config
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
</system.web>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="Will <willrathlou3@gmail.com>">
<network host ="smtp.gmail.com" enableSsl="true" port="465" />
</smtp>
</mailSettings>
</system.net>
</configuration>
答案 0 :(得分:0)
你需要像这样设置NetworkCredential。
client.Credentials = new NetworkCredential(“username”,“password”);
您的代码应该是这样的。
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Test Message";
myMessage.Body = "Hello World";
myMessage.From = new MailAddress("willrathlou3@gmail.com", "Will");
myMessage.To.Add(new MailAddress("willrathlou3@gmail.com", "Will"));
SmtpClient mySmtpClient = new SmtpClient("smtp.gmail.com");
mySmtpClient .Credentials=new NetworkCredential("username", "password");
mySmtpClient.Send(myMessage);