1

I need to send an email from my program I wrote this code but it did not work the error was

" Le serveur SMTP requiert une connexion sécurisée ou le client n'était pas authentifié. La réponse du serveur était : 5.7.0 Must issue a STARTTLS command first. e2sm19845644wix.15 - gsmtp "

English Translation:

"The SMTP server requires a secure connection or the client was not authenticated The server response was : 5.7.0 Must issue a STARTTLS command first e2sm19845644wix.15 - gsmtp"

private void button3_Click(object sender, EventArgs e)
{
    try
    {   
         MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

         SmtpServer.Credentials = new System.Net.NetworkCredential("oussabdalla@gmail.com", "xxx");
        mail.From = new MailAddress("oussabdalla@gmail.com");
        mail.To.Add(textBox4.Text);
        mail.Subject = "Attention";
        mail.Body = textBox1.Text + textBox2.Text + textBox3.Text + "est absent de " + comboBox5.SelectedValue + "a" + comboBox4.SelectedValue;

        SmtpServer.Port = 587;



        SmtpServer.Send(mail);
        SmtpServer.EnableSsl = true;
        MessageBox.Show("mail Send");

    }
    catch (Exception ex)  
    {
        MessageBox.Show(ex.ToString());
    }
}

I changed my code following recommendations and got a new error

" Le délai d'attente de l'opération a expiré "

Translated:

"The transaction timeout has expired"

private void button3_Click(object sender, EventArgs e)
{
        try
        {
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            MailMessage mail = new MailMessage();
            SmtpServer.Credentials = new System.Net.NetworkCredential("oussabdalla@gmail.com", "xxxxx");
            SmtpServer.UseDefaultCredentials = false;

        mail.From = new MailAddress("oussabdalla@gmail.com");
        mail.To.Add(textBox4.Text);
        mail.Subject = "Attention";
        mail.Body = textBox1.Text + textBox2.Text + textBox3.Text + "est absent de " + comboBox5.SelectedValue + "a" + comboBox4.SelectedValue;

        SmtpServer.Port = 465;

        SmtpServer.EnableSsl = true;

        SmtpServer.Send(mail);

        MessageBox.Show("mail Send");

    }
    catch (Exception ex)  
    {
        MessageBox.Show(ex.ToString());
    }
}
James
  • 9,774
  • 5
  • 34
  • 58

2 Answers2

3

You need to set SmtpServer.EnableSsl = true; before you call SmtpServer.Send(mail); in order to be effective.

With my limited knowledge of french, I'd say the message asks you to use a secure connection using STARTTLS.

Google says, when you use the port 587, you need to use TLS, as stated here. So maybe you consider using port 465, which indicates usage of SSL instead of the 587-TLS-port.

Here is another post that explains well along an example how to send emails via gmail. It makes also clear, that you need to set UseDefaultCredentials = false before you assign your actual Credentials!

Community
  • 1
  • 1
Patrik
  • 1,355
  • 12
  • 22
2
try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("fromeamilp@gmail.com");
                mail.To.Add("toemail@gmail.com");
                mail.Subject = "Subject of email...";
                mail.Body = "Body of email...";
                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("gmailusername", "gmailpass");
                SmtpServer.EnableSsl = true;
                SmtpServer.Send(mail);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
Ferid Š. Sejdović
  • 998
  • 4
  • 18
  • 48