Click here to Skip to main content
15,914,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

my code as below,

C#
public void SendMaiL(string sSubject, string[] arToAddress, string[] arToAddressCC, int? iReqEmpId, int iMailType)
  {
      string sBody = string.Empty;
      bool enableSSL = true;

      if (arToAddress != null && arToAddress.Length > 0)
      {
          try
          {
              string sServer = "smtp.mail.com";
              int iPort =587;
              string sUserName = ConfigurationManager.AppSettings["userName"].ToString();
              string sPassword = ConfigurationManager.AppSettings["password"].ToString();
              string sFrom = ConfigurationManager.AppSettings["from"].ToString();

              //oSmtpClient.UseDefaultCredentials = false;
              //oSmtpClient.Credentials = new NetworkCredential(sUserName, sPassword);

              MailMessage oMailMessage = new MailMessage(); ;
              oMailMessage.From = new MailAddress(sFrom);
              if (arToAddress != null && arToAddress.Length > 0)
              {
                  sBody = GetMailBody(Convert.ToInt32(arToAddress[0]), iMailType, iReqEmpId);
                  for (int t = 0; t <= arToAddress.Length - 1; t++)
                  {
                      oMailMessage.To.Add(new MailAddress(GetEmpMailId(Convert.ToInt32(arToAddress[t].ToString()))));
                  }
              }
              if (arToAddressCC != null && arToAddressCC.Length > 0)
              {
                  for (int i = 0; i <= arToAddressCC.Length - 1; i++)
                  {
                      if (arToAddressCC[i] != null)
                          oMailMessage.CC.Add(new MailAddress(GetEmpMailId(Convert.ToInt32(arToAddressCC[i].ToString()))));
                  }
              }
              oMailMessage.Subject = sSubject;
              oMailMessage.Body = sBody;
              oMailMessage.IsBodyHtml = true;

              using (SmtpClient smtp = new SmtpClient(sServer, iPort))
              {
                  smtp.Credentials = new NetworkCredential(sFrom, sPassword);
                  smtp.EnableSsl = enableSSL;
                  smtp.Send(oMailMessage);
              }

          }
          catch (Exception ex)
          {
              //WriteLog(ex.ToString());
          }
      }
  }


But getting error smtp exception was caught.Failure sending mail. Unable to connect to the remote server.

:(

thanks..
Posted
Comments
Deepu S Nair 19-Nov-14 5:57am    
The Gmail SMTP server settings used for sending mail through Gmail from any
program :

Gmail SMTP server address: smtp.gmail.com

and i think it causes you the rror

Then the obvious question has to be, "is that address correct?".
C#
string sServer = "smtp.mail.com";

Did you actually mean to type:
C#
string sServer = "smtp.gmail.com";
 
Share this answer
 
Comments
hasbina 20-Nov-14 3:06am    
@Richard
sir, actually it was the mistake in my question. I am using
string sServer = "smtp.gmail.com". But getting the same issue. :(

thanks..
Richard MacCutchan 20-Nov-14 3:23am    
Then it sounds as if something (maybe your firewall) is blocking you from connecting to gmail. Check Krishna's suggestions below.
Yeah..common exception. I have encountered this. This may be caused due to

1) Your antivirus blocking mail sending. I use Mcafee and I had to disable access protection when you send mail. Let me know if you use this for further answers.

2) Check your port number. Gmail supports 25, 587, 465. I am using 25 and 587. Either of them work for me.

3) Try to login to gmail in your target browser. If your app is running in chrome, try to login to gmail in chrome and re-run your app :) Google asks two step authentication while you try to run gmail from an undetected device and logging in prior fixes it.

4) Verify your credentials also :D

Hope this should fix this problem. This is how I have fixed it in my servers. Happy to help you if you have issues still.
 
Share this answer
 
v2

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