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
   |  
package be.isl.tfe.negoce.jsf.util;
 
import be.isl.tfe.negoce.entity.Commande;
import be.isl.tfe.negoce.entity.Contact;
import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
 
/**
 *
 * @author
 */
public class SendMail {
 
    private static final String SMTP_HOST_NAME = "smtp.mail.yahoo.com"; 
    private static final String SMTP_AUTH_USER = "username"; 
    private static final String SMTP_AUTH_PWD = "password"; 
    private static final String emailMsgTxt = "Bonjour, \nCeci n'est pas un mail  \n\nBien à vous, \nSignature";
    private static final String emailSubjectTxtOrderSend = "Hello world!"; 
    private static final String emailFromAddress = "username@yahoo.com"; // à remplacer par "username@gmail.com"
 
    public static void sendCommandByMail(Contact contact, Commande commande, File file) throws MessagingException { 
// si tu n'envoie pas de pièce jointe supprime les args Commande et File 
// + leurs MimeBodyPart
 
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.from", emailFromAddress);
        props.put("mail.smtps.auth", "true");
        Session session = Session.getInstance(props, null);
 
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(emailFromAddress));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(contact.getEmail(), false));
 
        msg.setSubject(emailSubjectTxtOrderSend);
        msg.setSentDate(new Date());
 
        // Body part
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText(emailMsgTxt);
 
        //add bodypart to multipart
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
 
        // join
        messageBodyPart = new MimeBodyPart();
        FileDataSource source = new FileDataSource(file);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName("Commande_" + commande.getIdcommande()+ ".pdf");//
 
        //Ajout de la partie pièce jointe
        multipart.addBodyPart(messageBodyPart);
        msg.setContent(multipart);
 
 
        Transport transport = session.getTransport("smtp");
        transport.connect(SMTP_HOST_NAME, SMTP_AUTH_USER, SMTP_AUTH_PWD);
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();
    }
 
    // getters & setters
    public static String getSMTP_AUTH_PWD() {
        return SMTP_AUTH_PWD;
    }
 
    public static String getSMTP_AUTH_USER() {
        return SMTP_AUTH_USER;
    }
 
    public static String getSMTP_HOST_NAME() {
        return SMTP_HOST_NAME;
    }
 
    public static String getEmailFromAddress() {
        return emailFromAddress;
    }
 
    public static String getEmailMsgTxt() {
        return emailMsgTxt;
    }
 
    public static String getEmailSubjectTxtOrderSend() {
        return emailSubjectTxtOrderSend;
    }
} | 
Partager