001package org.codemonkey.simplejavamail;
002
003import java.util.Properties;
004
005import javax.mail.Session;
006
007/**
008 * Defines the various types of transport protocols and implements respective properties so that a {@link Session} may be configured using a
009 * <code>TransportStrategy</code> implementation.
010 * 
011 * @author Benny Bottema
012 */
013public enum TransportStrategy {
014
015        /**
016         * Simplest possible form: only vanilla ".smtp." property names and no extra properties. Additionally the transport protocol is
017         * explicitly set to smtp.
018         */
019        SMTP_PLAIN {
020                /**
021                 * Here protocol "mail.transport.protocol" is set to "smtp".
022                 * 
023                 * @see TransportStrategy#SMTP_PLAIN
024                 */
025                @Override
026                public Properties generateProperties() {
027                        final Properties props = super.generateProperties();
028                        props.put("mail.transport.protocol", "smtp");
029                        return props;
030                };
031
032                /**
033                 * @return "mail.smtp.host"
034                 */
035                @Override
036                String propertyNameHost() {
037                        return "mail.smtp.host";
038                };
039
040                /**
041                 * @return "mail.smtp.port"
042                 */
043                @Override
044                String propertyNamePort() {
045                        return "mail.smtp.port";
046                };
047
048                /**
049                 * @return "mail.smtp.username"
050                 */
051                @Override
052                String propertyNameUsername() {
053                        return "mail.smtp.username";
054                };
055
056                /**
057                 * @return "mail.smtp.auth"
058                 */
059                @Override
060                String propertyNameAuthenticate() {
061                        return "mail.smtp.auth";
062                };
063        },
064        /**
065         * SMTPS / SSL transport strategy, that returns the ".smtps." variation of the SMTP_PLAIN version. Additionally the transport protocol
066         * is explicitly set to smtps. Finally, he property "mail.smtps.quitwait" is set to false, to get rid of a strange SSL exception:<br>
067         * 
068         * <pre>
069         * javax.mail.MessagingException: Exception reading response;
070         * nested exception is:
071         *      javax.net.ssl.SSLException: Unsupported record version Unknown-50.49
072         * (..)
073         * </pre>
074         * 
075         * <blockquote>The mail is sent but the exception is unwanted. The property <em>quitwait</em> means If set to false, the QUIT command is sent
076         * and the connection is immediately closed. If set to true (the default), causes the transport to wait for the response to the QUIT
077         * command</blockquote><br>
078         * <strong>- <a href="http://www.rgagnon.com/javadetails/java-0570.html">source</a></strong>
079         */
080        SMTP_SSL {
081                /**
082                 * Here protocol "mail.transport.protocol" is set to "smtps" and the quitwait property "mail.smtps.quitwait" is set to "false".
083                 * 
084                 * @see TransportStrategy#SMTP_SSL
085                 */
086                @Override
087                public Properties generateProperties() {
088                        final Properties props = super.generateProperties();
089                        props.put("mail.transport.protocol", "smtps");
090                        props.put("mail.smtps.quitwait", "false");
091                        return props;
092                };
093
094                /**
095                 * @return "mail.smtps.host"
096                 */
097                @Override
098                String propertyNameHost() {
099                        return "mail.smtps.host";
100                };
101
102                /**
103                 * @return "mail.smtps.port"
104                 */
105                @Override
106                String propertyNamePort() {
107                        return "mail.smtps.port";
108                };
109
110                /**
111                 * @return "mail.smtps.username"
112                 */
113                @Override
114                String propertyNameUsername() {
115                        return "mail.smtps.username";
116                };
117
118                /**
119                 * @return "mail.smtps.auth"
120                 */
121                @Override
122                String propertyNameAuthenticate() {
123                        return "mail.smtps.auth";
124                };
125        },
126        /**
127         * <strong>NOTE: this code is in untested beta state</strong>
128         * <p>
129         * Uses standard ".smtp." property names (like {@link TransportStrategy#SMTP_PLAIN}). Additionally the transport protocol is explicitly
130         * set to smtp. Finally, the property "mail.smtp.starttls.enable" is being set to true.
131         */
132        SMTP_TLS {
133
134                /**
135                 * Here protocol "mail.transport.protocol" is set to "smtp" and the tls property "mail.smtp.starttls.enable" is set to "true".
136                 * 
137                 * @see TransportStrategy#SMTP_TLS
138                 */
139                @Override
140                public Properties generateProperties() {
141                        final Properties props = super.generateProperties();
142                        props.put("mail.transport.protocol", "smtp");
143                        props.put("mail.smtp.starttls.enable", "true");
144                        return props;
145                }
146
147                /**
148                 * @return "mail.smtp.host"
149                 */
150                @Override
151                String propertyNameHost() {
152                        return "mail.smtp.host";
153                };
154
155                /**
156                 * @return "mail.smtp.port"
157                 */
158                @Override
159                String propertyNamePort() {
160                        return "mail.smtp.port";
161                };
162
163                /**
164                 * @return "mail.smtp.username"
165                 */
166                @Override
167                String propertyNameUsername() {
168                        return "mail.smtp.username";
169                };
170
171                /**
172                 * @return "mail.smtp.auth"
173                 */
174                @Override
175                String propertyNameAuthenticate() {
176                        return "mail.smtp.auth";
177                };
178
179        };
180
181        /**
182         * Base implementation that simply returns an empty list of properties. Should be overridden by the various strategies where
183         * appropriate.
184         * 
185         * @return An empty <code>Properties</code> instance.
186         */
187        public Properties generateProperties() {
188                return new Properties();
189        }
190
191        abstract String propertyNameHost();
192
193        abstract String propertyNamePort();
194
195        abstract String propertyNameUsername();
196
197        abstract String propertyNameAuthenticate();
198};