protected JavaMailSender createJavaMailSender() { JavaMailSender answer = new DefaultJavaMailSender(); if (javaMailProperties != null) { answer.setJavaMailProperties(javaMailProperties); } else { // set default properties if none provided answer.setJavaMailProperties(createJavaMailProperties()); // add additional properties if provided if (additionalJavaMailProperties != null) { answer.getJavaMailProperties().putAll(additionalJavaMailProperties); } } if (host != null) { answer.setHost(host); } if (port >= 0) { answer.setPort(port); } if (username != null) { answer.setUsername(username); } if (password != null) { answer.setPassword(password); } if (protocol != null) { answer.setProtocol(protocol); } if (session != null) { answer.setSession(session); } else { ClassLoader tccl = Thread.currentThread().getContextClassLoader(); try { if (applicationClassLoader != null) { Thread.currentThread().setContextClassLoader(applicationClassLoader); } // use our authenticator that does no live user interaction but returns the already // configured username and password Session session = Session.getInstance( answer.getJavaMailProperties(), new DefaultAuthenticator(getUsername(), getPassword())); // sets the debug mode of the underlying mail framework session.setDebug(debugMode); answer.setSession(session); } finally { Thread.currentThread().setContextClassLoader(tccl); } } return answer; }