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 45 46 47 48 49 50 51 52 53 54 55 56
| public SendMail(String to[ ], String subject, String message , String from) throws MessagingException
{
//Initialise les informations de connexion à bluewin
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "mail.bluewin.ch");
props.setProperty("mail.port", "25");
//Récupère la session par défault
session = Session.getDefaultInstance(props, null);
//Création d'un message
Message msg = new MimeMessage(session);
//Initialise l'émetteur
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
//Initialise les destinataires
InternetAddress[] addressTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++)
{
addressTo[i] = new InternetAddress(to[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
//Place le sujet du mail
msg.setSubject(subject);
//Première partie du message
BodyPart messageBodyPart = new MimeBodyPart();
//Contenu du message
messageBodyPart.setText(message);
//Ajout de la première partie du message dans un objet Multipart
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
//Partie de la pièce jointe
/*messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("Exportation.txt");
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(videoname);*/
//Ajout de la partie pièce jointe
multipart.addBodyPart(messageBodyPart);
//On ajoute le tout au message
msg.setContent(multipart);
//Envoi du message
Transport.send(msg);
} |