title: JavaMail problems after using Key Store
body: <p>I'm having a weird problem with a JSF application I'm currently working on. It seems like there are two parts of my programm which are colliding.</p>
  
  <p>There are two parts:</p>
  
  <ul>
  <li>the "banking" functionality</li>
  <li>the mail functionality</li>
  </ul>
  
  <p>The relevant part of the <strong>banking functionality</strong> (It's a fake bank just for this exercise):</p>
  
  <pre><code>String path = FacesContext.getCurrentInstance().getExternalContext()                .getRealPath("/") + "/WEB-INF/sec/certs.jks";
  
  ErrorHandler.trace(path);
  
  System.setProperty("javax.net.ssl.trustStore", path);
  </code></pre>
  
  <p>Here it's setting the Trust Store with the certificate for the bank server.</p>
  
  <p>The <strong>mail</strong> part looks like this:</p>
  
  <pre><code>Properties props = new Properties();
  props.put("mail.smtp.auth", this.smtpServer.isAuthenticated());
  props.put("mail.smtp.starttls.enable", this.smtpServer.isTls());
  props.put("mail.smtp.host", this.smtpServer.getHostaddr());
  props.put("mail.smtp.port", this.smtpServer.getPort());
  props.put("mail.smtps.auth", "true");
  props.put("mail.smtp.debug", "true");
  
  final String username = this.smtpServer.getUsername();
  final String password = this.smtpServer.getPassword();
  
  Session session = Session.getDefaultInstance(props,
          new javax.mail.Authenticator() {
              @Override
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(username, password);
              }
          });
  
  session.setDebug(true);
  </code></pre>
  
  <p><strong>One way to reproduce the problem:</strong></p>
  
  <p>The problem I'm facing is that if I start the application and, for example, use the "change mail" functionality I'll get my notification mail immediately. No problem there. Then I'll try to buy a product thus triggering the bank functionality.</p>
  
  <p>That's where the problem shows up:</p>
  
  <pre><code>Communication Error: javax.ws.rs.WebApplicationException: javax.xml.bind.MarshalException
   - with linked exception:
  [javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target]
  </code></pre>
  
  <p><strong>Another way to reproduce the problem:</strong></p>
  
  <p>Now let's say I restart my application and try to order something, this time it'll work but the mail functionality is broken with this error message:</p>
  
  <pre><code>DEBUG: setDebug: JavaMail version 1.4.7
  DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
  DEBUG SMTP: useEhlo true, useAuth true
  DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
  TRACE Error Could not connect to SMTP host: smtp.gmail.com, port: 465
  </code></pre>
  
  <p><strong>Bottom line:</strong></p>
  
  <p>If I trigger bank and then mail -> mail not working
  If I trigger mail and then bank -> bank not working</p>
  
  <p>Anyone who could find a problem there?</p>
  
  <p>Thank you!</p>