0

Below is the code I'm using. Pls find the error message below. Please advise on how this can be rectified.

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);
    }
}}

Error message below

System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. Learn more at at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message)

DaImTo
  • 88,623
  • 26
  • 153
  • 389

2 Answers2

1

You should enable the less secure app settings on the Gmail account you are using to send emails. You can use this Link to enable the settings.

More information about your problem here.

Andrei Solero
  • 792
  • 1
  • 3
  • 12
  • it's work. But why the "Allow less secure apps" became OFF automatically. I had set it to ON but now it became OFF, why it's happening? – SAGAR SAHA Feb 02 '22 at 16:56
1

The SMTP server requires a secure connection or the client was not authenticated.

To fix this error Go to your google account and generate an apps password

When running your code use the password generated instead of the actual users password.

This happens most often when the account has 2fa enabled.

Example with SmtpClient

using System;
using System.Net;
using System.Net.Mail;

namespace GmailSmtpConsoleApp
{
    class Program
    {
        private const string To = "test@test.com";
        private const string From = "test@test.com";
        
        private const string GoogleAppPassword = "XXXXXXXX";
        
        private const string Subject = "Test email";
        private const string Body = "<h1>Hello</h1>";
        
        
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            
            var smtpClient = new SmtpClient("smtp.gmail.com")
            {
                Port = 587,
                Credentials = new NetworkCredential(From , GoogleAppPassword),
                EnableSsl = true,
            };
            var mailMessage = new MailMessage
            {
                From = new MailAddress(From),
                Subject = Subject,
                Body = Body,
                IsBodyHtml = true,
            };
            mailMessage.To.Add(To);

            smtpClient.Send(mailMessage);
        }
    }
}
DaImTo
  • 88,623
  • 26
  • 153
  • 389