/*
 * Created on Jan 1, 2005
 * Copyright (C) 2004, 2005 Andrea Schweer
 *
 * This file is part of the Greenstone Alerting Service.
 * Refer to the COPYING file in the base directory of this package
 * for licensing information.
 */
package org.greenstone.gsdlas.util;

import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author schweer
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Mailer {
    
    private String fromAddress;
    private String mailHost;
    
    public Mailer (String from, String host) {
        fromAddress = from;
        mailHost = host;
    }
    
    public void sendMail(String to, String subject, String message) throws MessagingException {
        // Set the host smtp address
        Properties props = new Properties();
        props.put("mail.smtp.host", mailHost);
        
        // create some properties and get the default Session
        //Session session = Session.getDefaultInstance(props, null);
        Session session = Session.getInstance(props);
        
        // create a message
        Message msg = new MimeMessage(session);
        
        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(fromAddress);
        msg.setFrom(addressFrom);
        
        InternetAddress[] addressTo = new InternetAddress[1];
        addressTo[0] = new InternetAddress(to);
        msg.setRecipients(Message.RecipientType.TO, addressTo);
        
        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport.send(msg);
    }
}
