CorreoSqlserverDAO.java 1.7 KB
Newer Older
Luis Gangas committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package trismegistoplanilla.sqlserverdao;

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;
import trismegistoplanilla.beans.Correo;
import trismegistoplanilla.dao.CorreoDAO;

public class CorreoSqlserverDAO implements CorreoDAO {

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
	@Override
	public boolean enviarCorreo(Correo c) {
		System.out.println("CorreoSqlserverDAO: enviarCorreo");
		boolean envio;

		try {
			Properties p = new Properties();
			p.put("mail.smtp.host", "smtp.gmail.com");
			p.setProperty("mail.smtp.starttls.enable", "true");
			p.setProperty("mail.smtp.port", "587");
			p.setProperty("mail.smtp.user", "tplanilla@gmail.com");
			p.setProperty("mail.smtp.auth", "true");

			Session session = Session.getInstance(p, new Authenticator() {
				@Override
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("tplanilla@gmail.com", "aylffncgufqnockh");
				}
			});

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("tplanilla@gmail.com", "TRISMEGISTO - PLANILLA"));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(c.getDestino()));
			message.setSubject(c.getAsunto());
			message.setContent(c.getMensaje(), "text/html; chartset=utf-8");

			Transport.send(message, message.getAllRecipients());

			envio = true;
		} catch (UnsupportedEncodingException | MessagingException e) {
			e.printStackTrace();
			envio = false;
		}
		return envio;
	}
Luis Gangas committed
53
}