SMTP Email not sending

285 views Asked by At

I made a simple program to send e-mails, but it fails to send them and gives me an exception.

Here's the code:

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

public class test{
    public static void Main()
    {
        SmtpClient client = new SmtpClient();
        client.Host = "smtp.gmail.com";
        client.Port = 465;
        client.EnableSsl = true;
        client.Credentials = new NetworkCredential("[email protected]","mypassword");
        client.Send("[email protected]","[email protected]","Test","Test");
    }
}

NOTE: Just added the following lines of code:

client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;

and changed the port to 587, then it says that authentication is required, and I alredy put my credentials, maybe its because the port 587 needs TLS not SSL (from what I learned searching about this today, I know that SSL is an "evolution" of TLS, no?)

1

There are 1 answers

0
Thiago Avelino On

Tunigamer, just tried the code below and that worked:

SmtpClient gmailClient = new SmtpClient {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential("[email protected]", "password")
            };


            using(MailMessage gMessage = new MailMessage("[email protected]", "[email protected]", "Test", "Test")) {

                try {
                    gmailClient.Send(gMessage);
                } catch(Exception) { }
            }

Also, I received this notification below from GMAIL, you will need to disable the sign-in security features of your account:

enter image description here