'Why doesn't my sslstream get the certificate from a mail server?

From my code below, I should be getting the certificate of the mail server "mailgw.th-nuernberg.de".

That didn't work and I get the error "the handshake failed due to an unexpected packet format" by calling the method "AuthenticateAsClient".

I tried the same code with the mail server "smtp.gmail.com" on port 993. That works and I get the full certificate. The mail server "mailgw.th-nuernberg.de" exists but I don't know why Google's mail server is working and it isn't.

Here is my Code:

X509Certificate2 cert = null;
var client = new TcpClient("mailgw.th-nuernberg.de", 25);
var certValidation = new RemoteCertificateValidationCallback(delegate (object snd, X509Certificate certificate,
            X509Chain chainLocal, SslPolicyErrors sslPolicyErrors)
{
    return true; //Accept every certificate, even if it's invalid
});

// Create an SSL stream and takeover client's stream
using (var sslStream = new SslStream(client.GetStream(), true, certValidation))
{
    sslStream.AuthenticateAsClient("mailgw.th-nuernberg.de", null, System.Security.Authentication.SslProtocols.Tls13 | System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls11, true);
    var serverCertificate = sslStream.RemoteCertificate;
    cert = new X509Certificate2(serverCertificate);
    //System.Diagnostics.Debug.WriteLine("Heruntergeladenes Zertifikat: " + cert);
}
}
catch (Exception e)
{
    System.Diagnostics.Debug.WriteLine(e.Message);
    //throw some fancy exception ;-)
}

Does anyone know what the problem is? What's the difference using the Google mail server instead of using the mail server from my University?



Solution 1:[1]

You could directly write something like:

backgroundColor = colours[type] ? colours[type] : white

So if colours[type] is undefined, background color will be white otherwise the colour of the type you passed.

Solution 2:[2]

Try this way to reference a object value.

const colours = {
    normal: '#A8A77A',
    fire: '#EE8130',
    water: '#6390F0',
    electric: '#F7D02C',
    grass: '#7AC74C',
    ice: '#96D9D6',
    fighting: '#C22E28',
    poison: '#A33EA1',
    ground: '#E2BF65',
    flying: '#A98FF3',
    psychic: '#F95587',
    bug: '#A6B91A',
    rock: '#B6A136',
    ghost: '#735797',
    dragon: '#6F35FC',
    dark: '#705746',
    steel: '#B7B7CE',
    fairy: '#D685AD',
};

let typeFromPoke = "normal";
result = colours[typeFromPoke] || null;
console.log(result);

typeFromPoke = "steel";
result = colours[typeFromPoke] || null;
console.log(result);

typeFromPoke = "somethingelse";
result = colours[typeFromPoke] || null;
console.log(result);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Giovanni Esposito
Solution 2 Sivakumar A