Priyank Patel
Priyank Patel

Reputation: 7006

Error sending mail from Asp.Net/C#

I have built a feedback page.I am trying to send mail from the page.The client provided me with the SMTP Server name , Email ID and Password.But when I try to send mail from the page I am getting following error.

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 64.37.118.141:25

Here is my code

MailMessage feedBack = new MailMessage();

//feedBack.To.Add("master@myclient.com");
feedBack.From = new MailAddress("master@myclient.com");
feedBack.Subject = "Mail from Explor Corporate Website.";  
feedBack.Body = "Sender Name: " + Name.Text + "<br/><br/>Sender Last Name:"+LastName.Text+"<br/><br/>Sender Company:"+Company.Text+"<br/><br/>Sender Designation"+Designation.Text+"<br/><br/>Sender Email:"+Email.Text+"Sender Phone No:"+ PhoneNo.Text+"Sender Enquiry:"+Enquiry.Text;
feedBack.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.myclient.com"; //Or Your SMTP Server Address
//smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("master@myclient.com", "*****");
//Or your Smtp Email ID and Password

smtp.Send(feedBack);

Can anyone help me out with this?

Any suggestions are welcome.

Upvotes: 1

Views: 1890

Answers (2)

DaveB
DaveB

Reputation: 9530

Sounds like a possible timeout error or authentication problem. When accessing external resources from code you will need to handle possible errors:

try
{
 smtp.Send(feedBack); 
}
catch(Exception e)
{
    // Handle the exception here
}

Upvotes: 0

Mike Dinescu
Mike Dinescu

Reputation: 55760

This most likely indicates you don't have either the correct host name, or the correct port.

If you are using SSL then the default SMTP port which is 25 will not work. I noticed that you did have a line setting the port what was commented out.

Try setting the port to 587 and if that doesn't work, try port 465 which is sometimes used for secure SMTP.

If that doesn't work either confirm with your client the correct port and host name..

And another thing you may want to double check is whether the SMTP server you are using does indeed support/require SSL. If it does not support SSL that you will need to set the EnableSSL option to false and try connecting on the default port of 25 (or whatever port they are using).

Upvotes: 3

Related Questions