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
| package classes;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
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 SuperClass implements Serializable {
Session sess = null;
private String destinat = "";
private String subject = "";
private String content = "";
public SendMail() {
super();
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.host", Datas.SMTPHOST);
properties.put("mail.smtp.port", Datas.SMTPPORT);
//properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//properties.put("mail.debug", "true");
//sess = Session.getDefaultInstance(properties, null);
sess = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Datas.NOREPLY, Datas.PWDNOREPLY);
}
});
}
public void sendMessage() throws MessagingException, UnsupportedEncodingException {
Transport transport = sess.getTransport();
Message mess = new MimeMessage(sess);
mess.setFrom(new InternetAddress(Datas.NOREPLY, "Télétravail.coM"));
mess.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destinat));
mess.setSubject(subject);
mess.setContent(content, "text/html; charset=UTF-8");
mess.setHeader("Content-Type", "text/html; charset=\"UTF-8\"");
transport.connect();
transport.sendMessage(mess, mess.getRecipients(Message.RecipientType.TO));
transport.close();
}
public void sendMessage(String FromEmail, String FromName) throws MessagingException, UnsupportedEncodingException {
Transport transport = sess.getTransport();
Message mess = new MimeMessage(sess);
mess.setFrom(new InternetAddress(FromEmail, FromName));
mess.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destinat));
mess.setSubject(subject);
mess.setContent(content, "text/html; charset=UTF-8");
mess.setHeader("Content-Type", "text/html; charset=\"UTF-8\"");
transport.connect();
transport.sendMessage(mess, mess.getRecipients(Message.RecipientType.TO));
transport.close();
}
/**
* @param destinat the destinat to set
*/
public void setDestinat(String destinat) {
this.destinat = destinat;
}
/**
* @param subject the subject to set
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* @param content the content to set
*/
public void setContent(String content) {
this.content = content;
}
} |
Partager