tags:

views:

858

answers:

3

Hello, When I try to send E-mail using C# with gmail's smtp server,I get this error..

"The remote certificate is invalid according to the validation procedure".

SSL is enabled

Port used is 587

server name used is "Smtp.gmail.com".

username and password is correct

outlook express works fine on the same pc with the same settings

The c# program also works fine in other places...we get this error only in the clients place.

Would appreciate any help..

Thanks

Edit: @Andomar,Where do I find the root certificates in the client? How do I fix this?

@Alnitak,How do I issue starttls using System.Net.Mail library though?

@David,What do I pass as parameter for "(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)"

Thanks David. I've added those lines. But,I'm still confused about whats going on since this code doesn't have any direct connection with System.Net.Mail as far as my understanding.Hope the problem goes away.

+1  A: 

Check if the proper root certificates are in the client's store. And the client's system date is correct.

Andomar
Where to check certificates etc? I'm not using any certificates according to my C# code. I've used the classes in System.Net.Mail
Josh
SSL = certificates
Chad Grant
Since it works elsewhere and not the client, I would bet one US dollar that the client does not know about your CA, so you need to install the certificate/CA at the client
Chad Grant
yes I know SSL uses certificates....But I think the System.Net.Mail library handles them...
Josh
How to install certificate at the client?
Josh
How do I install the certificate at the client? how?
Josh
You can check the certificates in Control Panel -> Internet Options -> Content -> Certificates.
Andomar
How to installing a certificate? Just double-click it. If you make David's ValidateServerCertificate return true, your client will accept any server certificate (obviously insecure, but works.)
Andomar
+1  A: 

Are you using STARTTLS or assuming that the connection on port 587 is SSL encrypted from the outset?

Google's servers on smtp.gmail.com require STARTTLS.

Alnitak
I'm using System.Net.Mail.Is there a way to issue STARTTLS using System.Net.Mail?
Josh
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx suggests that STARTTLS is the default if SmtpClient.EnableSsl == true
Alnitak
+2  A: 

Also check that the root certificates are in the Client's Trusted Root Authority store. If this is from a service then adding the root certificates to the Local Machine store may also help. To get a better grasp of the reason then I have found the following policy helpful...

public bool ValidateServerCertificate( object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
// No errors so continue…
if (sslPolicyErrors == SslPolicyErrors.None)
       return true;

// I’m just logging it to a label on the page, 
//  this should be stored or logged to the event log at this time. 
lblStuff.Text += string.Format("Certificate error: {0} <BR/>", sslPolicyErrors);

// If the error is a Certificate Chain error then the problem is
// with the certificate chain so we need to investigate the chain 
// status for further info.  Further debug capturing could be done if
// required using the other attributes of the chain.
      if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors)
      {
       foreach (X509ChainStatus status in chain.ChainStatus)
       {
             lblStuff.Text += string.Format("Chain error: {0}: {1} <BR/>", status.Status, status.StatusInformation);
  }
}

// Do not allow this client to communicate 
//with unauthenticated servers.
      return false;
}

To add the policy in, use the following, this only needs to be done once for the Application domain.

using System.Net;
...
ServicePointManager.ServerCertificateValidationCallback =
new Security.RemoteCertificateValidationCallback(ValidateServerCertificate);

You can also use the policy to remove the error altogether but it would be better to fix the problem than do that.

David McEwing
You don't need to pass anything in the parameters as it is an Event handler. The underlying framewrok will do that for you. If you read past the first code block you will see the call for wiring it in.
David McEwing
I've added this. But,I'm yet to test it practically at the site where the problem exists.All I need to do is just add these lines? or Is there anything more to do?
Josh
You might want to change it to log to the Event log. But yes, I developed this snippet when remote diagnosing a certificate problem for a customer. Of course the problem was the Root CA cert was in the wrong store as @Andomar stated, but this script helped convince them.
David McEwing