0

When I remove the comment "//stmp.timeout" it gives timeout error. What should I do to fix that?

Here is my code:

public ActionResult Index(EmailModel model)
{
    var smtp = new System.Net.Mail.SmtpClient();
    {
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 180;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtp.Credentials = new NetworkCredential("sheikh.abm@gmail.com ","somepassword");
        //smtp.Timeout = 20000;
    }
    try
    {
        smtp.Send("sheikh.abm@gmail.com",model.To, model.Subject, model.Message);
        return View("Index");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);   //Should print stacktrace + details of inner exception
         if (ex.InnerException != null)
         {
             Console.WriteLine("InnerException is: {0}", ex.InnerException);
         }
    }
    return View("Index");
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Arslan Bilal
  • 625
  • 2
  • 9
  • 17

1 Answers1

1

In this line

smtp.Credentials = new NetworkCredential("sheikh.abm@gmail.com ","somepassword

there is a space after gmail.com. This could prevent the login to gmail. Remove it.
Also, I think that the port used to send mail to gmail is 465 for SSL or 587 for TLS/STARTTLS.

smtp.Port = 465;   
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Nice CATCH! :) I didn't pay that close of attention. – Henry Harris Jul 25 '12 at 21:19
  • after seting the port to 465 it gives me exception operation has been timed out – Arslan Bilal Jul 25 '12 at 21:30
  • There is a complete question/answer on this site. Instead of duplicating the answers I will give you [this link](http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail). Hope it helps. – Steve Jul 25 '12 at 21:36