如何使用SMTP邮件配置发送邮件。 [英] How to sent mail using SMTP mail config.?
本文介绍了如何使用SMTP邮件配置发送邮件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下错误
below error is shown when sent the mail
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
我尝试过:
我使用下面的代码来发送邮件配置.thats适用于其他应用程序但我的应用程序显示以下错误如何解决它。
What I have tried:
I use below code to mail config .thats works in other applications but my application shows the below error how to fix it.
Msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["MailServer"];
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
Msg.From = new MailAddress(ConfigurationManager.AppSettings["MailFrom"]);
NetworkCred.Password = ConfigurationManager.AppSettings["MailPassword"];
smtp.Credentials = NetworkCred;
smtp.Port = 25;
smtp.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};
smtp.Send(Msg);
推荐答案
你设置了NetworkCred.Password
,但不是NetworkCred.UserName
。大多数服务器都需要用户名和密码进行身份验证。
但还有更好的方法:将配置放在< SMTP>元素(网络设置) [ ^ ]:
You've set theNetworkCred.Password
, but not theNetworkCred.UserName
. Most servers will require both a username and a password for authentication.
But there's a better way: put your configuration in the <smtp> Element (Network Settings)[^]:
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="network" from="example@gmail.com">
<network
host="smtp.gmail.com"
port="587"
enableSsl="true"
defaultCredentials="false"
userName="YOUR-USER-NAME"
password="YOUR-PASSWORD"
/>
</smtp>
</mailSettings>
</system.net>
</configuration>
有了这个,你不需要配置 SmtpClient
;默认构造函数将自动从配置文件中读取设置。
With that in place, you don't need to configure the SmtpClient
; the default constructor will automatically read the settings from the config file.
SmtpClient smtp = new SmtpClient();
smtp.Send(Msg);
这篇关于如何使用SMTP邮件配置发送邮件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文