我想从我的应用程序发送电子邮件,并且我编写了以下代码来发送邮件
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);
我在本地主机上运行它,所以我在发送它时犯了什么错误。
当我发送按钮时,它给出了类似的错误
SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.5.1需要身份验证。
Web.config文件中的代码
<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>
我该怎么做才能解决此错误并发送邮件?