Package net.sf.jportlet.service.mail

Source Code of net.sf.jportlet.service.mail.MailServiceImpl

/*
* Created on May 11, 2003
*/
package net.sf.jportlet.service.mail;

import java.util.Date;

import javax.activation.DataHandler;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import net.sf.jportlet.service.PortletServiceAdapter;
import net.sf.jportlet.service.PortletServiceConfig;
import net.sf.jportlet.service.PortletServiceException;


/**
* @author <a href="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
*/
public class MailServiceImpl
    extends PortletServiceAdapter
    implements MailService
{
    //~ Instance fields --------------------------------------------------------

    private String _jndiSession;

    //~ Methods ----------------------------------------------------------------

    /**
     * @see net.sf.jportlet.service.mail.MailService#send(java.net.InetAddress, java.net.InetAddress[], java.lang.String, java.lang.String, java.lang.String)
     */
    public void send( Address  from,
                      Address  to[],
                      String   subject,
                      String   body,
                      String   mimeType )
        throws NamingException,
                   MessagingException
    {
        InitialContext ic = new InitialContext(  );
        Session        session = ( Session ) ic.lookup( _jndiSession );
        Message        msg = new MimeMessage( session );

        if ( from != null )
        {
            msg.setFrom( from );
        }
        else
        {
            msg.setFrom();
        }
        msg.setRecipients( javax.mail.Message.RecipientType.TO, to );
        msg.setSubject( subject );

        msg.setDataHandler( new DataHandler( body, mimeType ) );

        //msg.setHeader( "X-Mailer", "JavaMailer" );
        msg.setSentDate( new Date(  ) );

        Transport.send( msg );
    }

    /**
     * @see net.sf.jportlet.service.PortletService#getServiceName()
     */
    public String getServiceName(  )
    {
        return MailService.NAME;
    }

    /**
     * @see net.sf.jportlet.service.PortletService#init(net.sf.jportlet.service.PortletServiceConfig)
     */
    public void init( PortletServiceConfig config )
        throws PortletServiceException
    {
        super.init( config );

        _jndiSession = getInitParameter( "session" );
        if ( _jndiSession == null )
        {
            throw new PortletServiceException( "session parameter is required" );
        }
    }
}
TOP

Related Classes of net.sf.jportlet.service.mail.MailServiceImpl

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.