001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.component.mail; 018 019 import java.text.DateFormat; 020 import java.util.Date; 021 022 import javax.mail.Address; 023 import javax.mail.Message; 024 import javax.mail.MessagingException; 025 026 /** 027 * Mail utility class. 028 * <p> 029 * Parts of the code copied from Apache ServiceMix. 030 * 031 * @version $Revision: 788248 $ 032 */ 033 public final class MailUtils { 034 035 public static final int DEFAULT_PORT_SMTP = 25; 036 public static final int DEFAULT_PORT_SMTPS = 465; 037 public static final int DEFAULT_PORT_POP3 = 110; 038 public static final int DEFAULT_PORT_POP3S = 995; 039 public static final int DEFAULT_PORT_NNTP = 119; 040 public static final int DEFAULT_PORT_IMAP = 143; 041 public static final int DEFAULT_PORT_IMAPS = 993; 042 043 public static final String PROTOCOL_SMTP = "smtp"; 044 public static final String PROTOCOL_SMTPS = "smtps"; 045 public static final String PROTOCOL_POP3 = "pop3"; 046 public static final String PROTOCOL_POP3S = "pop3s"; 047 public static final String PROTOCOL_NNTP = "nntp"; 048 public static final String PROTOCOL_IMAP = "imap"; 049 public static final String PROTOCOL_IMAPS = "imaps"; 050 051 private MailUtils() { 052 } 053 054 /** 055 * Returns the default port for a given protocol. 056 * <p> 057 * If a protocol could not successfully be determined the default port number for SMTP protocol is returned. 058 * 059 * @param protocol the protocol 060 * @return the default port 061 */ 062 public static int getDefaultPortForProtocol(final String protocol) { 063 int port = DEFAULT_PORT_SMTP; 064 065 if (protocol != null) { 066 if (protocol.equalsIgnoreCase(PROTOCOL_IMAP)) { 067 port = DEFAULT_PORT_IMAP; 068 } else if (protocol.equalsIgnoreCase(PROTOCOL_IMAPS)) { 069 port = DEFAULT_PORT_IMAPS; 070 } else if (protocol.equalsIgnoreCase(PROTOCOL_NNTP)) { 071 port = DEFAULT_PORT_NNTP; 072 } else if (protocol.equalsIgnoreCase(PROTOCOL_POP3)) { 073 port = DEFAULT_PORT_POP3; 074 } else if (protocol.equalsIgnoreCase(PROTOCOL_POP3S)) { 075 port = DEFAULT_PORT_POP3S; 076 } else if (protocol.equalsIgnoreCase(PROTOCOL_SMTP)) { 077 port = DEFAULT_PORT_SMTP; 078 } else if (protocol.equalsIgnoreCase(PROTOCOL_SMTPS)) { 079 port = DEFAULT_PORT_SMTPS; 080 } else { 081 port = DEFAULT_PORT_SMTP; 082 } 083 } 084 085 return port; 086 } 087 088 /** 089 * Gets a log dump of the given message that can be used for tracing etc. 090 * 091 * @param message the Mail message 092 * @return a log string with important fields dumped 093 */ 094 public static String dumpMessage(Message message) { 095 if (message == null) { 096 return "null"; 097 } 098 099 try { 100 StringBuilder sb = new StringBuilder(); 101 102 int number = message.getMessageNumber(); 103 sb.append("messageNumber=[").append(number).append("]"); 104 105 Address[] from = message.getFrom(); 106 if (from != null) { 107 for (Address adr : from) { 108 sb.append(", from=[").append(adr).append("]"); 109 } 110 } 111 112 Address[] to = message.getRecipients(Message.RecipientType.TO); 113 if (to != null) { 114 for (Address adr : to) { 115 sb.append(", to=[").append(adr).append("]"); 116 } 117 } 118 119 String subject = message.getSubject(); 120 if (subject != null) { 121 sb.append(", subject=[").append(subject).append("]"); 122 } 123 124 Date sentDate = message.getSentDate(); 125 if (sentDate != null) { 126 sb.append(", sentDate=[").append(DateFormat.getDateTimeInstance().format(sentDate)).append("]"); 127 } 128 129 Date receivedDate = message.getReceivedDate(); 130 if (receivedDate != null) { 131 sb.append(", receivedDate=[").append(DateFormat.getDateTimeInstance().format(receivedDate)).append("]"); 132 } 133 134 return sb.toString(); 135 } catch (MessagingException e) { 136 // ignore the error and just return tostring 137 return message.toString(); 138 } 139 } 140 }