Unable to send mail over smtp server, as the 'operation timeout' error occurs

user3391912 picture user3391912 · Mar 7, 2014 · Viewed 12.9k times · Source

Unable to send mail over smtp server, as the 'operation timeout' error occurs. I have tried almost all the methods given on this website, have tried using another port number and also the client other than gmail, I wonder what the problem is. Following is the attached code segment.

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Security;
using System.Net.Mail;
using System.Net;

public partial class home : System.Web.UI.Page
{
    SmtpClient smtp = new SmtpClient();

    MailMessage newMail = new MailMessage();

    protected void Page_Load(object sender, EventArgs e)
    {   
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 465;
        smtp.Credentials = new NetworkCredential("[email protected]", "password");
        smtp.UseDefaultCredentials = false;
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.EnableSsl = true;

        newMail.From = new MailAddress("[email protected]");
        newMail.Priority = MailPriority.High;
    }
    protected void Button_send_Click(object sender, EventArgs e)
    {
        try
        {
            newMail.To.Add(new MailAddress(TextBox_to.Text));
            newMail.Subject = TextBox_sub.Text;
            newMail.Body = TextBox_mail.Text;
            smtp.Send(newMail);
            Label_msg.Text = "Message Sent Successfully.";
            Label_msg.Visible = true;
        }
        catch (Exception ex)
        {
            Label_msg.Text = "Message not sent <br/>" + ex.Message;
        }
    }
}

Answer

Nipun Ambastha picture Nipun Ambastha · Mar 7, 2014

Please try with port number 587.

enter image description here