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
|
...
Properties props = new Properties();
props.put("mail.smtp.user", email);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", String.valueOf(port));
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", String.valueOf(port));
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Authenticator auth = new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(email,password);
}
};
Session session = Session.getInstance(props, auth);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setText(message);
msg.setSubject(subject);
msg.setFrom(new InternetAddress(email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
Transport transport = session.getTransport("smtps");
transport.connect(host, port, username, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close(); |
Partager