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 42 43 44
| private static String smtpServer="un serveur smtp";
private static String fromPerson="mail@provider.fr";
/** send permet d'envoyer un message via un serveur smtp
* @param smtpServer le serveur smtp
* @param to destinataire du message
* @param cc personne en copie (???)
* @param from emetteur du message
* @param subject (sujet du message)
* @param body (corps du message)
*/
private static void send(String smtpServer,String to,String cc,String from,String subject,String body) {
try {
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
// -- We could include CC recipients too --
if (cc != null)
msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse(cc, false));
// -- Set the subject and body text --
msg.setSubject(subject);
msg.setText(body);
// -- Set some other header information --
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
// -- Send the message --
Transport.send(msg);
} catch (Exception ex) { ex.printStackTrace(); }
} |
Partager