View Javadoc

1   /*
2    * $Id: MailUtils.java 3141 2012-01-21 13:02:42Z andrewinkler $
3    * ============================================================================
4    * Project awtools-mail
5    * Copyright (c) 2004-2010 by Andre Winkler. All rights reserved.
6    * ============================================================================
7    *          GNU LESSER GENERAL PUBLIC LICENSE
8    *  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
9    *
10   *  This library is free software; you can redistribute it and/or
11   *  modify it under the terms of the GNU Lesser General Public
12   *  License as published by the Free Software Foundation; either
13   *  version 2.1 of the License, or (at your option) any later version.
14   *
15   *  This library is distributed in the hope that it will be useful,
16   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   *  Lesser General Public License for more details.
19   *
20   *  You should have received a copy of the GNU Lesser General Public
21   *  License along with this library; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   */
24  
25  package de.awtools.mail;
26  
27  import java.io.IOException;
28  
29  import javax.mail.BodyPart;
30  import javax.mail.Message;
31  import javax.mail.MessagingException;
32  import javax.mail.Multipart;
33  
34  /**
35   * Utility to get the mail text content. This is a simplification and does not
36   * respect other content types than 'text/plain'.
37   * 
38   * @version $LastChangedRevision: 3141 $ $LastChangedDate: 2010-09-12 15:04:04
39   *          +0200 (So, 12 Sep 2010) $
40   * @author by Andre Winkler, $LastChangedBy: andrewinkler $
41   */
42  public class MailUtils {
43  
44  	/** A text formatted message. */
45  	public static final String TEXT_PLAIN = "text/plain";
46  
47  	/**
48  	 * Extrahiert aus einer Mail-Message den Mail-Body.
49  	 * 
50  	 * @param msg
51  	 *            Eine Message.
52  	 * @return Der Mail-Body der Mail-Message.
53  	 * @throws MessagingException
54  	 *             Siehe Exception Beschreibung.
55  	 * @throws IOException
56  	 *             Siehe Exception Beschreibung.
57  	 */
58  	public static String getBody(Message msg) throws MessagingException,
59  			IOException {
60  
61  		String contentType = msg.getContentType();
62  		String mailMessage = null;
63  
64  		if (isTextPlain(contentType)) {
65  			mailMessage = (String) msg.getContent();
66  		} else if (isMultipartMessage(msg)) {
67  			StringBuffer mailText = new StringBuffer();
68  			Multipart mp = (Multipart) msg.getContent();
69  			BodyPart bp;
70  			for (int i = 0; i < mp.getCount(); i++) {
71  				bp = mp.getBodyPart(i);
72  				contentType = bp.getContentType();
73  				if (isTextPlain(contentType)) {
74  					mailText.append((String) bp.getContent());
75  				}
76  			}
77  			mailMessage = mailText.toString();
78  		} else {
79  			throwIOException();
80  		}
81  
82  		return mailMessage;
83  	}
84  
85  	private static void throwIOException() throws IOException {
86  		throw new IOException(
87  				"Content type not supported. It is not text/plain"
88  						+ " and not a multipart mail body type.");
89  	}
90  
91  	/**
92  	 * It this a multiple part message?
93  	 * 
94  	 * @param msg
95  	 *            The message.
96  	 * @return Returns <code>true</code>, if this is a multiple part message.
97  	 * @throws IOException
98  	 *             Something goes wrong.
99  	 * @throws MessagingException
100 	 *             Other things are also very bad.
101 	 */
102 	private static boolean isMultipartMessage(Message msg) throws IOException,
103 			MessagingException {
104 
105 		return (msg.getContent() instanceof Multipart);
106 	}
107 
108 	/**
109 	 * Check content type. Returns <code>true</code>, if content type is of
110 	 * text/plain.
111 	 * 
112 	 * @param contentType
113 	 *            The content type of the mail.
114 	 * @return <code>true</code> if mail has type <code>text/plain</code>.
115 	 */
116 	public static boolean isTextPlain(String contentType) {
117 		boolean isPlainText = false;
118 		if (contentType == null) {
119 			isPlainText = false;
120 		} else if (contentType.length() < TEXT_PLAIN.length()) {
121 			isPlainText = false;
122 		} else {
123 			isPlainText = contentType.substring(0, 10).equals(TEXT_PLAIN);
124 		}
125 		return isPlainText;
126 	}
127 
128 }