0

Below is my code of sending email from my mail but I am getting an error please help !

Error: Email not sent!System.Net.Mail.SmtpException: The operation has timed out. at System.Net.Mail.SmtpClient.Send(MailMessage message) at _Default.Button1_Click(Object sender, EventArgs e)

try{

        MailMessage mailmessage = new MailMessage();
        mailmessage.To.Add(TextBox3.Text);
        mailmessage.From=new MailAddress("sadiazar05@gmail.com");
        mailmessage.Subject = "User SignUp";
        mailmessage.Body = "Hello You're registered!";
        SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",465);
        mailmessage.Priority = MailPriority.High;
        smtpclient.Timeout = 60000;
        smtpclient.Send(mailmessage);
        Response.Write("Email sent successfully!");
        }
        catch(Exception exp)
        {
            Response.Write("Email not sent!" +exp);
        }
    }
Ariful Haque
  • 3,662
  • 5
  • 37
  • 59
sadia zar
  • 62
  • 1
  • 8

4 Answers4

0

You can try to use port 25 and if it doesn't work you can try port 587:

SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",The Other Port);
israel altar
  • 1,768
  • 1
  • 16
  • 24
0

try this code and enable SSL.

SmtpClient client = new SmtpClient("smtp.gmail.com", 587);           
 client.EnableSsl = true;
 MailAddress from = new MailAddress("YourGmailUserName@gmail.com", "[ Your full name here]");           
 MailAddress to = new MailAddress("your recipient e-mail address", "Your recepient name");
 MailMessage message = new MailMessage(from, to);
 message.Body = "This is a test e-mail message sent using gmail as a relay server ";
 message.Subject = "Gmail test email with SSL and Credentials";
 NetworkCredential myCreds = new NetworkCredential("YourGmailUserName@gmail.com", "YourPassword", "");           
 client.Credentials = myCreds;
 client.Send(message);
jtabuloc
  • 2,479
  • 2
  • 17
  • 33
0

You can try like this:

try{
    // message details
    MailMessage mailmessage = new MailMessage();
    mailmessage.To.Add(TextBox3.Text);
    mailmessage.From=new MailAddress("sadiazar05@gmail.com");
    mailmessage.Subject = "User SignUp";
    mailmessage.Body = "Hello You're registered!";      
    mailmessage.Priority = MailPriority.High;
    //smtp Client details
    smtpclient.UseDefaultCredentials = False
    smtpclient.Credentials = New Net.NetworkCredential("email", "password")// here you have to give your username and password
    smtpclient.Port = 587 // default port for gmail
    smtpclient.EnableSsl = True
    smtpclient.Host = "smtp.gmail.com"
    smtpclient.Timeout = 60000;
    smtpclient.Send(mailmessage);
    Response.Write("Email sent successfully!");
    }
    catch(Exception exp)
    {
        Response.Write("Email not sent!" +exp);
    }
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

587 is Gmail Port

smtpclient.Port = 587

SMTP by default uses TCP port 25. The protocol for mail submission is the same, but uses port 587. SMTP connections secured by SSL, known as SMTPS, default to port 465 (nonstandard, but sometimes used for legacy reasons).

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
Eng Mohamed Attian
  • 97
  • 1
  • 2
  • 10
  • didn't work it says that The SMTP server requires a secure connection or the client was not authenticated – sadia zar Aug 31 '15 at 06:17