2
protected void Button1_Click(object sender, EventArgs e)
    {
        SmtpClient mail = new SmtpClient();
        mail.UseDefaultCredentials = false;
        mail.Host="smtp.gmail.com";
        mail.Port=465;
        mail.Credentials=new NetworkCredential("b.soham1991","*******");
        mail.DeliveryMethod = SmtpDeliveryMethod.Network;
        mail.EnableSsl = true;

        mail.Timeout = 1000000;

        mail.Send("b.soham1991@gmail.com", "soham.elf@gmail.com", "Hi", "via gmail");
        Response.Write("success!");
    }

Reponse.Write is never exeuted and eventually connection is timed out. what am I missing?

Mad Dog Tannen
  • 7,129
  • 5
  • 31
  • 55
  • Are you sure that the reason it is not executed is because it's not working? Because if you somehow failed to send the mail and are waiting for the 1,000,000 ms timeout, it could also be a big problem. – Voidpaw Nov 26 '13 at 10:33

2 Answers2

1

I think you are using the wrong port.

protected void Button1_Click(object sender, EventArgs e)
{
    SmtpClient mail = new SmtpClient();
    mail.UseDefaultCredentials = false;
    mail.Host="smtp.gmail.com";
    mail.Port=587; //new port!
    mail.UseDefaultCredentials = false,
    mail.Credentials=new NetworkCredential("b.soham1991","*******");
    mail.DeliveryMethod = SmtpDeliveryMethod.Network;
    mail.EnableSsl = true;

    mail.Timeout = 1000000;

    mail.Send("b.soham1991@gmail.com", "soham.elf@gmail.com", "Hi", "via gmail");
    Response.Write("success!");
}

Additionally, be sure to use System.Net.Mail and not System.Web.Mail

Refer to the answer here for a detailed and refined answer.

Community
  • 1
  • 1
Marco
  • 22,856
  • 9
  • 75
  • 124
  • You're welcome. Please accept my post as the answer by clicking the check symbol left to my post – Marco Nov 26 '13 at 10:37
0

You should try to assign to a label for instace.

Add a label to the form.

The you can simply.

labelstatus.text = "success!";

Can you maybe add Exception handler to this to see exactly where it goes wrong in case the mail object never completes?

Mad Dog Tannen
  • 7,129
  • 5
  • 31
  • 55