Package org.jzonic.jlo.handler

Source Code of org.jzonic.jlo.handler.MailHandler

/*
* Created on 21.07.2003
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package org.jzonic.jlo.handler;

import org.jzonic.jlo.LogRecord;
import org.jzonic.jlo.error.ErrorHandler;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;

/**
* @author Andreas Mecky andreas.mecky@xcom.de
* @author Terry Dye terry.dye@xcom.de
*
* This handler can be used to send the log message as email.
* The following parameters are required:<br/>
* <parameter name="smtphost" value="name of the smtp server"/><br/>
* <parameter name="sender" value="the mail address of the sender"/><br/>
* <parameter name="recipient" value="list of recievers"/><br/>
* this one is optional:<br/>
* <parameter name="subject" value="the subject of the mail"/><br/>
* If not set the mail will be send with the subject "no subject".<br/>
* These parameters are required if the smtp server requires "smtp after pop"<br/>
* <parameter name="user" value="the user name"/><br/>
* <parameter name="pwd" value="the password for this user"/><br/>
* <parameter name="popserver" value="the name of the pop3 server"/><br/>
* <parameter name="authentification" value="true"/><br/>
*
*/
public class MailHandler extends  AbstractHandler {
   
    private String smtpHost;
    private String popHost;
    private String username;
    private String pwd;
    private boolean authentification = false;
    private String from;
    private String to;
    private String subject;

    public MailHandler(String configName) {
        super(configName);
    }

    /* (non-Javadoc)
         * @see org.jzonic.jlo.handler.Handler#publish(java.lang.String)
         */
    public void publish(String msg) {
        sendEmail(from,to,null,subject,msg);
       
    }
   
        /* (non-Javadoc)
         * @see org.jzonic.jlo.handler.Handler#publish(org.jzonic.jlo.LogRecord)
         */
    public void publish(LogRecord lr) {
        publish(lr.getMessage());
       
    }
   
        /* (non-Javadoc)
         * @see org.jzonic.jlo.handler.Handler#setParameter(java.util.Hashtable)
         */
    public void setParameter(Map parameters) {
        // smtphost
        if ( parameters.containsKey("smtphost")) {
            smtpHost = (String)parameters.get("smtphost");
        }
        // from
        if ( parameters.containsKey("sender")) {
            from = (String)parameters.get("sender");
        }
        // to
        if ( parameters.containsKey("recipient")) {
            to = (String)parameters.get("recipient");
        }
        // subject
        if ( parameters.containsKey("subject")) {
            subject = (String)parameters.get("subject");
        }
        else {
            subject = "No subject";
        }
        // authentification
        if ( parameters.containsKey("authentification")) {
            String tmp = (String)parameters.get("authentification");
            if ( tmp.equalsIgnoreCase("true")) {
                authentification = true;
            }
        }
        // user
        if ( parameters.containsKey("user")) {
            username = (String)parameters.get("user");
        }
        // pwd
        if ( parameters.containsKey("pwd")) {
            pwd = (String)parameters.get("pwd");
        }
        // popserver
        if ( parameters.containsKey("popserver")) {
            popHost = (String)parameters.get("popserver");
        }

    }
   
    private void sendEmail(String from, String recipients, String copies, String subject, String bodyText) {
        try {
            Properties props = System.getProperties();
            props.put("mail.smtp.host", smtpHost);
            if (authentification) {
                props.put("mail.smtp.auth", "true");
            }
            Session session = Session.getDefaultInstance(props, null);           
            if (authentification) {
                // Log onto pop server first as security protocol may require this
                Session pSession = Session.getInstance(props, null);
                Store store = pSession.getStore("pop3");
                store.connect(popHost, username, pwd);
                store.close();
            }
            // Create a mime message
            MimeMessage mail = new MimeMessage(session);
            mail.setFrom(new InternetAddress(from));
           
            StringTokenizer st = new StringTokenizer(recipients, ",");
            InternetAddress[] recList = new InternetAddress[st.countTokens()];
            for (int r = 0; st.hasMoreTokens(); r++)
                recList[r] = new InternetAddress(st.nextToken().trim());
            mail.setRecipients(Message.RecipientType.TO, recList);
           
            if (copies != null) {
                st = new StringTokenizer(copies, ",");
                InternetAddress[] copyList = new InternetAddress[st.countTokens()];
                for (int c = 0; st.hasMoreTokens(); c++) {
                    copyList[c] = new InternetAddress(st.nextToken().trim());
                }
                mail.setRecipients(Message.RecipientType.CC, copyList);
            }
            mail.setSubject(subject);
            mail.setText(bodyText);
            mail.setSentDate(new Date());
           
            // Send the message
            Transport trans = session.getTransport("smtp");
            trans.connect(smtpHost, username, pwd);
            trans.sendMessage(mail, mail.getAllRecipients());
            trans.close();
        } catch (Exception e) {
            ErrorHandler.reportError("Cannot send email", e);
        }
    }
}
TOP

Related Classes of org.jzonic.jlo.handler.MailHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.