问题描述
我想从我的应用程序中发送一封电子邮件,并且我已经编写了以下用于发送邮件的代码
I want to send an email from my application and i have written following code for sending mail
MailMessage msg = new MailMessage();
msg.From = new MailAddress("mymailid");
msg.To.Add("receipientid");
msg.Subject = "test";
msg.Body = "Test Content";
msg.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("mymailid", "mypassword", "smtp.gmail.com");
client.Host = "smtp.gmail.com";
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.UseDefaultCredentials = true;
client.Send(msg);
我在本地主机上运行它,所以我在发送它时犯了什么错误.
I am running it on localhost so what mistake i am doing to send it.
当我发送按钮时,它会出现类似的错误
When i send button it gives an error like
SMTP 服务器需要安全连接,或者客户端没有认证.服务器响应是:5.5.1 身份验证必需.
Web.config 文件中的代码
Code in Web.config file
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="smtpServer" value="smtp.gmail.com" />
<add key="EnableSsl" value = "true"/>
<add key="smtpPort" value="587" />
<add key="smtpUser" value="sender@gmail.com" />
<add key="smtpPass" value="mypassword" />
<add key="adminEmail" value="sender@gmail.com" />
</appSettings>
<system.net>
<mailSettings>
<smtp from="sender@gmail.com">
<network host="smtp.gmail.com" password="mypassword" port="587" userName="sender@gmail.com" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
我应该怎么做才能解决这个错误并发送邮件??
what should i do to solve this error and send mail??
推荐答案
首先检查gmail的安全相关问题.您可能在 gmail 中启用了双重身份验证.如果您收到任何安全警报,还请检查您的 gmail 收件箱.在这种情况下,请检查@mjb 的其他答案,如下所示
First check for gmail's security related issues. You may have enabled double authentication in gmail. Also check your gmail inbox if you are getting any security alerts. In such cases check other answer of @mjb as below
以下是我总是首先检查此类问题的非常普遍的事情
Below is the very general thing that i always check first for such issues
client.UseDefaultCredentials = true;
将其设置为 false.
set it to false.
注意@Joe King 的 answer - 您必须设置 client.UseDefaultCredentials before 设置客户端.凭据
Note @Joe King's answer - you must set client.UseDefaultCredentials before you set client.Credentials
这篇关于SMTP 服务器需要安全连接或客户端未通过身份验证.服务器响应是:5.5.1 需要身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!