SMTP exception. asking for authentication? [closed]
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage message = new MailMessage()
{
Subject = "Subject",
Body = "Body"
};
message.To.Add(new MailAddress("demo@example.com", "Some name"));
SmtpClient client = new SmtpClient();
client.EnableSsl = true;
client.Send(message);
}
config
<configuration>
<system.net>
<mailSettings>
<smtp from="de开发者_Go百科mo@example.com">
<network host="****" defaultCredentials="false" port="587" userName="****" password="****" />
</smtp>
</mailSettings>
</system.net>
<system.web>
Correction. it works now. Thanks Marek
Your problem might be with defaultCredentials
set to true. Try setting that to false and try again. Also, why are you setting your credentials again in code? Gmail also requires emails to be sent via SSL (if I remember correctly) on port 587.
Just today I implemented gmail smtp in my code. My settings and code:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="MY_GMAIL_EMAIL">
<network defaultCredentials="false" host="smtp.gmail.com" port="587" userName="MY_GMAIL_USERNAME" password="MY_GMAIL_PASSWORD" />
</smtp>
</mailSettings>
</system.net>
MailMessage message = new MailMessage() {
Subject = "Subject",
Body = "Body"
};
message.To.Add(new MailAddress("someemail@domain.com", "Some name"));
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.Send(message);
精彩评论