package eu.dnetlib.openaire.user.utils;

import org.apache.log4j.Logger;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by kiatrop on 9/10/2017.
 */
public class EmailSender {

    private String username;
    private String password;
    private String host;
    private String port;
    private String from;
    private String auth;
    private String sslProtocols;

    Logger logger = Logger.getLogger(EmailSender.class);

    public void sendEmail(String recipient, String subject, String body) throws MessagingException {

        // Get system properties
        Properties properties = System.getProperties();
        properties.put("mail.transport.protocol", "smtp");
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", auth);
        properties.put("mail.smtp.starttls.enable", "true");
        if(sslProtocols != null) {
            properties.put("mail.smtp.ssl.protocols", sslProtocols);
        }
        Session session = javax.mail.Session.getInstance(properties,
                new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);

        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));

        // Set To: header field of the header.
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));

        // Set Subject: header field
        message.setSubject(subject, "UTF-8");

        // For simple text setText() can be used instead of setContent()

        // Send the actual HTML message, as big as you like
        message.setContent(body, "text/html;charset=UTF-8");

        // Send message
        Transport.send(message);
        logger.debug("Sent message successfully....\n");

    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getAuth() {
        return auth;
    }

    public void setAuth(String auth) {
        this.auth = auth;
    }

    public String getSslProtocols() {
        return sslProtocols;
    }

    public void setSslProtocols(String sslProtocols) {
        this.sslProtocols = sslProtocols;
    }
}
