SMTP服务器需要安全连接,或者客户端未通过身份验证.服务器响应为:5.5.1需要身份验证.?8 [英] The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.?8
问题描述
我正在使用c#创建示例电子邮件发件人,但显示错误为:
I was create sample email sender using c# but it shows an error is:
SMTP服务器需要安全连接,或者客户端未通过身份验证.服务器响应为:5.5.1需要身份验证.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add("toAddress@gmail.com");
mail.From = new MailAddress("fromAddress@gmail.com", "Test Mail !", System.Text.Encoding.UTF8);
mail.Subject = "Test Mail";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "Test message";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("myMail@gmail.com", "password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.UseDefaultCredentials = true;
client.Send(mail);
Web配置:
<system.net>
<mailSettings>
<smtp from="myMail@gmail.com">
<network host="smtp.gmail.com" password="password"
port="587" userName="myMail@gmail.com" />
</smtp>
</mailSettings>
</system.net>
推荐答案
如果启用了双重身份验证,则需要为自己的Google帐户生成一个应用密码.
If you have two-factor authentication enabled, you will need to generate yourself an App Password for your Google account.
生成此类密码的步骤如下( [Google页面] | [已归档页面] ):
The steps for generating such password are as follows ( [Google page] | [Archived page]):
- 访问您的应用密码页面.可能会要求您登录到您的Google帐户.
- 在底部,单击选择应用程序",然后选择您正在使用的应用程序.
- 点击选择设备,然后选择您正在使用的设备.
- Visit your App passwords page. You may be asked to sign in to your Google Account.
- At the bottom, click Select app and choose the app you’re using.
- Click Select device and choose the device you’re using.
- 点击生成.
- 按照说明在设备上输入应用密码(黄色栏中的16个字符代码).
- 点击完成.
点击完成后,您将不会再看到该应用的密码. 但是,您将看到已创建的应用程序和设备的列表 的密码.
Once you click done, you’ll won’t see that App password code again. However, you will see a list of apps and devices you’ve created App passwords for.
然后,在您的NetworkCredentials中,将密码替换为生成的16位密码.
Then, in your NetworkCredentials, replace your password with the generated 16-digit passcode.
这篇关于SMTP服务器需要安全连接,或者客户端未通过身份验证.服务器响应为:5.5.1需要身份验证.?8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!