Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to implement automatic email functionality for a website I developed. On initial basis I tested email code with Gmail credentials and it worked fine. Below is the code which is working fine.
C#
public static int SendEmail(string from, string to, string subject, string body)
  {
      int result = 0;
      MailMessage msg = new MailMessage();
      MailMessage mail = new MailMessage();
      SmtpClient SmtpServer = new SmtpClient();
      SmtpServer.Credentials = new System.Net.NetworkCredential("MyEmailID@gmail.com", "MyGmailPassword");
      SmtpServer.Port = 587;
      SmtpServer.Host = "smtp.gmail.com";
      SmtpServer.EnableSsl = true;
      mail = new MailMessage();
      try
      {
          mail.From = new MailAddress(from, "", System.Text.Encoding.UTF8);
          mail.To.Add(to);
          mail.Subject = subject;
          mail.IsBodyHtml = true;
          mail.Body = body;
          mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
          mail.ReplyToList.Add("MyEmailID@gmail.com");
          SmtpServer.Send(mail);
          result = 1; //--------- MESSAGE SENT SUCCESSFULLY
      }
      catch (Exception ex)
      {
          result = 2; //--------- ERROR OCCURRED WHILE SENDING MESSAGE
      }
      return result;
  }


When I used client's email settings with above code, it is not working. Client has provided me with below information.

Official EmailID/Password: Two different email address and their passwords
SMTP:
Outgoing Server: Two different server for SSL and NON SSL settings
SMTP PORT: 587 and 465

I tried all the possible combination of email/password, SMTP port..etc but none is working.

I am getting two kinds of error with these combinations...

err 1: System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
err 2: The operation has timed out.

But I am able to login on webmail with the credentials provided, so I guess both the emailIDs, and their respective passwords are correct.

Any help would be appreciated.

Thanks and regards,

Sam
Posted

 
Share this answer
 
v2
It worked, I added System.Net Namespace along with System.Net.Mail namespace...
 
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