/*
 * Copyright (c) 2004-2017, by Alex Leigh.
 * All rights reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 */

package lc.mecha.smtp;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Provides an interface for sending messages via email.
 *
 * @author Alex Leigh
 */
public class EmailTransport {
    private final Properties props = new Properties();

    public EmailTransport(final String smtpRelayHost, final int smtpRelayPort) {
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.host", smtpRelayHost);
        props.setProperty("mail.smtp.port", String.valueOf(smtpRelayPort));
        props.setProperty("mail.smtp.auth", "false");
        props.setProperty("mail.smtp.starttls.enable", String.valueOf(false));
    }

    public void sendTextEmail(final String from, final String[] to, final String subject, final String body)
            throws MessagingException {
        final Session ses = Session.getDefaultInstance(props);
        final Message msg = new MimeMessage(ses);

        msg.setFrom(new InternetAddress(from));
        msg.setSubject(subject);
        msg.setContent(body, "text/plain");

        final InternetAddress[] addressTo = new InternetAddress[to.length];
        for (int i = 0; i < to.length; i++) {
            addressTo[i] = new InternetAddress(to[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);

        // finalize
        msg.saveChanges();

        Transport.send(msg);
    }

    public void sendHtmlEmail(final String from, final String[] to, final String[] cc, final String[] bcc,
                              final String subject, final String html)
            throws MessagingException {
        final Session ses = Session.getDefaultInstance(props);
        final Message msg = new MimeMessage(ses);

        msg.setFrom(new InternetAddress(from));
        msg.setSubject(subject);
        msg.setContent(html, "text/html");

        final InternetAddress[] addressTo = new InternetAddress[to.length];
        for (int i = 0; i < to.length; i++) {
            addressTo[i] = new InternetAddress(to[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);

        if (cc != null) {
            final InternetAddress[] ccAddr = new InternetAddress[cc.length];
            for (int i = 0; i < cc.length; i++) {
                ccAddr[i] = new InternetAddress(cc[i]);
            }
            msg.setRecipients(Message.RecipientType.CC, ccAddr);
        }

        if (bcc != null) {
            final InternetAddress[] bccAddr = new InternetAddress[bcc.length];
            for (int i = 0; i < bcc.length; i++) {
                bccAddr[i] = new InternetAddress(bcc[i]);
            }
            msg.setRecipients(Message.RecipientType.BCC, bccAddr);
        }

        // finalize
        msg.saveChanges();

        Transport.send(msg);
    }

    public void sendHtmlEmail(final String from, final String[] to, final String subject, final String html)
            throws MessagingException {
        sendHtmlEmail(from, to, null, null, subject, html);
    }

}
