Spring Boot and Camel Mail SSL config

1.6k Views Asked by At

Am trying to configure Spring Boot with Camel so that am able to send mails using smtps. I have simple route

@Component
public class EmailRoute extends RouteBuilder {

  public static final String IN = "seda://email";

  public static final String OUT = "smtps://myhost:465?myuser&password=mypass&debugMode=true";

  @Override
  public void configure() throws Exception {
    from(IN).to(OUT);
  }

When I send something using this route am getting error :

javax.mail.MessagingException: Could not connect to SMTP host : myhost    
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

I have pem file cert ( without keys , just cert ) and I have imported it to the jks keychain file

added a method to the EmailRoute class for creating SSLContextParameters params

private SSLContextParameters sslContextParameters(){
    KeyStoreParameters store = new KeyStoreParameters();
    store.setResource("pathToJskFile/cert.jks");
    store.setPassword("123456");

    TrustManagersParameters trust = new TrustManagersParameters();
    trust.setKeyStore(store);

    SSLContextParameters parameters = new SSLContextParameters();
    parameters.setTrustManagers(trust);

    return parameters;
  }

Not sure now how to connect does two using camel and spring boot , as the example from http://camel.apache.org/mail.html with registry is not 100% clear

1

There are 1 best solutions below

1
On BEST ANSWER

You can likely use Spring Boot to register the SSLContextParameters in its registry with the @Bean.

Make the method public and add the @Bean annotation:

@Bean
public SSLContextParameters myMailSslContextParameters() {

And then tell Camel smtp endpoint to use this using the # lookup syntax:

smtps://myhost:465?myuser&password=mypass&debugMode=true&sslContextParameters=#myMailSslContextParameters

Notice how the name of the method is the name you use for lookup.