locked
Error - 'The SMTP server requires a secure connection or the client was not authenticated RRS feed

  • Question

  • User1979860870 posted
    protected void sendEmail(string address, string subject, string from, string body)
            {
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                message.To.Add(address);
                message.Subject = subject;
                message.From = new System.Net.Mail.MailAddress(from);
                message.Body = body;
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
                smtp.Credentials = new System.Net.NetworkCredential("test@gmail.com", "xxxx");
    
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.Send(message);
    
            }


    Hi

    <div>protected void sendEmail(string address, string subject, string from, string body)</div> <div>        {</div> <div>            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();</div> <div>            message.To.Add(address);</div> <div>            message.Subject = subject;</div> <div>            message.From = new System.Net.Mail.MailAddress(from);</div> <div>            message.Body = body;</div> <div>            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");</div> <div>            smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "XXXXX");</div> <div>            smtp.Port = 587;</div> <div>            smtp.EnableSsl = true;</div> <div>            smtp.Send(message);</div> <div> </div> <div>        }</div>

    Thanks

    Wednesday, December 30, 2020 2:32 AM

All replies

  • User-939850651 posted

    Hi jagjit saini,

    I'm not sure if you have modified the setting in Less secure app access setting to "turn on", so that you can use gmail to send mail in third-party applications.

    According to the code you provided, you also need to set UseDefaultCredentials property to "false", something like this:

    protected void sendEmail(string address, string subject, string from, string body)
            {
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                message.To.Add(address);
                message.Subject = subject;
                message.From = new System.Net.Mail.MailAddress(from);
                message.Body = body;
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
                smtp.Credentials = new System.Net.NetworkCredential("test@gmail.com", "xxxx");
    
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.UseDefaultCredentials = false;
                smtp.Send(message);
    
            }

    Hope this can help you.

    Best regards,

    Xudong Peng

    Thursday, December 31, 2020 2:41 AM