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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| public class Mail extends javax.mail.Authenticator {
private String email;
private String password;
private String[] to;
private String from;
private String port;
private String sport;
private String host;
private String subject;
private String body;
private boolean auth;
private boolean debuggable;
private Multipart multipart;
// Constructeur par défaut
public Mail() {
host = "smtp.gmail.com"; // Serveur SMTP
port = "465"; // Port SMTP
sport = "465"; // SocketFactory Port
email = ""; // Email
password = ""; // MDP
from = ""; // Destination
subject = ""; // Sujet
body = ""; // Message
debuggable = false; // Mode debug
auth = true; // Authentification SMTP
multipart = new MimeMultipart();
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
}
// Constructeur avec 2 paramétres
public Mail(String email, String password) {
this();
this.email = email;
this.password = password;
}
// Fonction qui permet d'envoyer le mail
public boolean send() throws Exception {
Properties props = _setProperties();
// Vérification que tous les attributs nécésaires ne sont pas vides
if(!email.equals("") && !password.equals("") && to.length > 0 && !from.equals("")
&& !subject.equals("") && !body.equals("")) {
// Crée une Session ...
Session session = Session.getInstance(props, this);
final MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
// Ajout tous les adresse de destination
InternetAddress[] addressTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
addressTo[i] = new InternetAddress(to[i]);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
// Le sujet
msg.setSubject(subject);
// La date
msg.setSentDate(new Date());
// Le message
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
multipart.addBodyPart(messageBodyPart);
// Les pièces jointes
msg.setContent(multipart);
// GO
new Thread(new Runnable() { @Override public void run() { try {
Transport.send(msg);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }).start();
//Transport.send(msg);
return true;
}
else {
return false;
}
}
// Ajout d'une pièce jointe
public void addAttachment(String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
}
// Obtenir l'authentification
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(email, password);
}
// Propriétés pour la connexion
private Properties _setProperties() {
Properties props = new Properties();
props.put("mail.smtp.host", host);
if(debuggable) {
props.put("mail.debug", "true");
}
if(auth) {
props.put("mail.smtp.auth", "true");
}
props.put("mail.smtp.port", port);
props.put("mail.smtp.socketFactory.port", sport);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
return props;
}
// Acceseurs
public void setBody(String body) {
this.body = body;
}
public void setFrom(String from) {
this.from = from;
}
public void setTo(String[] toArr) {
this.to = toArr;
}
public void setSubject(String subject) {
this.subject = subject;
}
} |
Partager