简体   繁体   中英

SMTP Email not sending

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("myemail@gmail.com","mypassword");
        client.Send("myemail@gmail.com","myemail@gmail.com","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?)

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@gmail.com", "password")
            };


            using(MailMessage gMessage = new MailMessage("email@gmail.com", "email@gmail.com", "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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM