001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/framework/mail/MailHelper.java $ 002 /*---------------- FILE HEADER ------------------------------------------ 003 004 This file is part of deegree. 005 Copyright (C) 2001-2008 by: 006 EXSE, Department of Geography, University of Bonn 007 http://www.giub.uni-bonn.de/deegree/ 008 lat/lon GmbH 009 http://www.lat-lon.de 010 011 This library is free software; you can redistribute it and/or 012 modify it under the terms of the GNU Lesser General Public 013 License as published by the Free Software Foundation; either 014 version 2.1 of the License, or (at your option) any later version. 015 016 This library is distributed in the hope that it will be useful, 017 but WITHOUT ANY WARRANTY; without even the implied warranty of 018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 019 Lesser General Public License for more details. 020 021 You should have received a copy of the GNU Lesser General Public 022 License along with this library; if not, write to the Free Software 023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 024 025 Contact: 026 027 Andreas Poth 028 lat/lon GmbH 029 Aennchenstr. 19 030 53115 Bonn 031 Germany 032 E-Mail: poth@lat-lon.de 033 034 Prof. Dr. Klaus Greve 035 Department of Geography 036 University of Bonn 037 Meckenheimer Allee 166 038 53115 Bonn 039 Germany 040 E-Mail: greve@giub.uni-bonn.de 041 042 043 ---------------------------------------------------------------------------*/ 044 package org.deegree.framework.mail; 045 046 // J2EE 1.2 047 import java.io.File; 048 import java.util.ArrayList; 049 import java.util.Date; 050 import java.util.List; 051 import java.util.Properties; 052 053 import javax.mail.Message; 054 import javax.mail.Multipart; 055 import javax.mail.Session; 056 import javax.mail.Transport; 057 import javax.mail.internet.InternetAddress; 058 import javax.mail.internet.MimeBodyPart; 059 import javax.mail.internet.MimeMessage; 060 import javax.mail.internet.MimeMultipart; 061 062 import org.deegree.framework.log.ILogger; 063 import org.deegree.framework.log.LoggerFactory; 064 import org.deegree.framework.util.CharsetUtils; 065 import org.deegree.framework.util.StringTools; 066 import org.deegree.i18n.Messages; 067 068 /** 069 * A helper class to create and send mail. 070 * 071 * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe </A> 072 * @author last edited by: $Author: apoth $ 073 * 074 * @version $Revision: 9339 $,$Date: 2007-12-27 13:31:52 +0100 (Do, 27 Dez 2007) $ 075 * 076 * @see javax.mail.Message 077 * @see javax.mail.internet.MimeMessage 078 */ 079 080 public final class MailHelper { 081 082 private static final ILogger LOG = LoggerFactory.getLogger( MailHelper.class ); 083 084 /** 085 * Creates a mail helper to send a message. 086 * 087 */ 088 public MailHelper() { 089 } 090 091 /** 092 * This method creates an email message and sends it using the J2EE mail services 093 * 094 * @param eMess 095 * a email message 096 * @param mailHost 097 * name of the SMTP host 098 * 099 * @throws SendMailException 100 * an exception if the message is undeliverable 101 */ 102 public static void createAndSendMail( MailMessage eMess, String mailHost ) 103 throws SendMailException { 104 Properties p = System.getProperties(); 105 p.put( "mail.smtp.host", mailHost ); 106 new MailHelper().createAndSendMail( eMess, Session.getDefaultInstance( p ) ); 107 } 108 109 /** 110 * This method creates an email message and sends it using the J2EE mail services 111 * 112 * @param eMess 113 * a email message 114 * @param mailHost 115 * name of the SMTP host 116 * @param attachment 117 * Object to attach to a mail 118 * @param mimeType 119 * mimetype of the attchment 120 * @throws SendMailException 121 */ 122 public static void createAndSendMail( MailMessage eMess, String mailHost, Object[] attachment, 123 String[] mimeType ) 124 throws SendMailException { 125 Properties p = System.getProperties(); 126 p.put( "mail.smtp.host", mailHost ); 127 new MailHelper().createAndSendMail( eMess, Session.getDefaultInstance( p ), attachment, 128 mimeType ); 129 } 130 131 /** 132 * This method creates an email message and sends it using the J2EE mail services 133 * 134 * @param eMess 135 * a email message 136 * @param mailHost 137 * name of the SMTP host 138 * @param files 139 * files to attach to a mail 140 * @param mimeType 141 * mimetype of the attchment 142 * @throws SendMailException 143 */ 144 public static void createAndSendMail( MailMessage eMess, String mailHost, File[] files, 145 String[] mimeType ) 146 throws SendMailException { 147 Properties p = System.getProperties(); 148 p.put( "mail.smtp.host", mailHost ); 149 new MailHelper().createAndSendMail( eMess, Session.getDefaultInstance( p ), files, 150 mimeType ); 151 } 152 153 /** 154 * This method creates an email message and sends it using the J2EE mail services 155 * 156 * @param eMess 157 * a email message 158 * @param session 159 * 160 * @throws SendMailException 161 * an exception if the message is undeliverable 162 * 163 * @see javax.mail.Transport 164 * @see <a href="http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Resources4.html#63060">J2EE Mail 165 * Session connection </a> 166 */ 167 public void createAndSendMail( MailMessage eMess, Session session ) 168 throws SendMailException { 169 createAndSendMail( eMess, session, null, null ); 170 } 171 172 /** 173 * This method creates an email message and sends it using the J2EE mail services 174 * 175 * @param eMess 176 * an email message 177 * @param session 178 * @param attachment 179 * Object to attach to a mail 180 * @param mimeType 181 * mimetype of the attchment 182 * @throws SendMailException 183 * an exception if the message is undeliverable 184 */ 185 public void createAndSendMail( MailMessage eMess, Session session, Object[] attachment, 186 String[] mimeType ) 187 throws SendMailException { 188 if ( eMess == null || !eMess.isValid() ) { 189 throw new SendMailException( "Not a valid e-mail!" ); 190 } 191 try { 192 int k = eMess.getMessageBody().length() > 60 ? 60 : eMess.getMessageBody().length() - 1; 193 LOG.logDebug( StringTools.concat( 500, "Sending message, From: ", eMess.getSender(), 194 "\nTo: ", eMess.getReceiver(), "\nSubject: ", 195 eMess.getSubject(), "\nContents: ", 196 eMess.getMessageBody().substring( 0, k ), "..." ) ); 197 // construct the message 198 MimeMessage msg = new MimeMessage( session ); 199 msg.setFrom( new InternetAddress( eMess.getSender() ) ); 200 msg.setRecipients( Message.RecipientType.TO, 201 InternetAddress.parse( eMess.getReceiver(), false ) ); 202 msg.setSubject( eMess.getSubject(), CharsetUtils.getSystemCharset() ); 203 msg.setHeader( "X-Mailer", "JavaMail" ); 204 msg.setSentDate( new Date() ); 205 206 // create and fill the first message part 207 List<MimeBodyPart> mbps = new ArrayList<MimeBodyPart>(); 208 MimeBodyPart mbp = new MimeBodyPart(); 209 mbp.setContent( eMess.getMessageBody(), eMess.getMimeType() ); 210 mbps.add( mbp ); 211 if ( attachment != null ) { 212 if ( attachment.length != mimeType.length ) { 213 throw new SendMailException( Messages.getMessage( "FRAMEWORK_MAIL_ATTACH" ) ); 214 } 215 for ( int i = 0; i < attachment.length; i++ ) { 216 mbp = new MimeBodyPart(); 217 mbp.setContent( attachment[i], mimeType[i] ); 218 mbp.setFileName( "file" + i ); 219 mbps.add( mbp ); 220 } 221 } 222 // create the Multipart and add its parts to it 223 Multipart mp = new MimeMultipart(); 224 for ( int i = 0; i < mbps.size(); i++ ) { 225 mp.addBodyPart( mbps.get( i ) ); 226 } 227 msg.setContent( mp ); 228 // send the mail off 229 Transport.send( msg ); 230 LOG.logDebug( "Mail sent successfully! Header=", eMess.getHeader() ); 231 } catch ( Exception e ) { 232 LOG.logError( e.getMessage(), e ); 233 String s = Messages.getMessage( "FRAMEWORK_MAIL_SEND_ERROR", eMess.getHeader() ); 234 throw new SendMailException( s, e ); 235 } 236 } 237 238 /** 239 * This method creates an email message and sends it using the J2EE mail services 240 * 241 * @param eMess 242 * an email message 243 * @param session 244 * @param files 245 * files to attach to a mail 246 * @param mimeType 247 * mimetype of the attchment 248 * @throws SendMailException 249 * an exception if the message is undeliverable 250 */ 251 public void createAndSendMail( MailMessage eMess, Session session, File[] files, 252 String[] mimeType ) 253 throws SendMailException { 254 if ( eMess == null || !eMess.isValid() ) { 255 throw new SendMailException( "Not a valid e-mail!" ); 256 } 257 try { 258 int k = eMess.getMessageBody().length() > 60 ? 60 : eMess.getMessageBody().length() - 1; 259 LOG.logDebug( StringTools.concat( 500, "Sending message, From: ", eMess.getSender(), 260 "\nTo: ", eMess.getReceiver(), "\nSubject: ", 261 eMess.getSubject(), "\nContents: ", 262 eMess.getMessageBody().substring( 0, k ), "..." ) ); 263 // construct the message 264 MimeMessage msg = new MimeMessage( session ); 265 msg.setFrom( new InternetAddress( eMess.getSender() ) ); 266 msg.setRecipients( Message.RecipientType.TO, 267 InternetAddress.parse( eMess.getReceiver(), false ) ); 268 msg.setSubject( eMess.getSubject(), CharsetUtils.getSystemCharset() ); 269 msg.setHeader( "X-Mailer", "JavaMail" ); 270 msg.setSentDate( new Date() ); 271 272 // create and fill the first message part 273 List<MimeBodyPart> mbps = new ArrayList<MimeBodyPart>(); 274 MimeBodyPart mbp = new MimeBodyPart(); 275 mbp.setContent( eMess.getMessageBody(), eMess.getMimeType() ); 276 mbps.add( mbp ); 277 if ( files != null ) { 278 if ( files.length != mimeType.length ) { 279 throw new SendMailException( Messages.getMessage( "FRAMEWORK_MAIL_ATTACH" ) ); 280 } 281 for ( int i = 0; i < files.length; i++ ) { 282 mbp = new MimeBodyPart(); 283 mbp.attachFile( files[i] ); 284 mbp.setHeader( "Content-Type", mimeType[i] ); 285 mbps.add( mbp ); 286 } 287 } 288 // create the Multipart and add its parts to it 289 Multipart mp = new MimeMultipart(); 290 for ( int i = 0; i < mbps.size(); i++ ) { 291 mp.addBodyPart( mbps.get( i ) ); 292 } 293 msg.setContent( mp ); 294 // send the mail off 295 Transport.send( msg ); 296 LOG.logDebug( "Mail sent successfully! Header=", eMess.getHeader() ); 297 } catch ( Exception e ) { 298 LOG.logError( e.getMessage(), e ); 299 String s = Messages.getMessage( "FRAMEWORK_MAIL_SEND_ERROR", eMess.getHeader() ); 300 throw new SendMailException( s, e ); 301 } 302 } 303 }