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.net.URI; 020 import java.util.HashMap; 021 import java.util.Map; 022 import java.util.Properties; 023 import javax.mail.Authenticator; 024 import javax.mail.Message; 025 import javax.mail.PasswordAuthentication; 026 import javax.mail.Session; 027 028 import org.apache.camel.RuntimeCamelException; 029 import org.springframework.mail.javamail.JavaMailSender; 030 import org.springframework.mail.javamail.JavaMailSenderImpl; 031 032 /** 033 * Represents the configuration data for communicating over email 034 * 035 * @version 036 */ 037 public class MailConfiguration implements Cloneable { 038 039 private JavaMailSender javaMailSender; 040 private Properties javaMailProperties; 041 private Properties additionalJavaMailProperties; 042 private String protocol; 043 private String host; 044 private int port = -1; 045 private String username; 046 private String password; 047 private String subject; 048 private Session session; 049 private String defaultEncoding; 050 private String from = MailConstants.MAIL_DEFAULT_FROM; 051 private String folderName = MailConstants.MAIL_DEFAULT_FOLDER; 052 private boolean delete; 053 private boolean unseen = true; 054 private boolean ignoreUriScheme; 055 private Map<Message.RecipientType, String> recipients = new HashMap<Message.RecipientType, String>(); 056 private int fetchSize = -1; 057 private boolean debugMode; 058 private long connectionTimeout = MailConstants.MAIL_DEFAULT_CONNECTION_TIMEOUT; 059 private boolean dummyTrustManager; 060 private String contentType = "text/plain"; 061 private String alternativeBodyHeader = MailConstants.MAIL_ALTERNATIVE_BODY; 062 private boolean useInlineAttachments; 063 private boolean ignoreUnsupportedCharset; 064 065 public MailConfiguration() { 066 } 067 068 /** 069 * Returns a copy of this configuration 070 */ 071 public MailConfiguration copy() { 072 try { 073 MailConfiguration copy = (MailConfiguration) clone(); 074 // must set a new recipients map as clone just reuse the same reference 075 copy.recipients = new HashMap<Message.RecipientType, String>(); 076 copy.recipients.putAll(this.recipients); 077 return copy; 078 } catch (CloneNotSupportedException e) { 079 throw new RuntimeCamelException(e); 080 } 081 } 082 083 public void configure(URI uri) { 084 String value = uri.getHost(); 085 if (value != null) { 086 setHost(value); 087 } 088 089 if (!isIgnoreUriScheme()) { 090 String scheme = uri.getScheme(); 091 if (scheme != null) { 092 setProtocol(scheme); 093 } 094 } 095 096 String userInfo = uri.getUserInfo(); 097 if (userInfo != null) { 098 setUsername(userInfo); 099 } 100 101 int port = uri.getPort(); 102 if (port > 0) { 103 setPort(port); 104 } else if (port <= 0 && this.port <= 0) { 105 // resolve default port if no port number was provided, and not already configured with a port number 106 setPort(MailUtils.getDefaultPortForProtocol(uri.getScheme())); 107 } 108 } 109 110 protected JavaMailSenderImpl createJavaMailSender() { 111 JavaMailSenderImpl answer = new JavaMailSenderImpl(); 112 113 if (javaMailProperties != null) { 114 answer.setJavaMailProperties(javaMailProperties); 115 } else { 116 // set default properties if none provided 117 answer.setJavaMailProperties(createJavaMailProperties()); 118 // add additional properties if provided 119 if (additionalJavaMailProperties != null) { 120 answer.getJavaMailProperties().putAll(additionalJavaMailProperties); 121 } 122 } 123 124 if (defaultEncoding != null) { 125 answer.setDefaultEncoding(defaultEncoding); 126 } 127 if (host != null) { 128 answer.setHost(host); 129 } 130 if (port >= 0) { 131 answer.setPort(port); 132 } 133 if (username != null) { 134 answer.setUsername(username); 135 } 136 if (password != null) { 137 answer.setPassword(password); 138 } 139 if (protocol != null) { 140 answer.setProtocol(protocol); 141 } 142 if (session != null) { 143 answer.setSession(session); 144 } else { 145 // use our authenticator that does no live user interaction but returns the already configured username and password 146 Session session = Session.getInstance(answer.getJavaMailProperties(), getAuthenticator()); 147 // sets the debug mode of the underlying mail framework 148 session.setDebug(debugMode); 149 answer.setSession(session); 150 } 151 152 return answer; 153 } 154 155 private Properties createJavaMailProperties() { 156 // clone the system properties and set the java mail properties 157 Properties properties = (Properties)System.getProperties().clone(); 158 properties.put("mail." + protocol + ".connectiontimeout", connectionTimeout); 159 properties.put("mail." + protocol + ".timeout", connectionTimeout); 160 properties.put("mail." + protocol + ".host", host); 161 properties.put("mail." + protocol + ".port", "" + port); 162 if (username != null) { 163 properties.put("mail." + protocol + ".user", username); 164 properties.put("mail.user", username); 165 properties.put("mail." + protocol + ".auth", "true"); 166 } else { 167 properties.put("mail." + protocol + ".auth", "false"); 168 } 169 properties.put("mail.transport.protocol", protocol); 170 properties.put("mail.store.protocol", protocol); 171 properties.put("mail.host", host); 172 173 if (debugMode) { 174 // add more debug for the SSL communication as well 175 properties.put("javax.net.debug", "all"); 176 } 177 178 if (dummyTrustManager && isSecureProtocol()) { 179 // set the custom SSL properties 180 properties.put("mail." + protocol + ".socketFactory.class", "org.apache.camel.component.mail.security.DummySSLSocketFactory"); 181 properties.put("mail." + protocol + ".socketFactory.fallback", "false"); 182 properties.put("mail." + protocol + ".socketFactory.port", "" + port); 183 } 184 185 return properties; 186 } 187 188 /** 189 * Is the used protocol to be secure or not 190 */ 191 public boolean isSecureProtocol() { 192 return this.protocol.equalsIgnoreCase("smtps") || this.protocol.equalsIgnoreCase("pop3s") 193 || this.protocol.equalsIgnoreCase("imaps"); 194 } 195 196 /** 197 * Returns an authenticator object for use in sessions 198 */ 199 public Authenticator getAuthenticator() { 200 return new Authenticator() { 201 protected PasswordAuthentication getPasswordAuthentication() { 202 return new PasswordAuthentication(getUsername(), getPassword()); 203 } 204 }; 205 } 206 207 public String getMailStoreLogInformation() { 208 String ssl = ""; 209 if (isSecureProtocol()) { 210 ssl = " (SSL enabled" + (dummyTrustManager ? " using DummyTrustManager)" : ")"); 211 } 212 213 return protocol + "://" + host + ":" + port + ssl + ", folder=" + folderName; 214 } 215 216 // Properties 217 // ------------------------------------------------------------------------- 218 219 public JavaMailSender getJavaMailSender() { 220 return javaMailSender; 221 } 222 223 public void setJavaMailSender(JavaMailSender javaMailSender) { 224 this.javaMailSender = javaMailSender; 225 } 226 227 public String getDefaultEncoding() { 228 return defaultEncoding; 229 } 230 231 public void setDefaultEncoding(String defaultEncoding) { 232 this.defaultEncoding = defaultEncoding; 233 } 234 235 public String getHost() { 236 return host; 237 } 238 239 public void setHost(String host) { 240 this.host = host; 241 } 242 243 public Properties getJavaMailProperties() { 244 return javaMailProperties; 245 } 246 247 /** 248 * Sets the java mail options. Will clear any default properties and only use the properties 249 * provided for this method. 250 */ 251 public void setJavaMailProperties(Properties javaMailProperties) { 252 this.javaMailProperties = javaMailProperties; 253 } 254 255 public Properties getAdditionalJavaMailProperties() { 256 if (additionalJavaMailProperties == null) { 257 additionalJavaMailProperties = new Properties(); 258 } 259 return additionalJavaMailProperties; 260 } 261 262 /** 263 * Sets additional java mail properties, that will append/override any default properties 264 * that is set based on all the other options. This is useful if you need to add some 265 * special options but want to keep the others as is. 266 */ 267 public void setAdditionalJavaMailProperties(Properties additionalJavaMailProperties) { 268 this.additionalJavaMailProperties = additionalJavaMailProperties; 269 } 270 271 public String getPassword() { 272 return password; 273 } 274 275 public void setPassword(String password) { 276 this.password = password; 277 } 278 279 public String getSubject() { 280 return subject; 281 } 282 283 public void setSubject(String subject) { 284 this.subject = subject; 285 } 286 287 public int getPort() { 288 return port; 289 } 290 291 public void setPort(int port) { 292 this.port = port; 293 } 294 295 public String getProtocol() { 296 return protocol; 297 } 298 299 public void setProtocol(String protocol) { 300 this.protocol = protocol; 301 } 302 303 public Session getSession() { 304 return session; 305 } 306 307 public void setSession(Session session) { 308 this.session = session; 309 } 310 311 public String getUsername() { 312 return username; 313 } 314 315 public void setUsername(String username) { 316 this.username = username; 317 if (getRecipients().size() == 0) { 318 // set default destination to username@host for backwards compatibility 319 // can be overridden by URI parameters 320 String address = username; 321 if (address.indexOf("@") == -1) { 322 address += "@" + host; 323 } 324 setTo(address); 325 } 326 } 327 328 public String getFrom() { 329 return from; 330 } 331 332 public void setFrom(String from) { 333 this.from = from; 334 } 335 336 public boolean isDelete() { 337 return delete; 338 } 339 340 public void setDelete(boolean delete) { 341 this.delete = delete; 342 } 343 344 public String getFolderName() { 345 return folderName; 346 } 347 348 public void setFolderName(String folderName) { 349 this.folderName = folderName; 350 } 351 352 public boolean isIgnoreUriScheme() { 353 return ignoreUriScheme; 354 } 355 356 public void setIgnoreUriScheme(boolean ignoreUriScheme) { 357 this.ignoreUriScheme = ignoreUriScheme; 358 } 359 360 public boolean isUnseen() { 361 return unseen; 362 } 363 364 public void setUnseen(boolean unseen) { 365 this.unseen = unseen; 366 } 367 368 /** 369 * Sets the <tt>To</tt> email address. Separate multiple email addresses with comma. 370 */ 371 public void setTo(String address) { 372 recipients.put(Message.RecipientType.TO, address); 373 } 374 375 /** 376 * Sets the <tt>CC</tt> email address. Separate multiple email addresses with comma. 377 */ 378 public void setCC(String address) { 379 recipients.put(Message.RecipientType.CC, address); 380 } 381 382 /** 383 * Sets the <tt>BCC</tt> email address. Separate multiple email addresses with comma. 384 */ 385 public void setBCC(String address) { 386 recipients.put(Message.RecipientType.BCC, address); 387 } 388 389 public Map<Message.RecipientType, String> getRecipients() { 390 return recipients; 391 } 392 393 public int getFetchSize() { 394 return fetchSize; 395 } 396 397 public void setFetchSize(int fetchSize) { 398 this.fetchSize = fetchSize; 399 } 400 401 public boolean isDebugMode() { 402 return debugMode; 403 } 404 405 public void setDebugMode(boolean debugMode) { 406 this.debugMode = debugMode; 407 } 408 409 public long getConnectionTimeout() { 410 return connectionTimeout; 411 } 412 413 public void setConnectionTimeout(long connectionTimeout) { 414 this.connectionTimeout = connectionTimeout; 415 } 416 417 public boolean isDummyTrustManager() { 418 return dummyTrustManager; 419 } 420 421 public void setDummyTrustManager(boolean dummyTrustManager) { 422 this.dummyTrustManager = dummyTrustManager; 423 } 424 425 public String getContentType() { 426 return contentType; 427 } 428 429 public void setContentType(String contentType) { 430 this.contentType = contentType; 431 } 432 433 public String getAlternativeBodyHeader() { 434 return alternativeBodyHeader; 435 } 436 437 public void setAlternativeBodyHeader(String alternativeBodyHeader) { 438 this.alternativeBodyHeader = alternativeBodyHeader; 439 } 440 441 public boolean isUseInlineAttachments() { 442 return useInlineAttachments; 443 } 444 445 public void setUseInlineAttachments(boolean useInlineAttachments) { 446 this.useInlineAttachments = useInlineAttachments; 447 } 448 449 public boolean isIgnoreUnsupportedCharset() { 450 return ignoreUnsupportedCharset; 451 } 452 453 public void setIgnoreUnsupportedCharset(boolean ignoreUnsupportedCharset) { 454 this.ignoreUnsupportedCharset = ignoreUnsupportedCharset; 455 } 456 }