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." + protocol + ".rsetbeforequit", "true"); 170 properties.put("mail.transport.protocol", protocol); 171 properties.put("mail.store.protocol", protocol); 172 properties.put("mail.host", host); 173 174 if (debugMode) { 175 // add more debug for the SSL communication as well 176 properties.put("javax.net.debug", "all"); 177 } 178 179 if (dummyTrustManager && isSecureProtocol()) { 180 // set the custom SSL properties 181 properties.put("mail." + protocol + ".socketFactory.class", "org.apache.camel.component.mail.security.DummySSLSocketFactory"); 182 properties.put("mail." + protocol + ".socketFactory.fallback", "false"); 183 properties.put("mail." + protocol + ".socketFactory.port", "" + port); 184 } 185 186 return properties; 187 } 188 189 /** 190 * Is the used protocol to be secure or not 191 */ 192 public boolean isSecureProtocol() { 193 return this.protocol.equalsIgnoreCase("smtps") || this.protocol.equalsIgnoreCase("pop3s") 194 || this.protocol.equalsIgnoreCase("imaps"); 195 } 196 197 /** 198 * Returns an authenticator object for use in sessions 199 */ 200 public Authenticator getAuthenticator() { 201 return new Authenticator() { 202 protected PasswordAuthentication getPasswordAuthentication() { 203 return new PasswordAuthentication(getUsername(), getPassword()); 204 } 205 }; 206 } 207 208 public String getMailStoreLogInformation() { 209 String ssl = ""; 210 if (isSecureProtocol()) { 211 ssl = " (SSL enabled" + (dummyTrustManager ? " using DummyTrustManager)" : ")"); 212 } 213 214 return protocol + "://" + host + ":" + port + ssl + ", folder=" + folderName; 215 } 216 217 // Properties 218 // ------------------------------------------------------------------------- 219 220 public JavaMailSender getJavaMailSender() { 221 return javaMailSender; 222 } 223 224 public void setJavaMailSender(JavaMailSender javaMailSender) { 225 this.javaMailSender = javaMailSender; 226 } 227 228 public String getDefaultEncoding() { 229 return defaultEncoding; 230 } 231 232 public void setDefaultEncoding(String defaultEncoding) { 233 this.defaultEncoding = defaultEncoding; 234 } 235 236 public String getHost() { 237 return host; 238 } 239 240 public void setHost(String host) { 241 this.host = host; 242 } 243 244 public Properties getJavaMailProperties() { 245 return javaMailProperties; 246 } 247 248 /** 249 * Sets the java mail options. Will clear any default properties and only use the properties 250 * provided for this method. 251 */ 252 public void setJavaMailProperties(Properties javaMailProperties) { 253 this.javaMailProperties = javaMailProperties; 254 } 255 256 public Properties getAdditionalJavaMailProperties() { 257 if (additionalJavaMailProperties == null) { 258 additionalJavaMailProperties = new Properties(); 259 } 260 return additionalJavaMailProperties; 261 } 262 263 /** 264 * Sets additional java mail properties, that will append/override any default properties 265 * that is set based on all the other options. This is useful if you need to add some 266 * special options but want to keep the others as is. 267 */ 268 public void setAdditionalJavaMailProperties(Properties additionalJavaMailProperties) { 269 this.additionalJavaMailProperties = additionalJavaMailProperties; 270 } 271 272 public String getPassword() { 273 return password; 274 } 275 276 public void setPassword(String password) { 277 this.password = password; 278 } 279 280 public String getSubject() { 281 return subject; 282 } 283 284 public void setSubject(String subject) { 285 this.subject = subject; 286 } 287 288 public int getPort() { 289 return port; 290 } 291 292 public void setPort(int port) { 293 this.port = port; 294 } 295 296 public String getProtocol() { 297 return protocol; 298 } 299 300 public void setProtocol(String protocol) { 301 this.protocol = protocol; 302 } 303 304 public Session getSession() { 305 return session; 306 } 307 308 public void setSession(Session session) { 309 this.session = session; 310 } 311 312 public String getUsername() { 313 return username; 314 } 315 316 public void setUsername(String username) { 317 this.username = username; 318 if (getRecipients().size() == 0) { 319 // set default destination to username@host for backwards compatibility 320 // can be overridden by URI parameters 321 String address = username; 322 if (address.indexOf("@") == -1) { 323 address += "@" + host; 324 } 325 setTo(address); 326 } 327 } 328 329 public String getFrom() { 330 return from; 331 } 332 333 public void setFrom(String from) { 334 this.from = from; 335 } 336 337 public boolean isDelete() { 338 return delete; 339 } 340 341 public void setDelete(boolean delete) { 342 this.delete = delete; 343 } 344 345 public String getFolderName() { 346 return folderName; 347 } 348 349 public void setFolderName(String folderName) { 350 this.folderName = folderName; 351 } 352 353 public boolean isIgnoreUriScheme() { 354 return ignoreUriScheme; 355 } 356 357 public void setIgnoreUriScheme(boolean ignoreUriScheme) { 358 this.ignoreUriScheme = ignoreUriScheme; 359 } 360 361 public boolean isUnseen() { 362 return unseen; 363 } 364 365 public void setUnseen(boolean unseen) { 366 this.unseen = unseen; 367 } 368 369 /** 370 * Sets the <tt>To</tt> email address. Separate multiple email addresses with comma. 371 */ 372 public void setTo(String address) { 373 recipients.put(Message.RecipientType.TO, address); 374 } 375 376 /** 377 * Sets the <tt>CC</tt> email address. Separate multiple email addresses with comma. 378 */ 379 public void setCC(String address) { 380 recipients.put(Message.RecipientType.CC, address); 381 } 382 383 /** 384 * Sets the <tt>BCC</tt> email address. Separate multiple email addresses with comma. 385 */ 386 public void setBCC(String address) { 387 recipients.put(Message.RecipientType.BCC, address); 388 } 389 390 public Map<Message.RecipientType, String> getRecipients() { 391 return recipients; 392 } 393 394 public int getFetchSize() { 395 return fetchSize; 396 } 397 398 public void setFetchSize(int fetchSize) { 399 this.fetchSize = fetchSize; 400 } 401 402 public boolean isDebugMode() { 403 return debugMode; 404 } 405 406 public void setDebugMode(boolean debugMode) { 407 this.debugMode = debugMode; 408 } 409 410 public long getConnectionTimeout() { 411 return connectionTimeout; 412 } 413 414 public void setConnectionTimeout(long connectionTimeout) { 415 this.connectionTimeout = connectionTimeout; 416 } 417 418 public boolean isDummyTrustManager() { 419 return dummyTrustManager; 420 } 421 422 public void setDummyTrustManager(boolean dummyTrustManager) { 423 this.dummyTrustManager = dummyTrustManager; 424 } 425 426 public String getContentType() { 427 return contentType; 428 } 429 430 public void setContentType(String contentType) { 431 this.contentType = contentType; 432 } 433 434 public String getAlternativeBodyHeader() { 435 return alternativeBodyHeader; 436 } 437 438 public void setAlternativeBodyHeader(String alternativeBodyHeader) { 439 this.alternativeBodyHeader = alternativeBodyHeader; 440 } 441 442 public boolean isUseInlineAttachments() { 443 return useInlineAttachments; 444 } 445 446 public void setUseInlineAttachments(boolean useInlineAttachments) { 447 this.useInlineAttachments = useInlineAttachments; 448 } 449 450 public boolean isIgnoreUnsupportedCharset() { 451 return ignoreUnsupportedCharset; 452 } 453 454 public void setIgnoreUnsupportedCharset(boolean ignoreUnsupportedCharset) { 455 this.ignoreUnsupportedCharset = ignoreUnsupportedCharset; 456 } 457 }