Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 432CB118C0 for ; Wed, 9 Jul 2014 20:48:17 +0000 (UTC) Received: (qmail 98769 invoked by uid 500); 9 Jul 2014 20:48:16 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 98570 invoked by uid 500); 9 Jul 2014 20:48:16 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 98553 invoked by uid 99); 9 Jul 2014 20:48:16 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 09 Jul 2014 20:48:16 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 09 Jul 2014 20:48:11 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 707A42388CB5 for ; Wed, 9 Jul 2014 20:46:42 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r915608 [31/34] - in /websites/production/commons/content/proper/commons-email: ./ apidocs/ apidocs/org/apache/commons/mail/ apidocs/org/apache/commons/mail/class-use/ apidocs/org/apache/commons/mail/resolver/ apidocs/org/apache/commons/mai... Date: Wed, 09 Jul 2014 20:46:28 -0000 To: commits@commons.apache.org From: tn@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140709204642.707A42388CB5@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Modified: websites/production/commons/content/proper/commons-email/xref/org/apache/commons/mail/MultiPartEmail.html ============================================================================== --- websites/production/commons/content/proper/commons-email/xref/org/apache/commons/mail/MultiPartEmail.html (original) +++ websites/production/commons/content/proper/commons-email/xref/org/apache/commons/mail/MultiPartEmail.html Wed Jul 9 20:46:21 2014 @@ -27,568 +27,575 @@ 19 import java.io.File; 20 import java.io.IOException; 21 import java.io.InputStream; -22 import java.net.URL; -23 -24 import javax.activation.DataHandler; -25 import javax.activation.DataSource; -26 import javax.activation.FileDataSource; -27 import javax.activation.URLDataSource; -28 import javax.mail.BodyPart; -29 import javax.mail.MessagingException; -30 import javax.mail.internet.MimeBodyPart; -31 import javax.mail.internet.MimeMultipart; -32 import javax.mail.internet.MimePart; -33 -34 /** -35 * A multipart email. -36 * -37 * <p>This class is used to send multi-part internet email like -38 * messages with attachments. -39 * -40 * <p>To create a multi-part email, call the default constructor and -41 * then you can call setMsg() to set the message and call the -42 * different attach() methods. -43 * -44 * @since 1.0 -45 * @version $Id: MultiPartEmail.java 1510643 2013-08-05 18:23:25Z britter $ -46 */ -47 public class MultiPartEmail extends Email -48 { -49 /** Body portion of the email. */ -50 private MimeMultipart container; -51 -52 /** The message container. */ -53 private BodyPart primaryBodyPart; -54 -55 /** The MIME subtype. */ -56 private String subType; -57 -58 /** Indicates if the message has been initialized. */ -59 private boolean initialized; -60 -61 /** Indicates if attachments have been added to the message. */ -62 private boolean boolHasAttachments; -63 -64 /** -65 * Set the MIME subtype of the email. -66 * -67 * @param aSubType MIME subtype of the email -68 * @since 1.0 -69 */ -70 public void setSubType(String aSubType) -71 { -72 this.subType = aSubType; -73 } -74 -75 /** -76 * Get the MIME subtype of the email. -77 * -78 * @return MIME subtype of the email -79 * @since 1.0 -80 */ -81 public String getSubType() -82 { -83 return subType; -84 } -85 -86 /** -87 * Add a new part to the email. -88 * -89 * @param partContent The content. -90 * @param partContentType The content type. -91 * @return An Email. -92 * @throws EmailException see javax.mail.internet.MimeBodyPart -93 * for definitions -94 * @since 1.0 -95 */ -96 public Email addPart(String partContent, String partContentType) -97 throws EmailException -98 { -99 BodyPart bodyPart = createBodyPart(); -100 try -101 { -102 bodyPart.setContent(partContent, partContentType); -103 getContainer().addBodyPart(bodyPart); -104 } -105 catch (MessagingException me) -106 { -107 throw new EmailException(me); -108 } -109 -110 return this; -111 } -112 -113 /** -114 * Add a new part to the email. -115 * -116 * @param multipart The MimeMultipart. -117 * @return An Email. -118 * @throws EmailException see javax.mail.internet.MimeBodyPart -119 * for definitions -120 * @since 1.0 -121 */ -122 public Email addPart(MimeMultipart multipart) throws EmailException -123 { -124 try -125 { -126 return addPart(multipart, getContainer().getCount()); -127 } -128 catch (MessagingException me) -129 { -130 throw new EmailException(me); -131 } -132 } -133 -134 /** -135 * Add a new part to the email. -136 * -137 * @param multipart The part to add. -138 * @param index The index to add at. -139 * @return The email. -140 * @throws EmailException An error occurred while adding the part. -141 * @since 1.0 -142 */ -143 public Email addPart(MimeMultipart multipart, int index) throws EmailException -144 { -145 BodyPart bodyPart = createBodyPart(); -146 try -147 { -148 bodyPart.setContent(multipart); -149 getContainer().addBodyPart(bodyPart, index); -150 } -151 catch (MessagingException me) -152 { -153 throw new EmailException(me); -154 } -155 -156 return this; -157 } -158 -159 /** -160 * Initialize the multipart email. -161 * @since 1.0 -162 */ -163 protected void init() -164 { -165 if (initialized) -166 { -167 throw new IllegalStateException("Already initialized"); -168 } -169 -170 container = createMimeMultipart(); -171 super.setContent(container); -172 -173 initialized = true; -174 } -175 -176 /** -177 * Set the message of the email. -178 * -179 * @param msg A String. -180 * @return An Email. -181 * @throws EmailException see javax.mail.internet.MimeBodyPart -182 * for definitions -183 * @since 1.0 -184 */ -185 @Override -186 public Email setMsg(String msg) throws EmailException -187 { -188 // throw exception on null message -189 if (EmailUtils.isEmpty(msg)) -190 { -191 throw new EmailException("Invalid message supplied"); -192 } -193 try -194 { -195 BodyPart primary = getPrimaryBodyPart(); -196 -197 if ((primary instanceof MimePart) && EmailUtils.isNotEmpty(charset)) -198 { -199 ((MimePart) primary).setText(msg, charset); -200 } -201 else -202 { -203 primary.setText(msg); -204 } -205 } -206 catch (MessagingException me) -207 { -208 throw new EmailException(me); -209 } -210 return this; -211 } -212 -213 /** -214 * Does the work of actually building the MimeMessage. Please note that -215 * a user rarely calls this method directly and only if he/she is -216 * interested in the sending the underlying MimeMessage without -217 * commons-email. -218 * -219 * @exception EmailException if there was an error. -220 * @since 1.0 -221 */ -222 @Override -223 public void buildMimeMessage() throws EmailException -224 { -225 try -226 { -227 if (primaryBodyPart != null) -228 { -229 // before a multipart message can be sent, we must make sure that -230 // the content for the main body part was actually set. If not, -231 // an IOException will be thrown during super.send(). -232 -233 BodyPart body = this.getPrimaryBodyPart(); -234 try -235 { -236 body.getContent(); -237 } -238 catch (IOException e) // NOPMD -239 { -240 // do nothing here. -241 // content will be set to an empty string as a result. -242 // (Should this really be rethrown as an email exception?) -243 // throw new EmailException(e); -244 } -245 } -246 -247 if (subType != null) -248 { -249 getContainer().setSubType(subType); -250 } -251 -252 super.buildMimeMessage(); -253 } -254 catch (MessagingException me) -255 { -256 throw new EmailException(me); -257 } -258 } -259 -260 /** -261 * Attach a file. -262 * -263 * @param file A file attachment -264 * @return A MultiPartEmail. -265 * @throws EmailException see javax.mail.internet.MimeBodyPart -266 * for definitions -267 * @since 1.3 -268 */ -269 public MultiPartEmail attach(File file) -270 throws EmailException -271 { -272 String fileName = file.getAbsolutePath(); -273 -274 try -275 { -276 if (!file.exists()) -277 { -278 throw new IOException("\"" + fileName + "\" does not exist"); -279 } -280 -281 FileDataSource fds = new FileDataSource(file); +22 import java.io.UnsupportedEncodingException; +23 import java.net.URL; +24 +25 import javax.activation.DataHandler; +26 import javax.activation.DataSource; +27 import javax.activation.FileDataSource; +28 import javax.activation.URLDataSource; +29 import javax.mail.BodyPart; +30 import javax.mail.MessagingException; +31 import javax.mail.internet.MimeBodyPart; +32 import javax.mail.internet.MimeMultipart; +33 import javax.mail.internet.MimePart; +34 import javax.mail.internet.MimeUtility; +35 +36 /** +37 * A multipart email. +38 * +39 * <p>This class is used to send multi-part internet email like +40 * messages with attachments. +41 * +42 * <p>To create a multi-part email, call the default constructor and +43 * then you can call setMsg() to set the message and call the +44 * different attach() methods. +45 * +46 * @since 1.0 +47 * @version $Id: MultiPartEmail.java 1606710 2014-06-30 12:29:13Z ggregory $ +48 */ +49 public class MultiPartEmail extends Email +50 { +51 /** Body portion of the email. */ +52 private MimeMultipart container; +53 +54 /** The message container. */ +55 private BodyPart primaryBodyPart; +56 +57 /** The MIME subtype. */ +58 private String subType; +59 +60 /** Indicates if the message has been initialized. */ +61 private boolean initialized; +62 +63 /** Indicates if attachments have been added to the message. */ +64 private boolean boolHasAttachments; +65 +66 /** +67 * Set the MIME subtype of the email. +68 * +69 * @param aSubType MIME subtype of the email +70 * @since 1.0 +71 */ +72 public void setSubType(final String aSubType) +73 { +74 this.subType = aSubType; +75 } +76 +77 /** +78 * Get the MIME subtype of the email. +79 * +80 * @return MIME subtype of the email +81 * @since 1.0 +82 */ +83 public String getSubType() +84 { +85 return subType; +86 } +87 +88 /** +89 * Add a new part to the email. +90 * +91 * @param partContent The content. +92 * @param partContentType The content type. +93 * @return An Email. +94 * @throws EmailException see javax.mail.internet.MimeBodyPart +95 * for definitions +96 * @since 1.0 +97 */ +98 public Email addPart(final String partContent, final String partContentType) +99 throws EmailException +100 { +101 final BodyPart bodyPart = createBodyPart(); +102 try +103 { +104 bodyPart.setContent(partContent, partContentType); +105 getContainer().addBodyPart(bodyPart); +106 } +107 catch (final MessagingException me) +108 { +109 throw new EmailException(me); +110 } +111 +112 return this; +113 } +114 +115 /** +116 * Add a new part to the email. +117 * +118 * @param multipart The MimeMultipart. +119 * @return An Email. +120 * @throws EmailException see javax.mail.internet.MimeBodyPart +121 * for definitions +122 * @since 1.0 +123 */ +124 public Email addPart(final MimeMultipart multipart) throws EmailException +125 { +126 try +127 { +128 return addPart(multipart, getContainer().getCount()); +129 } +130 catch (final MessagingException me) +131 { +132 throw new EmailException(me); +133 } +134 } +135 +136 /** +137 * Add a new part to the email. +138 * +139 * @param multipart The part to add. +140 * @param index The index to add at. +141 * @return The email. +142 * @throws EmailException An error occurred while adding the part. +143 * @since 1.0 +144 */ +145 public Email addPart(final MimeMultipart multipart, final int index) throws EmailException +146 { +147 final BodyPart bodyPart = createBodyPart(); +148 try +149 { +150 bodyPart.setContent(multipart); +151 getContainer().addBodyPart(bodyPart, index); +152 } +153 catch (final MessagingException me) +154 { +155 throw new EmailException(me); +156 } +157 +158 return this; +159 } +160 +161 /** +162 * Initialize the multipart email. +163 * @since 1.0 +164 */ +165 protected void init() +166 { +167 if (initialized) +168 { +169 throw new IllegalStateException("Already initialized"); +170 } +171 +172 container = createMimeMultipart(); +173 super.setContent(container); +174 +175 initialized = true; +176 } +177 +178 /** +179 * Set the message of the email. +180 * +181 * @param msg A String. +182 * @return An Email. +183 * @throws EmailException see javax.mail.internet.MimeBodyPart +184 * for definitions +185 * @since 1.0 +186 */ +187 @Override +188 public Email setMsg(final String msg) throws EmailException +189 { +190 // throw exception on null message +191 if (EmailUtils.isEmpty(msg)) +192 { +193 throw new EmailException("Invalid message supplied"); +194 } +195 try +196 { +197 final BodyPart primary = getPrimaryBodyPart(); +198 +199 if (primary instanceof MimePart && EmailUtils.isNotEmpty(charset)) +200 { +201 ((MimePart) primary).setText(msg, charset); +202 } +203 else +204 { +205 primary.setText(msg); +206 } +207 } +208 catch (final MessagingException me) +209 { +210 throw new EmailException(me); +211 } +212 return this; +213 } +214 +215 /** +216 * Does the work of actually building the MimeMessage. Please note that +217 * a user rarely calls this method directly and only if he/she is +218 * interested in the sending the underlying MimeMessage without +219 * commons-email. +220 * +221 * @exception EmailException if there was an error. +222 * @since 1.0 +223 */ +224 @Override +225 public void buildMimeMessage() throws EmailException +226 { +227 try +228 { +229 if (primaryBodyPart != null) +230 { +231 // before a multipart message can be sent, we must make sure that +232 // the content for the main body part was actually set. If not, +233 // an IOException will be thrown during super.send(). +234 +235 final BodyPart body = this.getPrimaryBodyPart(); +236 try +237 { +238 body.getContent(); +239 } +240 catch (final IOException e) // NOPMD +241 { +242 // do nothing here. +243 // content will be set to an empty string as a result. +244 // (Should this really be rethrown as an email exception?) +245 // throw new EmailException(e); +246 } +247 } +248 +249 if (subType != null) +250 { +251 getContainer().setSubType(subType); +252 } +253 +254 super.buildMimeMessage(); +255 } +256 catch (final MessagingException me) +257 { +258 throw new EmailException(me); +259 } +260 } +261 +262 /** +263 * Attach a file. +264 * +265 * @param file A file attachment +266 * @return A MultiPartEmail. +267 * @throws EmailException see javax.mail.internet.MimeBodyPart +268 * for definitions +269 * @since 1.3 +270 */ +271 public MultiPartEmail attach(final File file) +272 throws EmailException +273 { +274 final String fileName = file.getAbsolutePath(); +275 +276 try +277 { +278 if (!file.exists()) +279 { +280 throw new IOException("\"" + fileName + "\" does not exist"); +281 } 282 -283 return attach(fds, file.getName(), null, EmailAttachment.ATTACHMENT); -284 } -285 catch (IOException e) -286 { -287 throw new EmailException("Cannot attach file \"" + fileName + "\"", e); -288 } -289 } -290 -291 /** -292 * Attach an EmailAttachment. -293 * -294 * @param attachment An EmailAttachment. -295 * @return A MultiPartEmail. -296 * @throws EmailException see javax.mail.internet.MimeBodyPart -297 * for definitions -298 * @since 1.0 -299 */ -300 public MultiPartEmail attach(EmailAttachment attachment) -301 throws EmailException -302 { -303 MultiPartEmail result = null; -304 -305 if (attachment == null) -306 { -307 throw new EmailException("Invalid attachment supplied"); -308 } -309 -310 URL url = attachment.getURL(); +283 final FileDataSource fds = new FileDataSource(file); +284 +285 return attach(fds, file.getName(), null, EmailAttachment.ATTACHMENT); +286 } +287 catch (final IOException e) +288 { +289 throw new EmailException("Cannot attach file \"" + fileName + "\"", e); +290 } +291 } +292 +293 /** +294 * Attach an EmailAttachment. +295 * +296 * @param attachment An EmailAttachment. +297 * @return A MultiPartEmail. +298 * @throws EmailException see javax.mail.internet.MimeBodyPart +299 * for definitions +300 * @since 1.0 +301 */ +302 public MultiPartEmail attach(final EmailAttachment attachment) +303 throws EmailException +304 { +305 MultiPartEmail result = null; +306 +307 if (attachment == null) +308 { +309 throw new EmailException("Invalid attachment supplied"); +310 } 311 -312 if (url == null) -313 { -314 String fileName = null; -315 try -316 { -317 fileName = attachment.getPath(); -318 File file = new File(fileName); -319 if (!file.exists()) -320 { -321 throw new IOException("\"" + fileName + "\" does not exist"); -322 } -323 result = -324 attach( -325 new FileDataSource(file), -326 attachment.getName(), -327 attachment.getDescription(), -328 attachment.getDisposition()); -329 } -330 catch (IOException e) -331 { -332 throw new EmailException("Cannot attach file \"" + fileName + "\"", e); -333 } -334 } -335 else -336 { -337 result = -338 attach( -339 url, -340 attachment.getName(), -341 attachment.getDescription(), -342 attachment.getDisposition()); -343 } -344 -345 return result; -346 } -347 -348 /** -349 * Attach a file located by its URL. The disposition of the file -350 * is set to mixed. -351 * -352 * @param url The URL of the file (may be any valid URL). -353 * @param name The name field for the attachment. -354 * @param description A description for the attachment. -355 * @return A MultiPartEmail. -356 * @throws EmailException see javax.mail.internet.MimeBodyPart -357 * for definitions -358 * @since 1.0 -359 */ -360 public MultiPartEmail attach(URL url, String name, String description) -361 throws EmailException -362 { -363 return attach(url, name, description, EmailAttachment.ATTACHMENT); -364 } -365 -366 /** -367 * Attach a file located by its URL. -368 * -369 * @param url The URL of the file (may be any valid URL). -370 * @param name The name field for the attachment. -371 * @param description A description for the attachment. -372 * @param disposition Either mixed or inline. -373 * @return A MultiPartEmail. -374 * @throws EmailException see javax.mail.internet.MimeBodyPart -375 * for definitions -376 * @since 1.0 -377 */ -378 public MultiPartEmail attach( -379 URL url, -380 String name, -381 String description, -382 String disposition) -383 throws EmailException -384 { -385 // verify that the URL is valid -386 try -387 { -388 InputStream is = url.openStream(); -389 is.close(); -390 } -391 catch (IOException e) -392 { -393 throw new EmailException("Invalid URL set:" + url, e); -394 } -395 -396 return attach(new URLDataSource(url), name, description, disposition); -397 } -398 -399 /** -400 * Attach a file specified as a DataSource interface. -401 * -402 * @param ds A DataSource interface for the file. -403 * @param name The name field for the attachment. -404 * @param description A description for the attachment. -405 * @return A MultiPartEmail. -406 * @throws EmailException see javax.mail.internet.MimeBodyPart -407 * for definitions -408 * @since 1.0 -409 */ -410 public MultiPartEmail attach( -411 DataSource ds, -412 String name, -413 String description) -414 throws EmailException -415 { -416 // verify that the DataSource is valid -417 try -418 { -419 final InputStream is = (ds != null) ? ds.getInputStream() : null; -420 if (is != null) -421 { -422 // close the input stream to prevent file locking on windows -423 is.close(); -424 } -425 -426 if (is == null) -427 { -428 throw new EmailException("Invalid Datasource"); -429 } -430 } -431 catch (IOException e) -432 { -433 throw new EmailException("Invalid Datasource", e); -434 } -435 -436 return attach(ds, name, description, EmailAttachment.ATTACHMENT); -437 } -438 -439 /** -440 * Attach a file specified as a DataSource interface. -441 * -442 * @param ds A DataSource interface for the file. -443 * @param name The name field for the attachment. -444 * @param description A description for the attachment. -445 * @param disposition Either mixed or inline. -446 * @return A MultiPartEmail. -447 * @throws EmailException see javax.mail.internet.MimeBodyPart -448 * for definitions -449 * @since 1.0 -450 */ -451 public MultiPartEmail attach( -452 DataSource ds, -453 String name, -454 String description, -455 String disposition) -456 throws EmailException -457 { -458 if (EmailUtils.isEmpty(name)) -459 { -460 name = ds.getName(); -461 } -462 BodyPart bodyPart = createBodyPart(); -463 try -464 { -465 getContainer().addBodyPart(bodyPart); -466 +312 final URL url = attachment.getURL(); +313 +314 if (url == null) +315 { +316 String fileName = null; +317 try +318 { +319 fileName = attachment.getPath(); +320 final File file = new File(fileName); +321 if (!file.exists()) +322 { [... 384 lines stripped ...]