且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

邮件不发送bcz身份验证

更新时间:2023-12-04 23:20:58





通过以下网址解决问题:



GMAIL错误:SMTP服务器需要安全连接或客户端未经过身份验证 [ ^ ]



c# - 如何根据新的安全策略在.Net中发送电子邮件? - 堆栈溢出 [ ^ ]



c# - SMTP服务器需要安全连接或客户端未经过身份验证。如果在godaddy上传 - Stack Overflow [ ^ ]



使用ASP.Net中的Gmail SMTP邮件服务器发送电子邮件 [ ^ ]



谢谢,


由于您的代码看起来不错,问题出在身份验证上。我认为您的密码和/或电子邮件地址无效或组合正确。如果不是这样,那么我猜您在Gmail上创建了一个新帐户,这就是为什么Google目前不允许您使用API​​和SMTP来使用该帐户。谷歌做到了; 我在使用我创建的新帐户时遇到了这个问题



另外,请记住始终在可以释放的对象上调用Dispose函数。一个好方法是使用使用块。因此,作为模板,您的代码将如下所示:



  //  您应该使用using语句 
使用(SmtpClient client = new SmtpClient( < smtp-server-address> 25 ))
{
// 配置客户端
client.EnableSsl = true ;
client.Credentials = new NetworkCredential( < username> < password>);
// client.UseDefaultCredentials = true;

// 已创建客户端,现在需要创建MailMessage对象
MailMessage message = new MailMessage(
from@example.com // 来自字段
to@example.com // 收件人字段
Hello // 电子邮件主题
World! // 电子邮件正文
);

// 发送消息
client.Send(消息);

/ *
*由于我使用的是控制台应用程序,这就是为什么我我能够使用Console
*对象,你的框架会有不同的对象。
*实际上不需要这些以下行,如果你愿意,你可以忽略它们
*。 SMTP协议仍会发送您的电子邮件。 * /


// 打印通知消息
Console.WriteLine( 已发送电子邮件。);
// 只是为了暂停应用程序
Console.Read( );
}





我用的是:



1 。传递构造函数中的值;为什么要浪费另一个语句来填充可以在构造函数中填充的值。

2.使用25,这是TCP上的默认SMTP端口。

3.不要告诉SMTP客户端使用默认凭据。它将知道您是否传递了网络凭据对象。



始终确保用户名和密码组合正确,并且您的帐户已配置为与SMTP协议一起使用。您可以在Gmail的设置标签中查看这些设置。为了确保一切,请尝试您的实际帐户,而不是测试帐户。



有关更多信息,请阅读我的文章,它与.NET中的SMTP协议支持以及如何解决一些问题有很多共同之处:通过.NET框架发送电子邮件,以及一般问题 - 使用C#代码 [ ^ ]


I am getting the following error while sending email:

An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code

Additional information: 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



What I have tried:

[HttpPost]
        public ActionResult Index(Class1 objModelMail, HttpPostedFileBase fileUploader)
        {
            if (ModelState.IsValid)
            {
             
                string from = "<email>@gmail.com";
                using (MailMessage mail = new MailMessage(from, objModelMail.To))
                {
                    mail.Subject = objModelMail.Subject;
                    mail.Body = objModelMail.Body;
                    if (fileUploader != null)
                    {
                        string fileName = Path.GetFileName(fileUploader.FileName);
                        mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));
                    }
                    mail.IsBodyHtml = false;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                 
                    NetworkCredential networkCredential = new NetworkCredential(from, "<password>");
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = networkCredential;
                    smtp.Port = 587;

                    smtp.Send(mail);
                    ViewBag.Message = "Sent";
                    return View("Index", objModelMail);
                }
            }
            else
            {
                return View();
            }

        }

Hi,

Go through the below urls to resolve the issue:

GMAIL Error: The SMTP server requires a secure connection or the client was not authenticated[^]

c# - How to send an email in .Net according to new security policies? - Stack Overflow[^]

c# - The SMTP server requires a secure connection or the client was not authenticated. if uploading on godaddy - Stack Overflow[^]

Send email using Gmail SMTP Mail Server in ASP.Net[^]

Thanks,


Since your code looks good and the problem is with authentication. I think your password and/or email address are not valid or correct combination. If not that, then I guess you created a new account on Gmail and that is why, Google is not currently allow you to use API and SMTP calls to use the account. Google does that; I also had this trouble with a new account that I created.

Also, remember to always call Dispose function on your objects that can be released. A good way to do that is using using block. So, just as a template, your code would look like this:

// You should use a using statement
using (SmtpClient client = new SmtpClient("<smtp-server-address>", 25))
{
   // Configure the client
   client.EnableSsl = true;
   client.Credentials = new NetworkCredential("<username>", "<password>");
   // client.UseDefaultCredentials = true;

   // A client has been created, now you need to create a MailMessage object
   MailMessage message = new MailMessage(
                            "from@example.com", // From field
                            "to@example.com", // Recipient field
                            "Hello", // Subject of the email message
                            "World!" // Email message body
                         );

   // Send the message
   client.Send(message);

   /* 
    * Since I was using Console app, that is why I am able to use the Console
    * object, your framework would have different ones. 
    * There is actually no need for these following lines, you can ignore them
    * if you want to. SMTP protocol would still send the email of yours. */
    
   // Print a notification message
   Console.WriteLine("Email has been sent.");
   // Just for the sake of pausing the application
   Console.Read();
}



What I use is:

1. Pass the values in the constructor; why waste another statement just to fill the values that can be filled in the constructor.
2. Use 25, which is the default SMTP port on TCP.
3. Do not tell the SMTP client to not use the default credentials. It will know if you pass the network credentials object.

Always make sure the username and password combination is correct and that your account is configured to be used with SMTP protocols. You can check those settings in the Settings tab of Gmail. To ensure everything, try your actual account and not testing account.

For more, please read my article, it shares much about SMTP protocol support in .NET and how to tackle a few problems: Sending emails over .NET framework, and general problems – using C# code[^]