Tag Archives: SSL

Java: Ignore SSL certificate errors

Not that we’d do such a thing of course, but here’s how to ignore all SSL certificate errors in Java.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;

import javax.net.ssl.*;

/**
 * A {@link X509TrustManager} and {@link HostnameVerifier} which trust everything.
 *
 * @author    Torleif Berger
 * @license   http://creativecommons.org/licenses/by/3.0/
 * @see       http://www.geekality.net/?p=2408
 */

public final class TrustAllCertificates implements X509TrustManager, HostnameVerifier
{
    public X509Certificate[] getAcceptedIssuers() {return null;}
    public void checkClientTrusted(X509Certificate[] certs, String authType) {}
    public void checkServerTrusted(X509Certificate[] certs, String authType) {}
    public boolean verify(String hostname, SSLSession session) {return true;}
   
    /**
     * Installs a new {@link TrustAllCertificates} as trust manager and hostname verifier.
     */

    public static void install()
    {
        try
        {
            TrustAllCertificates trustAll = new TrustAllCertificates();
           
            // Install the all-trusting trust manager
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null,
                    new TrustManager[]{trustAll},
                    new java.security.SecureRandom());         
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            // Install the all-trusting host verifier
            HttpsURLConnection.setDefaultHostnameVerifier(trustAll);
        }
        catch (NoSuchAlgorithmException e)
        {
            throw new RuntimeException("Failed setting up all thrusting certificate manager.", e);
        }
        catch (KeyManagementException e)
        {
            throw new RuntimeException("Failed setting up all thrusting certificate manager.", e);
        }
    }
}

Usage

TrustAllCertificates.install();

HttpURLConnection c = (HttpURLConnection) url.openConnection();
// ...and so on and so forth

C#: How to send emails

Sending a basic email message in a C# application is quite easy thanks to a class called SmptClient. We simply need an address to send to, an address to send from, the message we want to send and the address of an SMTP server, hand it all to the SMTP client, and you’re done:

var from = new MailAddress("me@example.com", "Me");
var to = new MailAddress("you@example.com", "You");

var message = new MailMessage(from, to)
{
    Subject = "Greetings!",
    Body = "How are you doing today?",
};

var client = new SmtpClient("smtp.example.com");

using (client)
{
    try
    {
        client.Send(message);
    }
    catch (SmtpException e)
    {
        Console.WriteLine(e.Message);
    }
}

That was pretty simple, wasn’t it? But what if we need to authenticate with our server? And what if we want to send our message in a more secure manner?

Continue reading