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
| protected void openConnection(final String host, final String adresse,
final String password, Integer port, boolean ssl, Integer timeout)
throws Exception {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", host);
if (timeout != null) {
// convert timeout to milliseconds
timeout = timeout.intValue();
props.put("mail.smtp.timeout", timeout);
}
props.put("mail.smtp.port", String.valueOf(port));
if (ssl) {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
props.put("mail.smtp.socketFactory.class",
DefaultParamsConstant.DEFAULT_SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.port", String.valueOf(port));
}
if (adresse != null && !adresse.isEmpty()) {
props.setProperty("mail.user", adresse);
if (password != null && !password.isEmpty()) {
props.setProperty("mail.password", password);
}
props.put("mail.smtp.auth", "true");
mailSession = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(adresse, password);
}
});
} else {
mailSession = Session.getInstance(props);
}
transport = mailSession.getTransport();
} |
Partager