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
|
package EnvoiMail;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.JOptionPane;
public class Send {
private String nom;
private String email;
private String contenu;
private String tel;
private CarteVisiteFenetre CVFenetre;
protected String mailMoi = "contact@bibi.fr";
protected String host = "smtp.gmail.com";
protected String idgmail = "email_gmail_bibi@gmail.com";
protected String pwdgmail = "mdpbibi";
private Properties prop;
private Properties props;
public Send(CarteVisiteFenetre cvf,String name, String Mail, String Tel, String Cont){
this.CVFenetre = cvf;
this.nom = name;
this.email = Mail;
this.contenu = Cont;
this.tel = Tel;
this.prop = new Properties();
this.props = new Properties();
}
public void envoi () throws AddressException, MessagingException{
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.host", this.host);
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.starttls.enable", true);
Session sessionMail = Session.getInstance(prop,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(idgmail, pwdgmail);
}
});
MimeMessage msg = new MimeMessage(sessionMail);
sessionMail.setDebug(true);
try {
msg.setFrom(new InternetAddress(this.mailMoi));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(this.mailMoi));
msg.setSubject(this.nom);
msg.setText(this.contenu + "\n\nCe message a été envoyé par : " + this.email + "\nTel : " + this.tel);
Transport.send(msg);
JOptionPane.showMessageDialog(this.CVFenetre, "Votre email est envoyé.");
} catch (MessagingException ex) {
Logger.getLogger(Send.class.getName()).log(Level.SEVERE, null, ex);
//JOptionPane.showMessageDialog(this.CVFenetre, ex);
}
}
} |
Partager