Skip to content

Commit d63c2c1

Browse files
committed
#89 fixed API for sending to delimited addresses under the same name
1 parent 12aac84 commit d63c2c1

File tree

2 files changed

+58
-58
lines changed

2 files changed

+58
-58
lines changed

src/main/java/org/simplejavamail/email/EmailBuilder.java

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -231,20 +231,6 @@ public EmailBuilder textHTML(@Nullable final String textHTML) {
231231
return this;
232232
}
233233

234-
/**
235-
* Adds a new {@link Recipient} to the list on account of name, address with recipient type {@link Message.RecipientType#TO}.
236-
*
237-
* @param name The name of the recipient.
238-
* @param address The emailaddress of the recipient.
239-
* @see #recipients
240-
* @see Recipient
241-
*/
242-
public EmailBuilder to(@Nullable final String name, @Nonnull final String address) {
243-
checkNonEmptyArgument(address, "address");
244-
recipients.add(new Recipient(name, address, Message.RecipientType.TO));
245-
return this;
246-
}
247-
248234
/**
249235
* Adds new {@link Recipient} instances to the list on account of name, address with recipient type {@link Message.RecipientType#TO}.
250236
*
@@ -260,23 +246,31 @@ public EmailBuilder to(@Nonnull final Recipient... recipientsToAdd) {
260246
}
261247

262248
/**
263-
* Adds anew {@link Recipient} instances to the list on account of empty name, address with recipient type {@link Message.RecipientType#TO}. List can be
264-
* comma ',' or semicolon ';' separated.
249+
* Delegates to {@link #to(String, String)} while omitting the name used for the recipient(s).
250+
*/
251+
public EmailBuilder to(@Nonnull final String emailAddressList) {
252+
return to(null, emailAddressList);
253+
}
254+
255+
/**
256+
* Adds anew {@link Recipient} instances to the list on account of given name, address with recipient type {@link Message.RecipientType#TO}.
257+
* List can be comma ',' or semicolon ';' separated.
265258
*
266-
* @param emailAddressList The recipients whose address to use for both name and address
259+
* @param name The name of the recipient(s).
260+
* @param emailAddressList The emailaddresses of the recipients (will be singular in most use cases).
267261
* @see #recipients
268262
* @see Recipient
269263
*/
270-
public EmailBuilder to(@Nonnull final String emailAddressList) {
264+
public EmailBuilder to(@Nullable final String name, @Nonnull final String emailAddressList) {
271265
checkNonEmptyArgument(emailAddressList, "emailAddressList");
272-
return addCommaOrSemicolonSeparatedEmailAddresses(emailAddressList, Message.RecipientType.TO);
266+
return addCommaOrSemicolonSeparatedEmailAddresses(name, emailAddressList, Message.RecipientType.TO);
273267
}
274268

275269
@Nonnull
276-
private EmailBuilder addCommaOrSemicolonSeparatedEmailAddresses(@Nonnull final String emailAddressList, @Nonnull final Message.RecipientType type) {
270+
private EmailBuilder addCommaOrSemicolonSeparatedEmailAddresses(@Nullable final String name, @Nonnull final String emailAddressList, @Nonnull final Message.RecipientType type) {
277271
checkNonEmptyArgument(type, "type");
278272
for (final String emailAddress : extractEmailAddresses(checkNonEmptyArgument(emailAddressList, "emailAddressList"))) {
279-
recipients.add(new Recipient(null, emailAddress, type));
273+
recipients.add(new Recipient(name, emailAddress, type));
280274
}
281275
return this;
282276
}
@@ -295,21 +289,6 @@ public EmailBuilder to(@Nonnull final String... emailAddresses) {
295289
return this;
296290
}
297291

298-
/**
299-
* Adds a new {@link Recipient} to the list on account of name, address with recipient type {@link Message.RecipientType#CC}.
300-
*
301-
* @param name The name of the recipient.
302-
* @param address The emailaddress of the recipient.
303-
* @see #recipients
304-
* @see Recipient
305-
*/
306-
@SuppressWarnings({ "WeakerAccess", "QuestionableName" })
307-
public EmailBuilder cc(@Nullable final String name, @Nonnull final String address) {
308-
checkNonEmptyArgument(address, "address");
309-
recipients.add(new Recipient(name, address, Message.RecipientType.CC));
310-
return this;
311-
}
312-
313292
/**
314293
* Adds new {@link Recipient} instances to the list on account of empty name, address with recipient type {@link Message.RecipientType#CC}.
315294
*
@@ -324,19 +303,29 @@ public EmailBuilder cc(@Nonnull final String... emailAddresses) {
324303
}
325304
return this;
326305
}
306+
307+
308+
/**
309+
* Delegates to {@link #cc(String, String)} while omitting the name for the CC recipient(s).
310+
*/
311+
@SuppressWarnings("QuestionableName")
312+
public EmailBuilder cc(@Nonnull final String emailAddressList) {
313+
return cc(null, emailAddressList);
314+
}
327315

328316
/**
329317
* Adds anew {@link Recipient} instances to the list on account of empty name, address with recipient type {@link Message.RecipientType#CC}. List can be
330318
* comma ',' or semicolon ';' separated.
331319
*
320+
* @param name The name of the recipient(s).
332321
* @param emailAddressList The recipients whose address to use for both name and address
333322
* @see #recipients
334323
* @see Recipient
335324
*/
336325
@SuppressWarnings("QuestionableName")
337-
public EmailBuilder cc(@Nonnull final String emailAddressList) {
326+
public EmailBuilder cc(@Nullable String name, @Nonnull final String emailAddressList) {
338327
checkNonEmptyArgument(emailAddressList, "emailAddressList");
339-
return addCommaOrSemicolonSeparatedEmailAddresses(emailAddressList, Message.RecipientType.CC);
328+
return addCommaOrSemicolonSeparatedEmailAddresses(name, emailAddressList, Message.RecipientType.CC);
340329
}
341330

342331
/**
@@ -354,21 +343,6 @@ public EmailBuilder cc(@Nonnull final Recipient... recipientsToAdd) {
354343
return this;
355344
}
356345

357-
/**
358-
* Adds a new {@link Recipient} to the list on account of name, address with recipient type {@link Message.RecipientType#BCC}.
359-
*
360-
* @param name The name of the recipient.
361-
* @param address The emailaddress of the recipient.
362-
* @see #recipients
363-
* @see Recipient
364-
*/
365-
@SuppressWarnings("WeakerAccess")
366-
public EmailBuilder bcc(@Nullable final String name, @Nonnull final String address) {
367-
checkNonEmptyArgument(address, "address");
368-
recipients.add(new Recipient(name, address, Message.RecipientType.BCC));
369-
return this;
370-
}
371-
372346
/**
373347
* Adds new {@link Recipient} instances to the list on account of empty name, address with recipient type {@link Message.RecipientType#BCC}.
374348
*
@@ -382,18 +356,26 @@ public EmailBuilder bcc(@Nonnull final String... emailAddresses) {
382356
}
383357
return this;
384358
}
359+
360+
/**
361+
* Delegates to {@link #bcc(String, String)} while omitting the name for the BCC recipient(s).
362+
*/
363+
public EmailBuilder bcc(@Nonnull final String emailAddressList) {
364+
return bcc(null, emailAddressList);
365+
}
385366

386367
/**
387368
* Adds anew {@link Recipient} instances to the list on account of empty name, address with recipient type {@link Message.RecipientType#BCC}. List can be
388369
* comma ',' or semicolon ';' separated.
389370
*
371+
* @param name The name of the recipient(s).
390372
* @param emailAddressList The recipients whose address to use for both name and address
391373
* @see #recipients
392374
* @see Recipient
393375
*/
394-
public EmailBuilder bcc(@Nonnull final String emailAddressList) {
376+
public EmailBuilder bcc(@Nullable String name, @Nonnull final String emailAddressList) {
395377
checkNonEmptyArgument(emailAddressList, "emailAddressList");
396-
return addCommaOrSemicolonSeparatedEmailAddresses(emailAddressList, Message.RecipientType.BCC);
378+
return addCommaOrSemicolonSeparatedEmailAddresses(name, emailAddressList, Message.RecipientType.BCC);
397379
}
398380

399381
/**

src/test/java/org/simplejavamail/email/EmailBuilderTest.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public void testBuilderToAddresses() {
8080
8181
8282
.to(new Recipient("13", "[email protected]", null), new Recipient("14", "[email protected]", null))
83+
84+
8385
.build();
8486

8587
assertThat(email.getRecipients()).containsExactlyInAnyOrder(
@@ -96,7 +98,11 @@ public void testBuilderToAddresses() {
9698
createRecipient(null, "[email protected]", Message.RecipientType.TO),
9799
createRecipient(null, "[email protected]", Message.RecipientType.TO),
98100
createRecipient("13", "[email protected]", Message.RecipientType.TO),
99-
createRecipient("14", "[email protected]", Message.RecipientType.TO)
101+
createRecipient("14", "[email protected]", Message.RecipientType.TO),
102+
createRecipient("15", "[email protected]", Message.RecipientType.TO),
103+
createRecipient("15", "[email protected]", Message.RecipientType.TO),
104+
createRecipient("16", "[email protected]", Message.RecipientType.TO),
105+
createRecipient("16", "[email protected]", Message.RecipientType.TO)
100106
);
101107
}
102108

@@ -112,6 +118,8 @@ public void testBuilderCCAddresses() {
112118
113119
114120
.cc(new Recipient("13", "[email protected]", null), new Recipient("14", "[email protected]", null))
121+
122+
115123
.build();
116124

117125
assertThat(email.getRecipients()).containsExactlyInAnyOrder(
@@ -128,7 +136,11 @@ public void testBuilderCCAddresses() {
128136
createRecipient(null, "[email protected]", Message.RecipientType.CC),
129137
createRecipient(null, "[email protected]", Message.RecipientType.CC),
130138
createRecipient("13", "[email protected]", Message.RecipientType.CC),
131-
createRecipient("14", "[email protected]", Message.RecipientType.CC)
139+
createRecipient("14", "[email protected]", Message.RecipientType.CC),
140+
createRecipient("15", "[email protected]", Message.RecipientType.CC),
141+
createRecipient("15", "[email protected]", Message.RecipientType.CC),
142+
createRecipient("16", "[email protected]", Message.RecipientType.CC),
143+
createRecipient("16", "[email protected]", Message.RecipientType.CC)
132144
);
133145
}
134146

@@ -144,6 +156,8 @@ public void testBuilderBCCAddresses() {
144156
145157
146158
.bcc(new Recipient("13", "[email protected]", null), new Recipient("14", "[email protected]", null))
159+
160+
147161
.build();
148162

149163
assertThat(email.getRecipients()).containsExactlyInAnyOrder(
@@ -160,7 +174,11 @@ public void testBuilderBCCAddresses() {
160174
createRecipient(null, "[email protected]", Message.RecipientType.BCC),
161175
createRecipient(null, "[email protected]", Message.RecipientType.BCC),
162176
createRecipient("13", "[email protected]", Message.RecipientType.BCC),
163-
createRecipient("14", "[email protected]", Message.RecipientType.BCC)
177+
createRecipient("14", "[email protected]", Message.RecipientType.BCC),
178+
createRecipient("15", "[email protected]", Message.RecipientType.BCC),
179+
createRecipient("15", "[email protected]", Message.RecipientType.BCC),
180+
createRecipient("16", "[email protected]", Message.RecipientType.BCC),
181+
createRecipient("16", "[email protected]", Message.RecipientType.BCC)
164182
);
165183
}
166184

0 commit comments

Comments
 (0)