I'm trying to connect to my Gmail account through SmtpClient
but it seems to not work as should. I specify port 465, enable SSL and define everything, but it takes like 2 minutes and then just shows some error that the message wasn't sent.
What am I doing wrong here?
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("myemail@gmail.com);
msg.To.Add(new MailAddress("theiremil@email.com));
msg.Subject = "This is the subject";
msg.Body = "This is the body";
SmtpClient sc = new SmtpClient("smtp.gmail.com", 465);
sc.EnableSsl = true;
sc.UseDefaultCredentials = false;
sc.Credentials = new NetworkCredential("myemail@gmail.com", "pass");
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
sc.Send(msg);
erroremail.Text = "Email has been sent successfully.";
}
catch (Exception ex)
{
erroremail.Text = "ERROR: " + ex.Message;
}