Getting "The remote certificate is invalid according to the validation procedure" when SMTP server has a valid certificate
Stefan Bogdanescu
Founder & Senior Architect
Decoding the SMTP Certificate Nightmare: Why "Remote Certificate is Invalid" Persists
Getting the dreaded error, "The remote certificate is invalid according to the validation procedure," when setting up secure SMTP communication is an incredibly frustrating experience. It suggests a fundamental disconnect between what your application expects and what the server is presenting, especially when you know the connection works perfectly fine in a standard web browser.
As a senior developer, I’ve dealt with this exact scenario repeatedly, particularly when dealing with internal systems like MS Exchange servers configured with Explicit SSL/TLS. This post will dissect why this error occurs, analyze the certificate chain mismatch you are seeing, and provide a robust strategy for handling certificate validation in your application code.
The Disconnect: Browser Trust vs. Application Validation
The core of your confusion lies in the difference between how a web browser handles SSL/TLS and how a C# SMTP client (or any other network library) performs validation.
When you navigate to https://webmail.ourdomain.co.uk in a browser, the browser relies on a massive, pre-installed list of trusted Root Certificate Authorities (CAs). It implicitly trusts the chain presented by the server because it is designed for general web browsing.
However, when your application code initiates an SMTP connection, it often enforces stricter, programmatic validation rules based on its specific trust store configuration. The errors you are seeing—RemoteCertificateChainErrors and RemoteCertificateNameMismatch—indicate that while the server presents a certificate, the chain leading back to a trusted root authority within your application's context is broken or invalid according to strict protocol checks.
Why Your Custom Callback Fails
You correctly attempted to use a custom validation callback:
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
public static bool ValidateServerCertificate(object sender,X509Certificate certificate,X509Chain chain,SslPolicyErrors sslPolicyErrors)
{
// ... custom logic ...
}
While this successfully bypasses the immediate error in your test code (often by forcefully accepting the certificate), it doesn't solve the underlying problem of why the system flags the chain as invalid. You are essentially telling the client, "I don't care if the chain is wrong," which shifts the security burden away from proper validation.
The issue isn't that the certificate is missing; it’s that the Certificate Authority (CA) that signed your internal Exchange certificate might not be included in the default trust store used by the .NET runtime, leading to the chain validation failure when the application attempts to verify the connection integrity.
The Developer Solution: Trusting Internal CAs Explicitly
If you cannot simply bypass the check, the solution lies in ensuring your application explicitly trusts the internal Certificate Authority (CA) that issued the Exchange server's certificate. This is a common requirement in enterprise environments where custom internal PKI systems are used instead of relying solely on public CAs.
For applications built on frameworks like Laravel or similar robust architectures, managing complex external dependencies—like secure network communication—requires precise control over trust. Think about how secure networking principles apply across different platforms; ensuring that all components agree on the established security context is paramount. This principle applies equally to application-level certificate handling as it does to routing and data integrity in a Laravel application.
Best Practice: Trusting the Root Certificate
Instead of relying solely on runtime validation, the most robust approach for internal systems is often to load the trusted root certificates directly into the application's store or configure the connection context to accept specific internal roots. This ensures that your system trusts the source of the certificate, not just its superficial presentation.
For environments where you need deterministic trust (especially when dealing with private CAs), you must ensure that the necessary Root and Intermediate certificates are imported into the operating system's or application-level certificate store. Once this is done correctly, the default validation mechanism will succeed without needing broad, insecure bypasses.
Conclusion
The error message "remote certificate is invalid" is a symptom of a trust boundary issue, not necessarily an issue with the server itself. By understanding the distinction between browser trust and application-level validation, you can move beyond temporary workarounds. For production systems, focus on correctly configuring the application's trust anchor to explicitly recognize your internal Certificate Authorities. This provides a secure, verifiable foundation for your SMTP communication, ensuring both security compliance and reliable operation.