[英]Error while sending email with credentials from web.config
I'm developing a website by ASP.NET MVC. 我正在通过ASP.NET MVC开发一个网站。 I have this function in my
IdentityConfig.cs
: 我在
IdentityConfig.cs
具有此功能:
public Task SendAsync(IdentityMessage message)
{
SmtpClient client = new SmtpClient();
var msg = new MailMessage();
msg.From = new MailAddress("info@mywebsite.com");
msg.To.Add(message.Destination);
msg.Subject = message.Subject;
msg.Body = message.Body;
client.Host = "mywebsite.com";
client.Port = 25;
client.Credentials = new
System.Net.NetworkCredential("info@mywebsite.com", "mypassword");
return client.SendMailAsync(msg);
}
I set up SMTP Service in IIS 8 and this function works fine. 我在IIS 8中设置了SMTP服务,此功能运行良好。 The problem is when I config SMTP credentials inside
Web.config
and try to send an email. 问题是当我在
Web.config
配置SMTP凭据并尝试发送电子邮件时。 MailSettings part of Web.config
is as follows: Web.config
MailSettings部分如下:
<system.net>
<mailSettings>
<smtp>
<network host="mywebsite.com"
port="25" userName="info@mywebsite.com" password="mypassword" />
</smtp>
</mailSettings>
</system.net>
and the new SendAsync function is: 并且新的SendAsync函数是:
public Task SendAsync(IdentityMessage message)
{
SmtpClient client = new SmtpClient();
var msg = new MailMessage();
msg.From = new MailAddress("info@mywebsite.com");
msg.To.Add(message.Destination);
msg.Subject = message.Subject;
msg.Body = message.Body;
return client.SendMailAsync(msg);
}
But when running this inside my controller, the following error occurs: 但是在我的控制器中运行此命令时,发生以下错误:
Bad sequence of commands.
命令顺序错误。 The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address.
服务器响应为:尝试发送到非本地电子邮件地址时,此邮件服务器需要身份验证。 Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.
请检查您的邮件客户端设置,或与管理员联系以验证是否为此服务器定义了域或地址。
What could be the cause? 可能是什么原因?
try adding defaultCredentials to false 尝试将defaultCredentials添加为false
<network
clientDomain="mywebsite.com"
defaultCredentials="false"
enableSsl="true"
host="smtp.gmail.com"
port="587"
userName="sender@mywebsite.it" password="xxxxxxx"
/>
Note that for some servers (like gmail) the message.From
needs to match with the username 请注意,对于某些服务器(例如gmail),该
message.From
发件人需要与用户名匹配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.