Skip to content

Commit

Permalink
#241: distinguish properly between TO, CC and BCC recipients when con…
Browse files Browse the repository at this point in the history
…verting from Outlook messages
  • Loading branch information
bbottema committed Jan 7, 2020
1 parent 1856f47 commit 9e739cd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
2 changes: 1 addition & 1 deletion angular-app
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.simplejavamail.api.email.EmailPopulatingBuilder;
import org.simplejavamail.api.email.EmailStartingBuilder;
import org.simplejavamail.api.email.Recipient;
import org.simplejavamail.api.internal.outlooksupport.model.EmailFromOutlookMessage;
import org.simplejavamail.internal.modules.OutlookModule;
import org.simplejavamail.internal.outlooksupport.internal.model.OutlookMessageProxy;
Expand All @@ -15,6 +16,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Objects;

import static org.simplejavamail.internal.util.MiscUtil.extractCID;
import static org.simplejavamail.internal.util.Preconditions.assumeNonNull;
Expand Down Expand Up @@ -75,16 +77,24 @@ private static EmailFromOutlookMessage buildEmailFromOutlookMessage(
}

private static void copyReceiversFromOutlookMessage(@NotNull EmailPopulatingBuilder builder, @NotNull OutlookMessage outlookMessage) {
for (final OutlookRecipient to : outlookMessage.getRecipients()) {
builder.to(to.getName(), to.getAddress());
}
//noinspection QuestionableName
for (final OutlookRecipient cc : outlookMessage.getCcRecipients()) {
builder.cc(cc.getName(), cc.getAddress());
}
for (final OutlookRecipient bcc : outlookMessage.getBccRecipients()) {
builder.bcc(bcc.getName(), bcc.getAddress());
}
// only add remaining recipients...
outerloop:
for (final OutlookRecipient to : outlookMessage.getRecipients()) {
for (final Recipient recipient : builder.getRecipients()) {
if (Objects.equals(recipient.getName(), to.getName()) &&
Objects.equals(recipient.getAddress(), to.getAddress())) {
continue outerloop;
}
}
builder.to(to.getName(), to.getAddress());
}
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,24 @@ public static EmailPopulatingBuilder mimeMessageToEmailBuilder(@NotNull final Mi
/**
* Delegates to {@link #outlookMsgToEmail(String, Pkcs12Config)}.
*
* @param msgFile The content of an Outlook (.msg) message from which to create the {@link Email}.
* @param msgData The content of an Outlook (.msg) message from which to create the {@link Email}.
*/
@SuppressWarnings("unused")
@NotNull
public static Email outlookMsgToEmail(@NotNull final String msgFile) {
return outlookMsgToEmail(msgFile, null);
public static Email outlookMsgToEmail(@NotNull final String msgData) {
return outlookMsgToEmail(msgData, null);
}

/**
* @param msgFile The content of an Outlook (.msg) message from which to create the {@link Email}.
* @param msgData The content of an Outlook (.msg) message from which to create the {@link Email}.
* @param pkcs12Config Private key store for decrypting S/MIME encrypted attachments
* (only needed when the message is encrypted rather than just signed).
*/
@SuppressWarnings("deprecation")
@NotNull
public static Email outlookMsgToEmail(@NotNull final String msgFile, @Nullable final Pkcs12Config pkcs12Config) {
checkNonEmptyArgument(msgFile, "msgFile");
EmailFromOutlookMessage result = ModuleLoader.loadOutlookModule().outlookMsgToEmailBuilder(msgFile, new EmailStartingBuilderImpl());
public static Email outlookMsgToEmail(@NotNull final String msgData, @Nullable final Pkcs12Config pkcs12Config) {
checkNonEmptyArgument(msgData, "msgFile");
EmailFromOutlookMessage result = ModuleLoader.loadOutlookModule().outlookMsgToEmailBuilder(msgData, new EmailStartingBuilderImpl());
return decryptAttachments(result.getEmailBuilder(), result.getOutlookMessage(), pkcs12Config)
.buildEmail();
}
Expand Down Expand Up @@ -372,9 +372,9 @@ public static MimeMessage outlookMsgToMimeMessage(@NotNull final String msgFile)
* @return Result of {@link #outlookMsgToEmail(String, Pkcs12Config)} and {@link #emailToMimeMessage(Email)}.
*/
@NotNull
public static MimeMessage outlookMsgToMimeMessage(@NotNull final String msgFile, @Nullable final Pkcs12Config pkcs12Config) {
checkNonEmptyArgument(msgFile, "outlookMsgData");
return emailToMimeMessage(outlookMsgToEmail(msgFile, pkcs12Config));
public static MimeMessage outlookMsgToMimeMessage(@NotNull final String msgData, @Nullable final Pkcs12Config pkcs12Config) {
checkNonEmptyArgument(msgData, "outlookMsgData");
return emailToMimeMessage(outlookMsgToEmail(msgData, pkcs12Config));
}

/**
Expand Down Expand Up @@ -535,9 +535,9 @@ public static String outlookMsgToEML(@NotNull final String msgFile) {
* @return Result of {@link #outlookMsgToEmail(String, Pkcs12Config)} and {@link #emailToEML(Email)}
*/
@NotNull
public static String outlookMsgToEML(@NotNull final String msgFile, @Nullable final Pkcs12Config pkcs12Config) {
checkNonEmptyArgument(msgFile, "outlookMsgData");
return emailToEML(outlookMsgToEmail(msgFile, pkcs12Config));
public static String outlookMsgToEML(@NotNull final String msgData, @Nullable final Pkcs12Config pkcs12Config) {
checkNonEmptyArgument(msgData, "outlookMsgData");
return emailToEML(outlookMsgToEmail(msgData, pkcs12Config));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.simplejavamail.converter;

import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import org.simplejavamail.api.email.Email;
import org.simplejavamail.api.email.EmailAssert;
import org.simplejavamail.api.email.Recipient;

import java.io.File;

import static demo.ResourceFolderHelper.determineResourceFolder;
import static javax.mail.Message.RecipientType.CC;
import static javax.mail.Message.RecipientType.TO;
import static org.assertj.core.api.Assertions.assertThat;
import static org.simplejavamail.internal.util.MiscUtil.normalizeNewlines;

public class EmailConverterTest {

private static final String RESOURCE_FOLDER = determineResourceFolder("simple-java-mail") + "/test/resources/test-messages";

@Test
public void testOutlookBasicConversions() {
final Recipient elias = new Recipient("Elias Laugher", "[email protected]", null);
final Recipient sven = new Recipient("Sven Sielenkemper", "[email protected]", TO);
final Recipient niklas = new Recipient("[email protected]", "[email protected]", CC);

@NotNull Email msg = EmailConverter.outlookMsgToEmail(new File(RESOURCE_FOLDER + "/simple email with TO and CC.msg"));
EmailAssert.assertThat(msg).hasFromRecipient(elias);
EmailAssert.assertThat(msg).hasSubject("Test E-Mail");
EmailAssert.assertThat(msg).hasOnlyRecipients(sven, niklas);
EmailAssert.assertThat(msg).hasNoAttachments();
assertThat(msg.getPlainText()).isNotEmpty();
assertThat(normalizeNewlines(msg.getHTMLText())).isEqualTo("<div dir=\"auto\">Just a test to get an email with one cc recipient.</div>\n");
assertThat(normalizeNewlines(msg.getPlainText())).isEqualTo("Just a test to get an email with one cc recipient.\n");
}
}
Binary file not shown.

0 comments on commit 9e739cd

Please sign in to comment.