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: 760909 $
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            try {
096                StringBuilder sb = new StringBuilder();
097    
098                int number = message.getMessageNumber();
099                sb.append("messageNumber=[").append(number).append("]");
100    
101                Address[] from = message.getFrom();
102                if (from != null) {
103                    for (Address adr : from) {
104                        sb.append(", from=[").append(adr).append("]");
105                    }
106                }
107    
108                Address[] to = message.getRecipients(Message.RecipientType.TO);
109                if (to != null) {
110                    for (Address adr : to) {
111                        sb.append(", to=[").append(adr).append("]");
112                    }
113                }
114    
115                String subject = message.getSubject();
116                if (subject != null) {
117                    sb.append(", subject=[").append(subject).append("]");
118                }
119    
120                Date sentDate = message.getSentDate();
121                if (sentDate != null) {
122                    sb.append(", sentDate=[").append(DateFormat.getDateTimeInstance().format(sentDate)).append("]");
123                }
124    
125                Date receivedDate = message.getReceivedDate();
126                if (receivedDate != null) {
127                    sb.append(", receivedDate=[").append(DateFormat.getDateTimeInstance().format(receivedDate)).append("]");
128                }
129    
130                return sb.toString();
131            } catch (MessagingException e) {
132                // ignore the error and just return tostring 
133                return message.toString();
134            }
135    
136        }
137    
138       
139    }