0

I am trying to send a mail the form posts, but when the form posts it tries to send the mail and then times out. the code that is timing out is this:

    MailMessage mail = new MailMessage();
    mail.From = new MailAddress("someone@example.com");
    mail.To.Add("someoneElse@example.com");

    SmtpClient smtp = new SmtpClient();
    smtp.Port = 465;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.UseDefaultCredentials = false;
    smtp.Host = "smtp.gmail.com";

    mail.Subject = "Hello";
    mail.Body = "World!";
    smtp.Send(mail);
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Joris van Roy
  • 183
  • 1
  • 1
  • 12
  • What does "times out" mean? Is there an exception? What is its full text, including the call stack? Is it an exception from IIS or the email client? Are you **sure** these settings will allow you to send via gmail? Try this code in a unit test first, until you manage to send an email, then put it in your web page – Panagiotis Kanavos Nov 03 '15 at 09:42
  • Also please check the many SO questions about sending emails via gmail. I'm almost certain this has already been answered – Panagiotis Kanavos Nov 03 '15 at 09:43
  • @PanagiotisKanavos this is the error: Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Net.Mail.SmtpException: The operation has timed out. – Joris van Roy Nov 03 '15 at 09:46
  • First, that's only the message, not the full exception (call ToString()). Second, network timeouts typically occur because the address or port are wrong. And I do thing Gmail dropped SSL (port 465) support for TLS (587) some years ago – Panagiotis Kanavos Nov 03 '15 at 10:06

1 Answers1

0

This is almost certainly an issue with your client setup.

If you're using port 465, you're required to use SSL - See here https://support.google.com/a/answer/176600?hl=en In your current client setup this is omitted.

Also, I think you're missing your credentials.

Try changing your client to

SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true; //ssl is required
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("your-email@gmail.com", "YourPassword");
client.Timeout = 20000; //increase the timeout

As mentioned in the comments, you may also need to allow "less secure apps" access to your account -

Info here http://support.google.com/accounts/answer/6010255

Alex
  • 37,502
  • 51
  • 204
  • 332
  • This may not be enough [as shown here](http://stackoverflow.com/questions/31231033/using-smtpclient-to-send-an-email-from-gmail), the OP may have to allow less secure apps to connect – Panagiotis Kanavos Nov 03 '15 at 10:06
  • @PanagiotisKanavos have added this to the answer, although I've never had to do that – Alex Nov 03 '15 at 10:09
  • Well, someone else [just found](http://stackoverflow.com/questions/33496290/how-to-send-email-by-using-mailkit) that his account does need that. – Panagiotis Kanavos Nov 03 '15 at 10:16
  • hmm, that's different though - the OP is getting a time out, that particular question is getting Authentication failed – Alex Nov 03 '15 at 10:19