下面是我正在使用的代码。请告知如何纠正此问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Net.Mail;
public partial class mailtest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SendEmail(object sender, EventArgs e)
{
lblmsg.Text = "";
try
{
using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
{
mm.Subject = txtSubject.Text;
mm.Body = txtBody.Text;
//if (fuAttachment.HasFile)
//{
// string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
// mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
//}
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = txtsmtphost.Text.Trim();
smtp.EnableSsl = false;
if (chkssl.Checked == true)
{
smtp.EnableSsl = true;
}
NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = int.Parse(txtport.Text);
smtp.Send(mm);
lblmsg.ForeColor = System.Drawing.Color.DarkGreen;
lblmsg.Text = "Email sent successfuly !!";
//ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent successfuly !!');", true);
}
}
catch (Exception ex)
{
lblmsg.ForeColor = System.Drawing.Color.Red;
lblmsg.Text = "Failed " + ex.ToString();
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Failed');", true);
}
}}
错误信息是:
System.Net.Mail.SmtpException: SMTP 服务器需要安全连接或客户端未经身份验证。服务器响应是:5.7.0 需要身份验证。要了解更多信息,请访问 System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, BooleanallowUnicode) at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)。 Mail.SmtpTransport.SendMail(MailAddress发件人,MailAddressCollection收件人,字符串deliveryNotify,布尔allowUnicode,SmtpFailedRecipientException&例外)在System.Net.Mail.SmtpClient.Send(MailMessage消息)
由于这个问题一直是 Google 上的第一个问题,所以今天(2022 年 6 月 11 日)对我有用的内容就是我将分享的内容。这个问题已经以类似的方式被问过很多次,并且许多不同的事情对很多人都有效,但是现在停止了,“代码”中不再有解决办法(假设您的代码是完美的,但抛出上面标题中所写的异常)!为什么因为谷歌做了改变(从https://support.google.com/accounts/answer/6010255 https://support.google.com/accounts/answer/6010255):
安全性较低的应用和您的 Google 帐户:
为了帮助保护您的帐户安全,从 2022 年 5 月 30 日起,Google 不再支持使用要求您仅使用用户名和密码登录 Google 帐户的第三方应用或设备。
重要提示:此截止日期不适用于 Google Workspace 或 Google Cloud Identity 客户。这些客户的强制执行日期将于稍后在 Workspace 博客上公布。
现在你做什么,比你想象的更简单,去https://myaccount.google.com/ https://myaccount.google.com/,点击“安全”,启用两步验证,完成后,返回myaccount安全页面,您将看到“应用程序密码”,打开它,它会给您提供您要为其设置密码的选项(一堆设备),选择“Windows计算机”,创建一个密码,复制它,在NetworkCredential
class 对象,并且您的电子邮件应该通过 Code 再次开始工作。
昨天这浪费了我很多时间,我的 Winforms 应用程序运行得很好,直到几天前它停止发送电子邮件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)