|
5 | 5 | import javax.activation.DataSource; |
6 | 6 | import javax.annotation.Nonnull; |
7 | 7 | import javax.annotation.Nullable; |
8 | | -import javax.mail.Message; |
9 | 8 | import javax.mail.Message.RecipientType; |
10 | 9 | import javax.mail.util.ByteArrayDataSource; |
11 | 10 | import java.io.File; |
@@ -117,21 +116,21 @@ public Email(final boolean readFromDefaults) { |
117 | 116 | if (hasProperty(DEFAULT_TO_NAME)) { |
118 | 117 | addRecipient((String) getProperty(DEFAULT_TO_NAME), (String) getProperty(DEFAULT_TO_ADDRESS), RecipientType.TO); |
119 | 118 | } else { |
120 | | - addRecipient((String) getProperty(DEFAULT_TO_ADDRESS), RecipientType.TO); |
| 119 | + addRecipients((String) getProperty(DEFAULT_TO_ADDRESS), RecipientType.TO); |
121 | 120 | } |
122 | 121 | } |
123 | 122 | if (hasProperty(DEFAULT_CC_ADDRESS)) { |
124 | 123 | if (hasProperty(DEFAULT_CC_NAME)) { |
125 | 124 | addRecipient((String) getProperty(DEFAULT_CC_NAME), (String) getProperty(DEFAULT_CC_ADDRESS), RecipientType.CC); |
126 | 125 | } else { |
127 | | - addRecipient((String) getProperty(DEFAULT_CC_ADDRESS), RecipientType.CC); |
| 126 | + addRecipients((String) getProperty(DEFAULT_CC_ADDRESS), RecipientType.CC); |
128 | 127 | } |
129 | 128 | } |
130 | 129 | if (hasProperty(DEFAULT_BCC_ADDRESS)) { |
131 | 130 | if (hasProperty(DEFAULT_BCC_NAME)) { |
132 | 131 | addRecipient((String) getProperty(DEFAULT_BCC_NAME), (String) getProperty(DEFAULT_BCC_ADDRESS), RecipientType.BCC); |
133 | 132 | } else { |
134 | | - addRecipient((String) getProperty(DEFAULT_BCC_ADDRESS), RecipientType.BCC); |
| 133 | + addRecipients((String) getProperty(DEFAULT_BCC_ADDRESS), RecipientType.BCC); |
135 | 134 | } |
136 | 135 | } |
137 | 136 | if (hasProperty(DEFAULT_SUBJECT)) { |
@@ -240,21 +239,43 @@ public void addRecipient(@Nullable final String name, @Nonnull final String addr |
240 | 239 | * @see Recipient |
241 | 240 | * @see RecipientType |
242 | 241 | */ |
243 | | - public void addRecipient(@Nonnull final String emailAddressList, @Nonnull final RecipientType type) { |
244 | | - addCommaOrSemicolonSeparatedEmailAddresses( |
245 | | - checkNonEmptyArgument(emailAddressList, "emailAddressList"), |
246 | | - checkNonEmptyArgument(type, "type") |
247 | | - ); |
| 242 | + public void addRecipients(@Nonnull final String emailAddressList, @Nonnull final RecipientType type) { |
| 243 | + checkNonEmptyArgument(type, "type"); |
| 244 | + checkNonEmptyArgument(emailAddressList, "emailAddressList"); |
| 245 | + addRecipients(type, extractEmailAddresses(emailAddressList)); |
248 | 246 | } |
249 | 247 |
|
250 | | - @Nonnull |
251 | | - private void addCommaOrSemicolonSeparatedEmailAddresses(@Nonnull final String emailAddressList, @Nonnull final Message.RecipientType type) { |
| 248 | + /** |
| 249 | + * Adds all given recipients addresses to the list on account of address and recipient type (eg. {@link RecipientType#CC}). |
| 250 | + * |
| 251 | + * @param recipientEmailAddressesToAdd List of preconfigured recipients. |
| 252 | + * @see #recipients |
| 253 | + * @see Recipient |
| 254 | + * @see RecipientType |
| 255 | + */ |
| 256 | + public void addRecipients(@Nonnull final RecipientType type, @Nonnull final String... recipientEmailAddressesToAdd) { |
252 | 257 | checkNonEmptyArgument(type, "type"); |
253 | | - for (String emailAddress : extractEmailAddresses(checkNonEmptyArgument(emailAddressList, "emailAddressList"))) { |
| 258 | + for (String emailAddress : checkNonEmptyArgument(recipientEmailAddressesToAdd, "recipientEmailAddressesToAdd")) { |
254 | 259 | recipients.add(new Recipient(null, emailAddress, type)); |
255 | 260 | } |
256 | 261 | } |
257 | 262 |
|
| 263 | + /** |
| 264 | + * Adds all given {@link Recipient} instances to the list (as copies) on account of name, address and recipient type (eg. {@link RecipientType#CC}). |
| 265 | + * |
| 266 | + * @param recipientsToAdd List of preconfigured recipients. |
| 267 | + * @see #recipients |
| 268 | + * @see Recipient |
| 269 | + * @see RecipientType |
| 270 | + */ |
| 271 | + public void addRecipients(@Nonnull final Recipient... recipientsToAdd) { |
| 272 | + for (Recipient recipient : checkNonEmptyArgument(recipientsToAdd, "recipientsToAdd")) { |
| 273 | + final String address = checkNonEmptyArgument(recipient.getAddress(), "recipient.address"); |
| 274 | + final RecipientType type = checkNonEmptyArgument(recipient.getType(), "recipient.type"); |
| 275 | + recipients.add(new Recipient(recipient.getName(), address, type)); |
| 276 | + } |
| 277 | + } |
| 278 | + |
258 | 279 | /** |
259 | 280 | * Adds an embedded image (attachment type) to the email message and generates the necessary {@link DataSource} with the given byte data. Then |
260 | 281 | * delegates to {@link #addEmbeddedImage(String, DataSource)}. At this point the datasource is actually a {@link ByteArrayDataSource}. |
|
0 commit comments