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
   |  
package scheduler;
 
import java.io.IOException;
import java.util.List;
import java.util.Properties;
 
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
 
import gct.dao.ParticipationDao;
import gct.dao.ParticipationTacheDao;
import gct.entities.ParticipationTache;
import gct.validators.GMailAuthenticator;
 
public class SchedulerJob implements Job{
	@EJB
	private ParticipationTacheDao participationTacheDao;
	public void envoyerEmail() throws AddressException, MessagingException {
		List<ParticipationTache> participantTache=participationTacheDao.listeParticipantTacheNonAvertiParMail();
		for(ParticipationTache participant:participantTache){
			String sujetEmail="Tâches";
			String messageSent="Vous êtes informé que vous participez à la réalisation de la tâche "+participant.getTache().getNom()+" du "+participant.getTache().getDateDbt()+" au "+participant.getTache().getDateFin()+". Description:"
					+ participant.getTache().getDescription();
			String destinataireEnvoyer=participant.getUtilisateur().getEmail();
			System.out.println("Initialisation d'envoi de mail... ");
			try {
			final String username="myemail@gmail.com";
			final String password="mypassword";
			String host = "smtp.gmail.com";
			String from = "myemail@gmail.com";
			String pass = "mypassword";
			Properties props = System.getProperties();
			props.put("mail.smtp.starttls.enable", "true");
			props.put("mail.smtp.host", host);
			props.put("mail.smtp.user", from);
			props.put("mail.smtp.password", pass);
			props.put("mail.smtp.port", "587");
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.ssl.enable", "false");
			props.put("mail.debug", "true");
 
			Session session =  Session.getInstance(props, new GMailAuthenticator(username, password));
			MimeMessage message = new MimeMessage(session);
			Address fromAddress = new InternetAddress(from);
			Address toAddress = new InternetAddress(destinataireEnvoyer);
 
			message.setFrom(fromAddress);
			message.setRecipient(Message.RecipientType.TO, toAddress);
 
			message.setSubject(sujetEmail);
			message.setText(messageSent);
			Transport transport = session.getTransport("smtp");
			transport.connect(host, from, pass);
			message.saveChanges();
			Transport.send(message);
			transport.close();
 
      }catch(Exception ex){
 
			System.out.println("<html><head></head><body>");
			System.out.println("ERROR: " + ex);
			System.out.println("</body></html>");
      }	
 
			System.out.println("Email envoyé avec succès au participant "+participant.getUtilisateur().getPrenom());
		}
	}
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		try {
			envoyerEmail();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
 
	}
 
} | 
Partager