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
| package classes;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* @author PJ
*/
public class SendMail extends Objet {
private String object = "";
private String content = "";
private String hostname = "smtp.gmail.com ";
private Session session = null;
private String from = "";
private String to = "";
private String user = "XXX@gmail.com";
private String password = "XXXXXXX";
private String firstname = "";
private String lastName = "";
public SendMail(String from, String to) {
this.from = from;
this.to = to;
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", hostname);
props.put("mail.from", this.from);
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.ssl", "true");
session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
}
public void send() {
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(firstname + " " + lastName + "<" + from + ">"));
msg.setRecipients(Message.RecipientType.TO, to);
msg.setSubject("XXX.com : " + object);
msg.setSentDate(new Date());
initContent();
msg.setContent(content, "text/html; charset=UTF-8");
msg.setHeader("Content-Type", "text/html; charset=\"UTF-8\"");
Transport.send(msg);
} catch (MessagingException ex) {
Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
errorMsg += "Impossible d'envoyer le message : " + ex.getMessage();
}
}
private void initContent() {
content = "<!DOCTYPE html>\n"
+ "<html>\n"
+ " <head>\n"
+ " <title></title>\n"
+ " <meta charset=\"UTF-8\">\n"
+ " </head>\n"
+ "<style type=\"text/css\">\n"
+ "body {"
+ " font-family: Arial, serif;"
+ " font-size: 1em;"
+ "}"
+ "</style>"
+ " <body>\n"
+ "<div>Mail envoyé depuis XXX.com :</div>"
+ "<p>" + content + "</p>"
+ " </body>\n"
+ "</html>\n";
}
/**
* @return the object
*/
public String getObject() {
return object;
}
/**
* @param object the object to set
*/
public void setObject(String object) {
this.object = object;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content the content to set
*/
public void setContent(String content) {
this.content = content;
}
/**
* @return the hostname
*/
public String getHostname() {
return hostname;
}
/**
* @param hostname the hostname to set
*/
public void setHostname(String hostname) {
this.hostname = hostname;
}
/**
* @param firstname the firstname to set
*/
public void setFirstname(String firstname) {
this.firstname = firstname;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
} |
Partager