1
+ package org .simplejavamail .mailer ;
2
+
3
+ import org .simplejavamail .mailer .config .TransportStrategy ;
4
+
5
+ import javax .annotation .Nonnull ;
6
+ import javax .annotation .Nullable ;
7
+ import javax .mail .Session ;
8
+
9
+ import static org .simplejavamail .util .ConfigLoader .Property .SMTP_HOST ;
10
+ import static org .simplejavamail .util .ConfigLoader .Property .SMTP_PASSWORD ;
11
+ import static org .simplejavamail .util .ConfigLoader .Property .SMTP_PORT ;
12
+ import static org .simplejavamail .util .ConfigLoader .Property .SMTP_USERNAME ;
13
+ import static org .simplejavamail .util .ConfigLoader .Property .TRANSPORT_STRATEGY ;
14
+ import static org .simplejavamail .util .ConfigLoader .getProperty ;
15
+ import static org .simplejavamail .util .ConfigLoader .hasProperty ;
16
+
17
+ /**
18
+ * Entry builder used to start a {@link MailerGenericBuiler} and fully configure a Mailer.
19
+ * <p>
20
+ * Any of these methods return a specialized builder, of which there are two:
21
+ * <ul>
22
+ * <li>One to configure a Mailer using a custom {@link Session} instance</li>
23
+ * <li>One to fully configure a Mailer which will produce its own {@link Session} instance</li>
24
+ * </ul>
25
+ *
26
+ * @see MailerFromSessionBuilder
27
+ * @see MailerRegularBuilder
28
+ */
29
+ public class MailerBuilder {
30
+
31
+ /**
32
+ * Delegates to {@link MailerFromSessionBuilder#usingSession(Session)}.
33
+ */
34
+ public static MailerFromSessionBuilder usingSession (@ Nonnull final Session session ) {
35
+ return new MailerFromSessionBuilder ().usingSession (session );
36
+ }
37
+
38
+ /**
39
+ * Delegates to {@link MailerRegularBuilder#withTransportStrategy(TransportStrategy)}.
40
+ */
41
+ public static MailerRegularBuilder withTransportStrategy (@ Nonnull final TransportStrategy transportStrategy ) {
42
+ return new MailerRegularBuilder ().withTransportStrategy (transportStrategy );
43
+ }
44
+
45
+ /**
46
+ * Delegates to {@link MailerRegularBuilder#withSMTPServer(ServerConfig)}.
47
+ */
48
+ public static MailerRegularBuilder withSMTPServer (@ Nonnull final ServerConfig serverConfig ) {
49
+ return new MailerRegularBuilder ().withSMTPServer (serverConfig );
50
+ }
51
+
52
+ /**
53
+ * Delegates to {@link MailerRegularBuilder#withSMTPServer(String, Integer, String, String)}.
54
+ */
55
+ public static MailerRegularBuilder withSMTPServer (@ Nullable final String host , @ Nullable final Integer port , @ Nullable final String username , @ Nullable final String password ) {
56
+ return new MailerRegularBuilder ().withSMTPServer (host , port , username , password );
57
+ }
58
+
59
+ /**
60
+ * Delegates to {@link MailerRegularBuilder#withSMTPServer(String, Integer, String)}.
61
+ */
62
+ public static MailerRegularBuilder withSMTPServer (@ Nullable final String host , @ Nullable final Integer port , @ Nullable final String username ) {
63
+ return new MailerRegularBuilder ().withSMTPServer (host , port , username );
64
+ }
65
+
66
+ /**
67
+ * Delegates to {@link MailerRegularBuilder#withSMTPServer(String, Integer)}.
68
+ */
69
+ public static MailerRegularBuilder withSMTPServer (@ Nullable final String host , @ Nullable final Integer port ) {
70
+ return new MailerRegularBuilder ().withSMTPServer (host , port );
71
+ }
72
+
73
+ /**
74
+ * Delegates to {@link MailerRegularBuilder#withSMTPServerHost(String)}.
75
+ */
76
+ public static MailerRegularBuilder withSMTPServerHost (@ Nullable final String host ) {
77
+ return new MailerRegularBuilder ().withSMTPServerHost (host );
78
+ }
79
+
80
+ /**
81
+ * Delegates to {@link MailerRegularBuilder#withSMTPServerPort(Integer)}.
82
+ */
83
+ public static MailerRegularBuilder withSMTPServerPort (@ Nullable final Integer port ) {
84
+ return new MailerRegularBuilder ().withSMTPServerPort (port );
85
+ }
86
+
87
+ /**
88
+ * Delegates to {@link MailerRegularBuilder#withSMTPServerUsername(String)}.
89
+ */
90
+ public static MailerRegularBuilder withSMTPServerUsername (@ Nullable final String username ) {
91
+ return new MailerRegularBuilder ().withSMTPServerUsername (username );
92
+ }
93
+
94
+ /**
95
+ * Delegates to {@link MailerRegularBuilder#withSMTPServerPassword(String)}.
96
+ */
97
+ public static MailerRegularBuilder withSMTPServerPassword (@ Nullable final String password ) {
98
+ return new MailerRegularBuilder ().withSMTPServerPassword (password );
99
+ }
100
+
101
+ /**
102
+ * Delegates to {@link MailerRegularBuilder#withDebugLogging(Boolean)}
103
+ * <p>
104
+ * <strong>Note:</strong> Assumes you don't want to use your own {@link Session} object (otherwise start with {@link #usingSession(Session)}
105
+ * instead).
106
+ */
107
+ public static MailerRegularBuilder withDebugLogging (Boolean debugLogging ) {
108
+ return new MailerRegularBuilder ().withDebugLogging (debugLogging );
109
+ }
110
+
111
+ /**
112
+ * Shortcuts to {@link MailerRegularBuilder#buildMailer()}. This means that none of the builder methods are used and the configuration completely
113
+ * depends on defaults being configured from property file ("simplejavamail.properties") on the classpath or through programmatic defaults.
114
+ */
115
+ public static Mailer buildMailer () {
116
+ return new MailerRegularBuilder ().buildMailer ();
117
+ }
118
+
119
+ /**
120
+ * Default builder for generating Mailer instances.
121
+ * <p>
122
+ * In addition on generic Mailer setting, this builder is used to configure SMTP server details and transport strategy needed to produce a valid
123
+ * {@link Session} instance.
124
+ *
125
+ * @see TransportStrategy
126
+ */
127
+ public static class MailerRegularBuilder extends MailerGenericBuiler <MailerRegularBuilder > {
128
+
129
+ private String host ;
130
+ private Integer port ;
131
+ private String username ;
132
+ private String password ;
133
+ private TransportStrategy transportStrategy ;
134
+
135
+ /**
136
+ * Sets defaults configured for SMTP host, SMTP port, SMTP username, SMTP password and transport strategy.
137
+ * <p>
138
+ * <strong>Note:</strong> Any builder methods invoked after this will override the default value.
139
+ */
140
+ MailerRegularBuilder () {
141
+ if (hasProperty (SMTP_HOST )) {
142
+ withSMTPServerHost ((String ) getProperty (SMTP_HOST ));
143
+ }
144
+ if (hasProperty (SMTP_PORT )) {
145
+ withSMTPServerPort ((Integer ) getProperty (SMTP_PORT ));
146
+ }
147
+ if (hasProperty (SMTP_USERNAME )) {
148
+ withSMTPServerUsername ((String ) getProperty (SMTP_USERNAME ));
149
+ }
150
+ if (hasProperty (SMTP_PASSWORD )) {
151
+ withSMTPServerPassword ((String ) getProperty (SMTP_PASSWORD ));
152
+ }
153
+ withTransportStrategy (TransportStrategy .SMTP );
154
+ if (hasProperty (TRANSPORT_STRATEGY )) {
155
+ withTransportStrategy ((TransportStrategy ) getProperty (TRANSPORT_STRATEGY ));
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Sets the optional transport strategy of this mailer. Will default to {@link TransportStrategy#SMTP} is left empty.
161
+ */
162
+ public MailerRegularBuilder withTransportStrategy (@ Nonnull final TransportStrategy transportStrategy ) {
163
+ this .transportStrategy = transportStrategy ;
164
+ return this ;
165
+ }
166
+
167
+ /**
168
+ * @param host Delegates to {@link #withSMTPServerHost(String)}.
169
+ * @param port Delegates to {@link #withSMTPServerPort(Integer)}.
170
+ * @param username Delegates to {@link #withSMTPServerUsername(String)}.
171
+ * @param password Delegates to {@link #withSMTPServerPassword(String)}.
172
+ */
173
+ public MailerRegularBuilder withSMTPServer (@ Nullable final String host , @ Nullable final Integer port , @ Nullable final String username , @ Nullable final String password ) {
174
+ return withSMTPServerHost (host )
175
+ .withSMTPServerPort (port )
176
+ .withSMTPServerUsername (username )
177
+ .withSMTPServerPassword (password );
178
+ }
179
+
180
+ /**
181
+ * @param host Delegates to {@link #withSMTPServerHost(String)}.
182
+ * @param port Delegates to {@link #withSMTPServerPort(Integer)}.
183
+ * @param username Delegates to {@link #withSMTPServerUsername(String)}.
184
+ */
185
+ public MailerRegularBuilder withSMTPServer (@ Nullable final String host , @ Nullable final Integer port , @ Nullable final String username ) {
186
+ return withSMTPServerHost (host )
187
+ .withSMTPServerPort (port )
188
+ .withSMTPServerUsername (username );
189
+ }
190
+
191
+ /**
192
+ * @param host Delegates to {@link #withSMTPServerHost(String)}.
193
+ * @param port Delegates to {@link #withSMTPServerPort(Integer)}.
194
+ */
195
+ public MailerRegularBuilder withSMTPServer (@ Nullable final String host , @ Nullable final Integer port ) {
196
+ return withSMTPServerHost (host )
197
+ .withSMTPServerPort (port );
198
+ }
199
+
200
+ /**
201
+ * Delegates for each contained property respectively to:
202
+ * <ul>
203
+ * <li>{@link #withSMTPServerHost(String)}</li>
204
+ * <li>{@link #withSMTPServerPort(Integer)}</li>
205
+ * <li>{@link #withSMTPServerUsername(String)}</li>
206
+ * <li>{@link #withSMTPServerPassword(String)}</li>
207
+ * </ul>
208
+ */
209
+ public MailerRegularBuilder withSMTPServer (@ Nonnull final ServerConfig serverConfig ) {
210
+ return withSMTPServerHost (serverConfig .getHost ())
211
+ .withSMTPServerPort (serverConfig .getPort ())
212
+ .withSMTPServerUsername (serverConfig .getUsername ())
213
+ .withSMTPServerPassword (serverConfig .getPassword ());
214
+ }
215
+
216
+ /**
217
+ * Sets the optional SMTP host. Will default to pre-configured property if left empty.
218
+ */
219
+ public MailerRegularBuilder withSMTPServerHost (@ Nullable final String host ) {
220
+ this .host = host ;
221
+ return this ;
222
+ }
223
+
224
+ /**
225
+ * Sets the optional SMTP port. Will default to pre-configured property if left empty.
226
+ */
227
+ public MailerRegularBuilder withSMTPServerPort (@ Nullable final Integer port ) {
228
+ this .port = port ;
229
+ return this ;
230
+ }
231
+
232
+ /**
233
+ * Sets the optional SMTP username. Will default to pre-configured property if left empty.
234
+ */
235
+ public MailerRegularBuilder withSMTPServerUsername (@ Nullable final String username ) {
236
+ this .username = username ;
237
+ return this ;
238
+ }
239
+
240
+ /**
241
+ * Sets the optional SMTP password. Will default to pre-configured property if left empty.
242
+ */
243
+ public MailerRegularBuilder withSMTPServerPassword (@ Nullable final String password ) {
244
+ this .password = password ;
245
+ return this ;
246
+ }
247
+
248
+ /**
249
+ * Builds the actual {@link Mailer} instance with everything configured on this builder instance.
250
+ * <p>
251
+ * For all configurable values: if omitted, a default value will be attempted by looking at property files or manually defined defauls.
252
+ */
253
+ public Mailer buildMailer () {
254
+ return new Mailer (this );
255
+ }
256
+
257
+ /**
258
+ * For internal use.
259
+ */
260
+ ServerConfig buildServerConfig () {
261
+ return new ServerConfig (getHost (), getPort (), getUsername (), getPassword ());
262
+ }
263
+
264
+ public String getHost () {
265
+ return host ;
266
+ }
267
+
268
+ public Integer getPort () {
269
+ return port ;
270
+ }
271
+
272
+ public String getUsername () {
273
+ return username ;
274
+ }
275
+
276
+ public String getPassword () {
277
+ return password ;
278
+ }
279
+
280
+ public TransportStrategy getTransportStrategy () {
281
+ return transportStrategy ;
282
+ }
283
+ }
284
+ }
0 commit comments