001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.camel.component.mail;
019
020 import org.apache.camel.Converter;
021
022 import javax.mail.BodyPart;
023 import javax.mail.Message;
024 import javax.mail.MessagingException;
025 import javax.mail.Multipart;
026 import javax.mail.internet.MimeMultipart;
027 import java.io.IOException;
028
029 /**
030 * @version $Revision: 1.1 $
031 */
032 @Converter
033 public class MailConverters {
034 /**
035 * Converts the given JavaMail message to a String body
036 *
037 * @param message the message
038 * @return the String content
039 * @throws MessagingException
040 * @throws IOException
041 */
042 @Converter
043 public String toString(Message message) throws MessagingException, IOException {
044 Object content = message.getContent();
045 if (content instanceof MimeMultipart) {
046 MimeMultipart multipart = (MimeMultipart) content;
047 if (multipart.getCount() > 0) {
048 BodyPart part = multipart.getBodyPart(0);
049 content = part.getContent();
050 }
051 }
052 if (content != null) {
053 return content.toString();
054 }
055 return null;
056 }
057
058 @Converter
059 public static String toString(Multipart multipart) throws MessagingException, IOException {
060 for (int i = 0, size = multipart.getCount(); i < size; i++) {
061 BodyPart part = multipart.getBodyPart(i);
062 if (part.getContentType().startsWith("text")) {
063 return part.getContent().toString();
064 }
065 }
066 return null;
067 }
068 }