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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
import javax.mail.internet.*;
import java.util.Properties;
import javax.mail.*;
class Mail {
public static void sendMessage(String from,String pwd,String to,String sub,String msg){
//Propriétés
Properties p = new Properties();
p.put("mail.smtp.host", "mail.mailo.com");
p.put("mail.smtp.port", "465");
p.put("mail.smtp.auth", "true");
//p.put("mail.smtp.starttls.enable", "true");
//p.put("mail.smtp.starttls.required", "true");
p.put("mail.smtp.ssl.protocols", "TLSv1.2");
p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// p.put("mail.smtp.host", "smtp.mailo.com");
// p.put("mail.smtp.socketFactory.port", "465");
// p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// p.put("mail.smtp.auth", "true");
// p.put("mail.smtp.port", "465");
//Session
Session s = Session.getDefaultInstance(p,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
System.out.println("from = "+from+" pwd = "+pwd);
return new PasswordAuthentication(from, pwd);
}
});
//composer le message
try {
MimeMessage m = new MimeMessage(s);
System.out.println("Message.RecipientType.TO = "+Message.RecipientType.TO+" InternetAddress(to) "+new InternetAddress(to));
m.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
m.setSubject(sub);
m.setText(msg);
//envoyer le message
Transport.send(m);
System.out.println("Message envoyé avec succès");
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//from, password, to, subject, message
Mail.sendMessage(
"f.name@mailo.com",
"passwd",
"f.name@mailo.com",
"Bienvenu sur mailo",
"mail de test!"
);
}
} |
Partager