Skip to content

Commit 178a519

Browse files
committed
Fix for #79 that adds support for custom ID value. Closes #77.
1 parent 058d64e commit 178a519

File tree

5 files changed

+85
-10
lines changed

5 files changed

+85
-10
lines changed

Diff for: src/main/java/org/simplejavamail/converter/internal/mimemessage/MimeMessageHelper.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.simplejavamail.email.AttachmentResource;
88
import org.simplejavamail.email.Email;
99
import org.simplejavamail.email.Recipient;
10+
import org.simplejavamail.internal.util.MiscUtil;
1011

1112
import javax.activation.DataHandler;
1213
import javax.activation.DataSource;
@@ -64,7 +65,16 @@ public static MimeMessage produceMimeMessage(final Email email, final Session se
6465
}
6566
// create new wrapper for each mail being sent (enable sending multiple emails with one mailer)
6667
final MimeEmailMessageWrapper messageRoot = new MimeEmailMessageWrapper();
67-
final MimeMessage message = new MimeMessage(session);
68+
final MimeMessage message = new MimeMessage(session) {
69+
@Override
70+
protected void updateMessageID() throws MessagingException {
71+
if (valueNullOrEmpty(email.getId())) {
72+
super.updateMessageID();
73+
} else {
74+
setHeader("Message-ID", email.getId());
75+
}
76+
}
77+
};
6878
// set basic email properties
6979
message.setSubject(email.getSubject(), CHARACTER_ENCODING);
7080
message.setFrom(new InternetAddress(email.getFromRecipient().getAddress(), email.getFromRecipient().getName(), CHARACTER_ENCODING));

Diff for: src/main/java/org/simplejavamail/email/Email.java

+26
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@
3939
*/
4040
@SuppressWarnings("SameParameterValue")
4141
public class Email {
42+
43+
/**
44+
* Optional ID, which will be used when sending using the underlying Java Mail framework. Will be generated otherwise.
45+
* <p>
46+
* Note that id can only ever be filled by end-users for sending an email. This library will never fill this field when converting a MimeMessage.
47+
* <p>
48+
* The id-format should be conform <a href="https://tools.ietf.org/html/rfc5322#section-3.6.4">rfc5322#section-3.6.4</a>
49+
*/
50+
private String id;
51+
4252
/**
4353
* The sender of the email. Can be used in conjunction with {@link #replyToRecipient}.
4454
*/
@@ -173,6 +183,13 @@ public void signWithDomainKey(@Nonnull final InputStream dkimPrivateKeyInputStre
173183
this.selector = checkNonEmptyArgument(selector, "selector");
174184
}
175185

186+
/**
187+
* Bean setter for {@link #id}.
188+
*/
189+
public void setId(@Nullable final String id) {
190+
this.id = id;
191+
}
192+
176193
/**
177194
* Sets the sender address.
178195
*
@@ -355,6 +372,13 @@ public void addAttachment(@Nullable final String name, @Nonnull final DataSource
355372
attachments.add(new AttachmentResource(MiscUtil.encodeText(name), filedata));
356373
}
357374

375+
/**
376+
* Bean getter for {@link #id}.
377+
*/
378+
public String getId() {
379+
return id;
380+
}
381+
358382
/**
359383
* Bean getter for {@link #fromRecipient}.
360384
*/
@@ -452,6 +476,7 @@ public boolean equals(final Object o) {
452476
@Override
453477
public String toString() {
454478
return "Email{" +
479+
"\n\tid=" + id +
455480
"\n\tfromRecipient=" + fromRecipient +
456481
",\n\treplyToRecipient=" + replyToRecipient +
457482
",\n\ttext='" + text + '\'' +
@@ -476,6 +501,7 @@ public String toString() {
476501
attachments = builder.getAttachments();
477502
headers = builder.getHeaders();
478503

504+
id = builder.getId();
479505
fromRecipient = builder.getFromRecipient();
480506
replyToRecipient = builder.getReplyToRecipient();
481507
text = builder.getText();

Diff for: src/main/java/org/simplejavamail/email/EmailBuilder.java

+22-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@
4040
*/
4141
@SuppressWarnings("UnusedReturnValue")
4242
public class EmailBuilder {
43+
44+
/**
45+
* Optional ID, which will be used when sending using the underlying Java Mail framework. Will be generated otherwise.
46+
* <p>
47+
* Note that id can only ever be filled by end-users for sending an email. This library will never fill this field when converting a MimeMessage.
48+
* <p>
49+
* The id-format should be conform <a href="https://tools.ietf.org/html/rfc5322#section-3.6.4">rfc5322#section-3.6.4</a>
50+
*/
51+
private String id;
52+
4353
private Recipient fromRecipient;
4454
/**
4555
* The reply-to-address, optional. Can be used in conjunction with {@link #fromRecipient}.
@@ -139,13 +149,18 @@ public EmailBuilder() {
139149
}
140150
}
141151

142-
/**
143-
*
144-
*/
145152
public Email build() {
146153
return new Email(this);
147154
}
148155

156+
/**
157+
* Sets the optional id to be used when sending using the underlying Java Mail framework. Will be generated otherwise.
158+
*/
159+
public EmailBuilder id(@Nullable final String id) {
160+
this.id = id;
161+
return this;
162+
}
163+
149164
/**
150165
* Sets the sender address {@link #fromRecipient}.
151166
*
@@ -523,6 +538,10 @@ public EmailBuilder signWithDomainKey(@Nonnull final InputStream dkimPrivateKeyI
523538
SETTERS / GETTERS
524539
*/
525540

541+
public String getId() {
542+
return id;
543+
}
544+
526545
public Recipient getFromRecipient() {
527546
return fromRecipient;
528547
}

Diff for: src/test/java/org/simplejavamail/mailer/MailerLiveTest.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.simplejavamail.mailer;
22

33
import org.junit.Before;
4+
import org.junit.Ignore;
45
import org.junit.Rule;
56
import org.junit.Test;
67
import org.simplejavamail.email.AttachmentResource;
@@ -14,12 +15,14 @@
1415
import testutil.testrules.TestSmtpServer;
1516

1617
import javax.mail.MessagingException;
18+
import javax.mail.internet.MimeMessage;
1719
import java.io.IOException;
1820
import java.util.Properties;
1921

2022
import static javax.mail.Message.RecipientType.TO;
2123
import static org.assertj.core.api.Assertions.assertThat;
2224
import static org.simplejavamail.converter.EmailConverter.mimeMessageToEmail;
25+
import static org.simplejavamail.internal.util.MiscUtil.valueNullOrEmpty;
2326
import static testutil.EmailHelper.normalizeText;
2427
import static testutil.EmailHelper.readOutlookMessage;
2528

@@ -45,6 +48,13 @@ public void createMailSession_StandardDummyMail()
4548
assertSendingEmail(EmailHelper.createDummyEmail());
4649
}
4750

51+
@Test
52+
@Ignore("Unfortunately, Wiser doesn't seem to get the ID back, but I confirmed with gmail that the (correct) ID should be there")
53+
public void createMailSession_StandardDummyMailWithId()
54+
throws IOException, MessagingException {
55+
assertSendingEmail(EmailHelper.createDummyEmail("<123@456>"));
56+
}
57+
4858
@Test
4959
public void createMailSession_OutlookMessageTest()
5060
throws IOException, MessagingException {
@@ -78,7 +88,11 @@ public void createMailSession_OutlookMessageTest()
7888
private Email assertSendingEmail(final Email originalEmail)
7989
throws MessagingException {
8090
mailer.sendMail(originalEmail);
81-
Email receivedEmail = mimeMessageToEmail(smtpServerRule.getOnlyMessage());
91+
MimeMessage receivedMimeMessage = smtpServerRule.getOnlyMessage();
92+
if (!valueNullOrEmpty(originalEmail.getId())) {
93+
assertThat(receivedMimeMessage.getMessageID()).isEqualTo(originalEmail.getId());
94+
}
95+
Email receivedEmail = mimeMessageToEmail(receivedMimeMessage);
8296
assertThat(receivedEmail).isEqualTo(originalEmail);
8397
return receivedEmail;
8498
}

Diff for: src/test/java/testutil/EmailHelper.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.simplejavamail.email.Email;
44

5+
import javax.annotation.Nullable;
56
import javax.mail.util.ByteArrayDataSource;
67
import java.io.IOException;
78
import java.io.InputStream;
@@ -12,10 +13,15 @@
1213
import static org.simplejavamail.converter.EmailConverter.outlookMsgToEmail;
1314

1415
public class EmailHelper {
15-
16-
public static Email createDummyEmail()
16+
17+
public static Email createDummyEmail() throws IOException {
18+
return createDummyEmail(null);
19+
}
20+
21+
public static Email createDummyEmail(@Nullable String id)
1722
throws IOException {
1823
final Email emailNormal = new Email();
24+
emailNormal.setId(id);
1925
emailNormal.setFromAddress("lollypop", "[email protected]");
2026
// normally not needed, but for the test it is because the MimeMessage will
2127
// have it added automatically as well, so the parsed Email will also have it then
@@ -25,7 +31,7 @@ public static Email createDummyEmail()
2531
emailNormal.setText("We should meet up!");
2632
emailNormal.setTextHTML("<b>We should meet up!</b><img src='cid:thumbsup'>");
2733
emailNormal.setSubject("hey");
28-
34+
2935
// add two text files in different ways and a black thumbs up embedded image ->
3036
ByteArrayDataSource namedAttachment = new ByteArrayDataSource("Black Tie Optional", "text/plain");
3137
namedAttachment.setName("dresscode.txt"); // normally not needed, but otherwise the equals will fail
@@ -35,12 +41,12 @@ public static Email createDummyEmail()
3541
emailNormal.addEmbeddedImage("thumbsup", parseBase64Binary(base64String), "image/png");
3642
return emailNormal;
3743
}
38-
44+
3945
public static Email readOutlookMessage(final String filePath) {
4046
InputStream resourceAsStream = EmailHelper.class.getClassLoader().getResourceAsStream(filePath);
4147
return outlookMsgToEmail(resourceAsStream);
4248
}
43-
49+
4450
public static String normalizeText(String text) {
4551
return text.replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
4652
}

0 commit comments

Comments
 (0)