I’m trying to send an email from a system which is trying to use as little bandwidth as possible. To this end, I’ve been caching DNS lookups locally and attempting to send direct to the IP address instead of the domain name.
This is the snippet which sends my data.
String^ _EmailServer = "smtp.gmail.com";
String^ _LoginName = "me@gmail.com";
String^ _LoginPassword = "mypassword";
System::Net::Mail::SmtpClient ^_SmtpClient = gcnew System::Net::Mail::SmtpClient(_EmailServer);
System::Net::NetworkCredential ^_NetworkCredential = gcnew System::Net::NetworkCredential(_LoginName, _LoginPassword);
_
_SmtpClient->DeliveryMethod = System::Net::Mail::SmtpDeliveryMethod::Network;
_SmtpClient->UseDefaultCredentials = false;
_SmtpClient->Credentials = _NetworkCredential;
_SmtpClient->EnableSsl = true;
_SmtpClient->Send(_MailMessage);
Now the weird thing is that snippet works but if I do the DNS lookup myself and change _EmailServer to the IP address I resolved, the code above throws a System::Security::Authentication::AuthenticationException which says The remote certificate is invalid according to the validation procedure.
This fails even if I do the lookup and immediately try and send using the IP address I resolved.
The reason seems to be the fact that the server certificate is issued to domain smtp.gmail.com and when it gets compared to IP address (which you passes in) it fails as the cert domain doesn’t match with the server address you passed i.e the IP address (instead of actual server name)