/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.siso.horario.Servlets;

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 sistem02user
 */
public class sendMail {

    String to = "cvelasquez.ti@sacooliveros.edu.pe";
    String subject = "Mensaje de conformidad de registro de disponibilidad";
    String mensaje = "<h1 style=\"color:red;\">Funca prro</h1>\n"
            + "<div>\n"
            + " <h3 style=\"color:purple;\">:v</h3>\n"
            + "</div>";

    private void sendMail() {
        Properties p = new Properties();
        p.put("mail.smtp.auth", "true");
        p.put("mail.smtp.starttls.enable", "true");
        p.put("mail.smtp.host", "smtp.gmail.com");
        p.put("mail.smtp.port", "587");
//        p.put("mail.smtp.port", "465");

        Session session = Session.getInstance(p, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("cvelasquez2892@gmail.com", "hnuoqbwezosrshgr"); // modificar por tu cuenta y contraseña generada (key)
            }
        });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("cvelasquez2892@gmail.com")); // cambiar por tu cuenta
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject(subject);
            message.setContent(mensaje, "text/html; chartset=utf-8");

            Transport.send(message);
            System.out.println("correo enviado!");

        } catch (MessagingException e) {
            System.out.println("Error al enviar el correo => " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        sendMail metodo = new sendMail();
        metodo.sendMail();
    }
}
