Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
This looks pretty simple but I am not able to get it working.
I am trying to send an email from my application. A simple Enquiry form.
It takes user's address, name, contact no etc and sends an email to Customer's email id.
Below is the code.
C#
SmtpClient client = new SmtpClient("ip address");
client.EnableSsl = true;
client.Timeout = 20000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("user@domain.com", "password"); //webmail.domain.com
MailMessage msg = new MailMessage();
msg.To.Add(txtEmailId.Text);
msg.From = new MailAddress("user@domain.com");
msg.Subject = "Test";
msg.Body = txtComments.Text;
client.Send(msg);

This gives an error, "Operation timed out." So I increased the timeout from 20000 to 50000.
But no luck.

The customer uses webmail.
The code has been written by other developers which I am not sure was working previously also.
I am able to ping the ip address given for SMTP server.

Any help in this would be of great help.

Thanks,
Lok

What I have tried:

I tried changing the Timeout duration. But I got another error. "Failure sending mail. Unable to connect to remote server."
Posted
Updated 24-Jul-17 8:09am
Comments
David_Wimbley 24-Jul-17 11:56am    
Chances are you need to look at what the port number is for the target server along with whether or not it uses SSL. That would be the first place i start.
Jochen Arndt 24-Jul-17 12:11pm    
Default SmtpClient port is 25. Nearly all SMTP servers use nowadays secure transport with port 587. It is very probably that this is the case here because you will get a timeout when no service is listening on the used port-

You have to ask the administrator of the server or check the documentation which port is used (and which authentication methods).
Rob Philpott 24-Jul-17 12:50pm    
Ah, but I understand SMTPClient doesn't support that - only an initial unencrypted connection promoted to secure by the use of STARTTLS.
Rob Philpott 24-Jul-17 12:50pm    
Sorry, mouse error, this was intended for comment above. Enough for one day, I'm going home!
ZurdoDev 24-Jul-17 12:26pm    
Look at what smtp server you are trying to connect to.

1 solution

Hi,

How you ping your server, it is working.
Try the function below. It's fine.
The code is very similar with your code, but I don't use some property that you use.
I use it on all projects in my work.

C#
public static string SendEmail(string strFrom, string strTo, string strCc, string strBcc, string strSubject, string strBody, string strAttach)
    {
        
      
  
      string strResults = "";
      MailMessage objEmail = null;
      SmtpClient objSmtp;
      try
      {
          objEmail = new MailMessage();
          //from email
          objEmail.From = new MailAddress(strFrom);
          objEmail.To.Add(strTo.Replace(";", ","));// To add more than recipient, separete for comma. 

	  //ocult copy 	
          if (strBcc != "")
              objEmail.Bcc.Add(strBcc.Replace(";", ","));// To add more than recipient, separete for comma. 

          //prioridade do email
          objEmail.Priority = MailPriority.Normal;

          //use true to enable html email, or false, to only text 
          objEmail.IsBodyHtml = true;
          objEmail.Subject = strSubject;    
          objEmail.Body = strBody;
          
          objEmail.SubjectEncoding = Encoding.GetEncoding("ISO-8859-1");
          objEmail.BodyEncoding = Encoding.GetEncoding("ISO-8859-1");

          //verify if you have attachment file
          if (strAttach != "")
          {
              Attachment objitemfile = new Attachment(strAttach);
              objEmail.Attachments.Add(objitemfile);
          }


          //create smtp client
          objSmtp = new SmtpClient();
          //add email server address 
          objSmtp.Host = "myserveremail@domain.com";

          //e-mail server authentication
          //objSmtp.Credentials = new NetworkCredential("username", "password");

          //send e-mail
          objSmtp.Send(objEmail);
          strResults = "OK";
      
      }
      catch (System.Exception erro)
      {
          strResults = " 1ª - HelpLink ==> " + erro.HelpLink;
          strResults += "\n\n 2ª - InnerException = " + erro.InnerException;
          strResults += "\n\n 3ª - Message = " + erro.Message;
          strResults += "\n\n 4ª - Source = " + erro.Source;
          strResults += "\n\n 5ª - StackTrace = " + erro.StackTrace;
          strResults += "\n\n 6ª - TargetSite = " + erro.TargetSite;
      }
      finally
      {
          objSmtp = null;
          objEmail.Dispose();
      }

      return strResults;
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900