Pour ceux que ça intéresse, j'ai écris une petite classe utilitaire pour envoyer des mails. Il suffit d'ajouter la librairie Java Mail au CLASSPATH. Ici, on passe par une connexion sécurisée.
import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class Mailer { private static String from = "test@gmail.com"; private static String password = "test"; private static String host = "smtp.gmail.com"; public static void sendMail(String recipient, String subject, String message) { String[] recipients = {recipient}; sendMail(recipients, subject, message); } public static void sendMail(String[] recipients, String subject, String message) { try { //Set the host smtp address Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.starttls.enable", "true"); // create some properties and get the default Session Session session = Session.getDefaultInstance(props, null); // create a message javax.mail.Message msg = new MimeMessage(session); // set the from and to address InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { addressTo[i] = new InternetAddress(recipients[i]); } msg.setRecipients(javax.mail.Message.RecipientType.TO, addressTo); msg.setSubject(subject); msg.setContent(message, "text/html"); Transport transport = session.getTransport("smtp"); transport.connect(session.getProperty("mail.smtp.host"), from, password); transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); } catch (MessagingException ex) { System.err.println(ex); } } }
Merci pour cette contribution Loic :)