Skip to content

Commit c7d26ff

Browse files
committed
add some more variations and renamed addRecipient to addRecipients
1 parent 15324b7 commit c7d26ff

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

src/main/java/org/simplejavamail/converter/EmailConverter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ public static Email outlookMsgToEmail(@Nonnull final InputStream msgInputStream)
9090
}
9191

9292
/**
93-
* Delegates to {@link #emlToMimeMessage(Session, String)} using a dummy {@link Session} instance and passes the result to {@link
93+
* Delegates to {@link #emlToMimeMessage(String, Session)} using a dummy {@link Session} instance and passes the result to {@link
9494
* #mimeMessageToEmail(MimeMessage)};
9595
*/
9696
public static Email emlToEmail(@Nonnull final String eml) {
97-
final MimeMessage mimeMessage = emlToMimeMessage(createDummySession(), checkNonEmptyArgument(eml, "eml"));
97+
final MimeMessage mimeMessage = emlToMimeMessage(checkNonEmptyArgument(eml, "eml"), createDummySession());
9898
return mimeMessageToEmail(mimeMessage);
9999
}
100100

@@ -151,18 +151,18 @@ public static MimeMessage emailToMimeMessage(@Nonnull final Email email, @Nonnul
151151
}
152152

153153
/**
154-
* Delegates to {@link #emlToMimeMessage(Session, String)} with an empty {@link Session} instance.
154+
* Delegates to {@link #emlToMimeMessage(String, Session)} with an empty {@link Session} instance.
155155
*
156156
* @see #emailToMimeMessage(Email, Session)
157157
*/
158158
public static MimeMessage emlToMimeMessage(@Nonnull final String eml) {
159-
return emlToMimeMessage(createDummySession(), checkNonEmptyArgument(eml, "eml"));
159+
return emlToMimeMessage(checkNonEmptyArgument(eml, "eml"), createDummySession());
160160
}
161161

162162
/**
163163
* Relies on JavaMail's native parser of EML data, {@link MimeMessage#MimeMessage(Session, InputStream)}.
164164
*/
165-
public static MimeMessage emlToMimeMessage(@Nonnull final Session session, @Nonnull final String eml) {
165+
public static MimeMessage emlToMimeMessage(@Nonnull final String eml, @Nonnull final Session session) {
166166
checkNonEmptyArgument(session, "session");
167167
checkNonEmptyArgument(eml, "eml");
168168
try {

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

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import javax.activation.DataSource;
66
import javax.annotation.Nonnull;
77
import javax.annotation.Nullable;
8-
import javax.mail.Message;
98
import javax.mail.Message.RecipientType;
109
import javax.mail.util.ByteArrayDataSource;
1110
import java.io.File;
@@ -117,21 +116,21 @@ public Email(final boolean readFromDefaults) {
117116
if (hasProperty(DEFAULT_TO_NAME)) {
118117
addRecipient((String) getProperty(DEFAULT_TO_NAME), (String) getProperty(DEFAULT_TO_ADDRESS), RecipientType.TO);
119118
} else {
120-
addRecipient((String) getProperty(DEFAULT_TO_ADDRESS), RecipientType.TO);
119+
addRecipients((String) getProperty(DEFAULT_TO_ADDRESS), RecipientType.TO);
121120
}
122121
}
123122
if (hasProperty(DEFAULT_CC_ADDRESS)) {
124123
if (hasProperty(DEFAULT_CC_NAME)) {
125124
addRecipient((String) getProperty(DEFAULT_CC_NAME), (String) getProperty(DEFAULT_CC_ADDRESS), RecipientType.CC);
126125
} else {
127-
addRecipient((String) getProperty(DEFAULT_CC_ADDRESS), RecipientType.CC);
126+
addRecipients((String) getProperty(DEFAULT_CC_ADDRESS), RecipientType.CC);
128127
}
129128
}
130129
if (hasProperty(DEFAULT_BCC_ADDRESS)) {
131130
if (hasProperty(DEFAULT_BCC_NAME)) {
132131
addRecipient((String) getProperty(DEFAULT_BCC_NAME), (String) getProperty(DEFAULT_BCC_ADDRESS), RecipientType.BCC);
133132
} else {
134-
addRecipient((String) getProperty(DEFAULT_BCC_ADDRESS), RecipientType.BCC);
133+
addRecipients((String) getProperty(DEFAULT_BCC_ADDRESS), RecipientType.BCC);
135134
}
136135
}
137136
if (hasProperty(DEFAULT_SUBJECT)) {
@@ -240,21 +239,43 @@ public void addRecipient(@Nullable final String name, @Nonnull final String addr
240239
* @see Recipient
241240
* @see RecipientType
242241
*/
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));
248246
}
249247

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) {
252257
checkNonEmptyArgument(type, "type");
253-
for (String emailAddress : extractEmailAddresses(checkNonEmptyArgument(emailAddressList, "emailAddressList"))) {
258+
for (String emailAddress : checkNonEmptyArgument(recipientEmailAddressesToAdd, "recipientEmailAddressesToAdd")) {
254259
recipients.add(new Recipient(null, emailAddress, type));
255260
}
256261
}
257262

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+
258279
/**
259280
* Adds an embedded image (attachment type) to the email message and generates the necessary {@link DataSource} with the given byte data. Then
260281
* delegates to {@link #addEmbeddedImage(String, DataSource)}. At this point the datasource is actually a {@link ByteArrayDataSource}.

0 commit comments

Comments
 (0)