Skip to content

Commit

Permalink
Fix for #79 that adds support for custom ID value. Closes #77.
Browse files Browse the repository at this point in the history
  • Loading branch information
bbottema committed May 21, 2017
1 parent 058d64e commit 178a519
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.simplejavamail.email.AttachmentResource;
import org.simplejavamail.email.Email;
import org.simplejavamail.email.Recipient;
import org.simplejavamail.internal.util.MiscUtil;

import javax.activation.DataHandler;
import javax.activation.DataSource;
Expand Down Expand Up @@ -64,7 +65,16 @@ public static MimeMessage produceMimeMessage(final Email email, final Session se
}
// create new wrapper for each mail being sent (enable sending multiple emails with one mailer)
final MimeEmailMessageWrapper messageRoot = new MimeEmailMessageWrapper();
final MimeMessage message = new MimeMessage(session);
final MimeMessage message = new MimeMessage(session) {
@Override
protected void updateMessageID() throws MessagingException {
if (valueNullOrEmpty(email.getId())) {
super.updateMessageID();
} else {
setHeader("Message-ID", email.getId());
}
}
};
// set basic email properties
message.setSubject(email.getSubject(), CHARACTER_ENCODING);
message.setFrom(new InternetAddress(email.getFromRecipient().getAddress(), email.getFromRecipient().getName(), CHARACTER_ENCODING));
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/simplejavamail/email/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
*/
@SuppressWarnings("SameParameterValue")
public class Email {

/**
* Optional ID, which will be used when sending using the underlying Java Mail framework. Will be generated otherwise.
* <p>
* 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.
* <p>
* The id-format should be conform <a href="https://tools.ietf.org/html/rfc5322#section-3.6.4">rfc5322#section-3.6.4</a>
*/
private String id;

/**
* The sender of the email. Can be used in conjunction with {@link #replyToRecipient}.
*/
Expand Down Expand Up @@ -173,6 +183,13 @@ public void signWithDomainKey(@Nonnull final InputStream dkimPrivateKeyInputStre
this.selector = checkNonEmptyArgument(selector, "selector");
}

/**
* Bean setter for {@link #id}.
*/
public void setId(@Nullable final String id) {
this.id = id;
}

/**
* Sets the sender address.
*
Expand Down Expand Up @@ -355,6 +372,13 @@ public void addAttachment(@Nullable final String name, @Nonnull final DataSource
attachments.add(new AttachmentResource(MiscUtil.encodeText(name), filedata));
}

/**
* Bean getter for {@link #id}.
*/
public String getId() {
return id;
}

/**
* Bean getter for {@link #fromRecipient}.
*/
Expand Down Expand Up @@ -452,6 +476,7 @@ public boolean equals(final Object o) {
@Override
public String toString() {
return "Email{" +
"\n\tid=" + id +
"\n\tfromRecipient=" + fromRecipient +
",\n\treplyToRecipient=" + replyToRecipient +
",\n\ttext='" + text + '\'' +
Expand All @@ -476,6 +501,7 @@ public String toString() {
attachments = builder.getAttachments();
headers = builder.getHeaders();

id = builder.getId();
fromRecipient = builder.getFromRecipient();
replyToRecipient = builder.getReplyToRecipient();
text = builder.getText();
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/org/simplejavamail/email/EmailBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
*/
@SuppressWarnings("UnusedReturnValue")
public class EmailBuilder {

/**
* Optional ID, which will be used when sending using the underlying Java Mail framework. Will be generated otherwise.
* <p>
* 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.
* <p>
* The id-format should be conform <a href="https://tools.ietf.org/html/rfc5322#section-3.6.4">rfc5322#section-3.6.4</a>
*/
private String id;

private Recipient fromRecipient;
/**
* The reply-to-address, optional. Can be used in conjunction with {@link #fromRecipient}.
Expand Down Expand Up @@ -139,13 +149,18 @@ public EmailBuilder() {
}
}

/**
*
*/
public Email build() {
return new Email(this);
}

/**
* Sets the optional id to be used when sending using the underlying Java Mail framework. Will be generated otherwise.
*/
public EmailBuilder id(@Nullable final String id) {
this.id = id;
return this;
}

/**
* Sets the sender address {@link #fromRecipient}.
*
Expand Down Expand Up @@ -523,6 +538,10 @@ public EmailBuilder signWithDomainKey(@Nonnull final InputStream dkimPrivateKeyI
SETTERS / GETTERS
*/

public String getId() {
return id;
}

public Recipient getFromRecipient() {
return fromRecipient;
}
Expand Down
16 changes: 15 additions & 1 deletion src/test/java/org/simplejavamail/mailer/MailerLiveTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.simplejavamail.mailer;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.simplejavamail.email.AttachmentResource;
Expand All @@ -14,12 +15,14 @@
import testutil.testrules.TestSmtpServer;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.Properties;

import static javax.mail.Message.RecipientType.TO;
import static org.assertj.core.api.Assertions.assertThat;
import static org.simplejavamail.converter.EmailConverter.mimeMessageToEmail;
import static org.simplejavamail.internal.util.MiscUtil.valueNullOrEmpty;
import static testutil.EmailHelper.normalizeText;
import static testutil.EmailHelper.readOutlookMessage;

Expand All @@ -45,6 +48,13 @@ public void createMailSession_StandardDummyMail()
assertSendingEmail(EmailHelper.createDummyEmail());
}

@Test
@Ignore("Unfortunately, Wiser doesn't seem to get the ID back, but I confirmed with gmail that the (correct) ID should be there")
public void createMailSession_StandardDummyMailWithId()
throws IOException, MessagingException {
assertSendingEmail(EmailHelper.createDummyEmail("<123@456>"));
}

@Test
public void createMailSession_OutlookMessageTest()
throws IOException, MessagingException {
Expand Down Expand Up @@ -78,7 +88,11 @@ public void createMailSession_OutlookMessageTest()
private Email assertSendingEmail(final Email originalEmail)
throws MessagingException {
mailer.sendMail(originalEmail);
Email receivedEmail = mimeMessageToEmail(smtpServerRule.getOnlyMessage());
MimeMessage receivedMimeMessage = smtpServerRule.getOnlyMessage();
if (!valueNullOrEmpty(originalEmail.getId())) {
assertThat(receivedMimeMessage.getMessageID()).isEqualTo(originalEmail.getId());
}
Email receivedEmail = mimeMessageToEmail(receivedMimeMessage);
assertThat(receivedEmail).isEqualTo(originalEmail);
return receivedEmail;
}
Expand Down
16 changes: 11 additions & 5 deletions src/test/java/testutil/EmailHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.simplejavamail.email.Email;

import javax.annotation.Nullable;
import javax.mail.util.ByteArrayDataSource;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -12,10 +13,15 @@
import static org.simplejavamail.converter.EmailConverter.outlookMsgToEmail;

public class EmailHelper {

public static Email createDummyEmail()

public static Email createDummyEmail() throws IOException {
return createDummyEmail(null);
}

public static Email createDummyEmail(@Nullable String id)
throws IOException {
final Email emailNormal = new Email();
emailNormal.setId(id);
emailNormal.setFromAddress("lollypop", "[email protected]");
// normally not needed, but for the test it is because the MimeMessage will
// have it added automatically as well, so the parsed Email will also have it then
Expand All @@ -25,7 +31,7 @@ public static Email createDummyEmail()
emailNormal.setText("We should meet up!");
emailNormal.setTextHTML("<b>We should meet up!</b><img src='cid:thumbsup'>");
emailNormal.setSubject("hey");

// add two text files in different ways and a black thumbs up embedded image ->
ByteArrayDataSource namedAttachment = new ByteArrayDataSource("Black Tie Optional", "text/plain");
namedAttachment.setName("dresscode.txt"); // normally not needed, but otherwise the equals will fail
Expand All @@ -35,12 +41,12 @@ public static Email createDummyEmail()
emailNormal.addEmbeddedImage("thumbsup", parseBase64Binary(base64String), "image/png");
return emailNormal;
}

public static Email readOutlookMessage(final String filePath) {
InputStream resourceAsStream = EmailHelper.class.getClassLoader().getResourceAsStream(filePath);
return outlookMsgToEmail(resourceAsStream);
}

public static String normalizeText(String text) {
return text.replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
}
Expand Down

0 comments on commit 178a519

Please sign in to comment.