Code Search for Developers
 
 
  

JRMailDelivery.java from redshed at Krugle


Show JRMailDelivery.java syntax highlighted

// JRMailDelivery.java
//
// Copyright (c) 2002 Red Shed Software. All rights reserved.
// by Jonathan 'Wolf' Rentzsch (jon at redshed dot net)
// 
// Tue Mar 19 2002 wolf: Created.

import com.webobjects.foundation.*;
import com.webobjects.appserver.*;

import org.apache.log4j.Category;

import java.util.Properties;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class JRMailDelivery {
        public
        static
        JRMailDelivery
    sharedInstance() {
        if( _sharedInstance == null )
            _sharedInstance = new JRMailDelivery();
        return _sharedInstance;
    }
    
        protected
    JRMailDelivery() {
        Properties properties = new Properties();
        JAssert.notEmpty( WOApplication.application().SMTPHost() );
        properties.put( "mail.smtp.host", WOApplication.application().SMTPHost() );
        _session = javax.mail.Session.getDefaultInstance( properties );
        properties = null;
    }
    
        public
        String
    composePlainTextEmail(
        String	fromEmailAddress,
        NSArray	toEmailAddresses,
        NSArray	bccEmailAddresses,
        String	subject,
        String	message,
        boolean	sendNow )
    {
        JAssert.notEmpty( fromEmailAddress );
        JAssert.notNull( toEmailAddresses );
        JAssert.greaterThan( toEmailAddresses.count(), 0 );
        JAssert.notEmpty( message );
        
        JAssert.notNull( _session );
        
        MimeMessage smtpMessage = newMimeMessage( fromEmailAddress, toEmailAddresses, bccEmailAddresses, subject, message, "text/plain", sendNow );
            
	return mimeMessageToString( smtpMessage );
    }
    
        public
        String
    composeComponentEmail(
        String		fromEmailAddress,
        NSArray		toEmailAddresses,
        NSArray		bccEmailAddresses,
        String		subject,
        WOComponent	component,
        boolean		sendNow )
    {
        JAssert.notEmpty( fromEmailAddress );
        JAssert.notNull( toEmailAddresses );
        JAssert.greaterThan( toEmailAddresses.count(), 0 );
        JAssert.notNull( component );
        JAssert.notNull( component.context() );
        
        JAssert.notNull( _session );
        
        WOSession session = component.context()._session();
        String response;
        
        component.context()._generateCompleteURLs();
        if( session == null ) {
            response = component.generateResponse().contentString();
        } else {
            boolean oldStoresIDsInURLs = session.storesIDsInURLs();
            session.setStoresIDsInURLs( true );
            response = component.generateResponse().contentString();
            session.setStoresIDsInURLs( oldStoresIDsInURLs );
        }
        component.context()._generateRelativeURLs();
        
        //--
        
        MimeMessage smtpMessage = newMimeMessage( fromEmailAddress, toEmailAddresses, bccEmailAddresses, subject, response, "text/html", sendNow );
            
	return mimeMessageToString( smtpMessage );
    }
    
        public
        void
    sendEmail( String mailString ) {
        JAssert.notEmpty( mailString );
        JAssert.notNull( _session );
        
        ByteArrayInputStream bais = new ByteArrayInputStream( mailString.getBytes() );
        try {
            MimeMessage smtpMessage = new MimeMessage( _session, bais );
            //Transport.send( smtpMessage );
            new ThreadedTransportSender( smtpMessage );
        } catch( Exception x ) {
            System.err.println( x );
        }
    }
    
        public
        String
    toString() {
        return "<JRMailDelivery smtpHost="+WOApplication.application().SMTPHost()+">";
    }
    
    // Private Implementation.
    
    private static Category log = Category.getInstance( JRMailDelivery.class.getName() );
    private static JRMailDelivery _sharedInstance = null;
    private javax.mail.Session _session = null;
    
        private
        MimeMessage
    newMimeMessage(
        String	fromEmailAddress,
        NSArray	toEmailAddresses,
        NSArray	bccEmailAddresses,
        String	subject,
        String	message,
        String	contentType,
        boolean	sendNow )
    {
        JAssert.notEmpty( fromEmailAddress );
        JAssert.notNull( toEmailAddresses );
        JAssert.greaterThan( toEmailAddresses.count(), 0 );
        JAssert.notEmpty( message );
        JAssert.notEmpty( contentType );
        
        JAssert.notNull( _session );
        
        MimeMessage smtpMessage = null;
        
        try {
            smtpMessage = new MimeMessage( _session );
            smtpMessage.setFrom( new InternetAddress( fromEmailAddress ) );
            
            java.util.Enumeration addressEnumerator = toEmailAddresses.objectEnumerator();
            while( addressEnumerator.hasMoreElements() ) {
                String address = (String) addressEnumerator.nextElement();
                smtpMessage.addRecipient( Message.RecipientType.TO, new InternetAddress( address ) );
            }
            
            if( bccEmailAddresses != null && bccEmailAddresses.count() > 0 ) {
                addressEnumerator = bccEmailAddresses.objectEnumerator();
                while( addressEnumerator.hasMoreElements() ) {
                    String address = (String) addressEnumerator.nextElement();
                    smtpMessage.addRecipient( Message.RecipientType.BCC, new InternetAddress( address ) );
                }
            }
            
            smtpMessage.setSubject( subject );
            smtpMessage.setContent( message, contentType );
            
            if( sendNow )
                new ThreadedTransportSender( smtpMessage );
                //Transport.send( smtpMessage );
        } catch( Exception x ) {
            log.error( x );
        }
        
        return smtpMessage;
    }
    
        private
        String
    mimeMessageToString( MimeMessage smtpMessage ) {
        JAssert.notNull( smtpMessage );
        
        String result = null;
        
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream( 1024 );
            smtpMessage.writeTo( baos );
            result = baos.toString();
        } catch( Exception x ) {
            log.error( x );
        }
        
        return result;
    }
    
    private class ThreadedTransportSender extends Thread {
        private MimeMessage _smtpMessage;
        public ThreadedTransportSender( MimeMessage smtpMessage ) {
            _smtpMessage = smtpMessage;
            start();
        }
        public void run() {
            try {
                Transport.send( _smtpMessage );
            } catch( javax.mail.MessagingException x ) {
                JRMailDelivery.log.error( x );
            }
        }
    }
}



See more files for this project here

redshed

Code for Mac+WebObjects.

Project homepage: http://sourceforge.net/projects/redshed
Programming language(s): C,Java,Objective C
License: other

  JRMailDelivery.pbproj/
    project.pbxproj
  PlainTextEmail.wo/
    PlainTextEmail.html
  HTML2PlainText.java
  JRMailDelivery.java
  PlainTextEmail.api
  PlainTextEmail.java
  activation.jar
  jaf1_0_1.zip
  javamail-1_2.zip
  mail.jar