Error while sending email

Asked

Viewed 119 times

-1

I’m having a boring mistake, I can’t fix it a few days ago.

I searched, but I couldn’t find anything on how to remove it.

Error:

The SMTP server requires a secure connection or the client was not authenticated. Server response was: 5.5.1 Authentication Required. Learn more at

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Net.Mail;

namespace email
{
    public partial class Form1 : Form
    {
        private MailMessage email;
        Stopwatch stop =  new Stopwatch();
        public Form1()
        {
            InitializeComponent();
        }

    private void button1_Click(object sender, EventArgs e)
    {

        email = new MailMessage();
        email.To.Add(new MailAddress(textBox1.Text)); 
        email.From = new MailAddress(textBox3.Text); 
        email.Subject = textBox2.Text; 
        email.IsBodyHtml = true;
        email.Body = textBox5.Text; 
        SmtpClient cliente = new SmtpClient(); 
        cliente.Host = "smtp.gmail.com";
        cliente.Port = 587;
        using (cliente)
        {
            cliente.Credentials  = new System.Net.NetworkCredential(textBox3.Text, textBox4.Text); //credenciais, senha e email
            cliente.EnableSsl = true;
            cliente.Send(email); 
        }
        MessageBox.Show("email enviado");

    }


}

}

1 answer

0

Probably the screen-informed user and password are not being effectively considered for authentication on the SMTP server.

Try to complement by setting the property Usedefaultcredentials for false:

using (SmtpClient cliente = new SmtpClient())
{
  cliente.Credentials  = new System.Net.NetworkCredential(textBox3.Text, textBox4.Text); 
  cliente.Host = "smtp.gmail.com";
  cliente.Port = 587;
  cliente.EnableSsl = true;
  cliente.UseDefaultCredentials = false; // Aqui!!
  cliente.Send(email); 
}
  • It didn’t work either, man. I tried switching to the console and even made another form, even putting the right credentials, this error happens.

  • What version of . NET are you using? And what is the operating system of the machine you are running the application on? Maybe it’s a matter of TLS version...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.