Skip to content

Commit

Permalink
#7: fix missing S/MIME details on the resulting Outlook message
Browse files Browse the repository at this point in the history
  • Loading branch information
bbottema committed Apr 14, 2019
1 parent c9386f5 commit 3c8b315
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Set;
import java.util.TreeMap;

import static java.util.Arrays.copyOfRange;
import static java.util.regex.Pattern.compile;

/**
Expand Down Expand Up @@ -64,6 +65,18 @@ public class OutlookMessage {
* The name part of Reply-To header
*/
private String replyToName;
/**
* The MIME part of the S/MIME header
*/
private String smimeMime;
/**
* The S/MIME type part of the S/MIME header
*/
private String smimeType;
/**
* The S/MIME name part of the S/MIME header
*/
private String smimeName;
/**
* The mail's subject.
*/
Expand Down Expand Up @@ -239,6 +252,9 @@ public void setProperty(final OutlookMessageProperty msgProp) {
case 0x39: //CLIENT SUBMIT TIME
setClientSubmitTime(stringValue);
break;
case 0x8005: // S/MIME details
setSmime(stringValue);
break;
}

// save all properties (incl. those identified above)
Expand All @@ -261,7 +277,7 @@ public void setProperty(final OutlookMessageProperty msgProp) {
// 3003: email address
// 1008 rtf sync
}

private String convertValueToString(final Object value) {
if (value == null) {
return null;
Expand Down Expand Up @@ -379,47 +395,43 @@ private byte[] decompressRtfBytes(final byte[] value) {

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("From: ").append(createMailString(fromEmail, fromName)).append("\n");
sb.append("To: ").append(createMailString(toEmail, toName)).append("\n");
if (date != null) {
final SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);
sb.append("Date: ").append(formatter.format(date)).append("\n");
}
if (subject != null) {
sb.append("Subject: ").append(subject).append("\n");
}
sb.append("").append(outlookAttachments.size()).append(" outlookAttachments.");
final StringBuilder sb = commonToString();
sb.append(outlookAttachments.size()).append(" outlookAttachments.");
return sb.toString();
}

/**
* @return All information of this message object.
*/
public String toLongString() {
final StringBuilder sb = new StringBuilder();
sb.append("From: ").append(createMailString(fromEmail, fromName)).append("\n");
sb.append("To: ").append(createMailString(toEmail, toName)).append("\n");
if (date != null) {
final SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);
sb.append("Date: ").append(formatter.format(date)).append("\n");
}
if (subject != null) {
sb.append("Subject: ").append(subject).append("\n");
}
final StringBuilder sb = commonToString();
sb.append("\n");
if (bodyText != null) {
sb.append(bodyText);
}
if (!outlookAttachments.isEmpty()) {
sb.append("\n");
sb.append("").append(outlookAttachments.size()).append(" outlookAttachments.\n");
sb.append(outlookAttachments.size()).append(" outlookAttachments.\n");
for (final OutlookAttachment att : outlookAttachments) {
sb.append(att).append("\n");
}
}
return sb.toString();
}

private StringBuilder commonToString() {
final StringBuilder sb = new StringBuilder();
sb.append("From: ").append(createMailString(fromEmail, fromName)).append("\n");
sb.append("To: ").append(createMailString(toEmail, toName)).append("\n");
if (date != null) {
final SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);
sb.append("Date: ").append(formatter.format(date)).append("\n");
}
if (subject != null) {
sb.append("Subject: ").append(subject).append("\n");
}
return sb;
}

/**
* Convenience method for creating an email address expression (including the name, the address, or both).
Expand All @@ -443,6 +455,22 @@ private String createMailString(final String mail, final String name) {
}
return "\"" + name + "\" <" + mail + ">";
}

void setSmime(String smimeHeader) {
// application/pkcs7-mime;smime-type=signed-data;name=smime.p7m
if (smimeHeader != null) {
final String[] smimeHeaderParts = smimeHeader.split(";");
setSmimeMime(smimeHeaderParts[0]);
for (String smimeHeaderParam : copyOfRange(smimeHeaderParts, 1, smimeHeaderParts.length)) {
final String[] smimeParamParts = smimeHeaderParam.split("=");
if (smimeParamParts[0].equals("smime-type")) {
setSmimeType(smimeParamParts[1]);
} else if (smimeParamParts[0].equals("name")) {
setSmimeName(smimeParamParts[1]);
}
}
}
}

/**
* Bean getter for {@link #outlookAttachments}.
Expand Down Expand Up @@ -1002,11 +1030,53 @@ public void setReplyToEmail(final String replyToEmail) {
public String getReplyToName() {
return replyToName;
}

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

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

/**
* Bean setter for {@link #smimeMime}.
*/
public void setSmimeMime(String smimeMime) {
this.smimeMime = smimeMime;
}

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

/**
* Bean setter for {@link #smimeType}.
*/
public void setSmimeType(String smimeType) {
this.smimeType = smimeType;
}

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

/**
* Bean setter for {@link #smimeName}.
*/
public void setSmimeName(String smimeName) {
this.smimeName = smimeName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.simplejavamail.outlookmessageparser.model;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class OutlookMessageTest {
@Test
public void testSetSmime() {
// application/pkcs7-mime;smime-type=signed-data;name=smime.p7m
testSmime(null, null, null, null);
testSmime("application/pkcs7-mime", "application/pkcs7-mime", null, null);
testSmime("application/pkcs7-mime;", "application/pkcs7-mime", null, null);
testSmime("application/pkcs7-mime;name=moo", "application/pkcs7-mime", null, "moo");
testSmime("application/pkcs7-mime;smime-type=signed-data;name=smime.p7m", "application/pkcs7-mime", "signed-data", "smime.p7m");
testSmime("application/pkcs7-mime;name=smime.p7m;smime-type=signed-data", "application/pkcs7-mime", "signed-data", "smime.p7m");
testSmime("application/pkcs7-mime;name=smime.p7m;smime-type=signed-data;", "application/pkcs7-mime", "signed-data", "smime.p7m");
}

private void testSmime(String smimeHeader, String smimeMime, String smimeType, String smimeName) {
OutlookMessage msg = new OutlookMessage();
msg.setSmime(smimeHeader);
assertThat(msg.getSmimeMime()).isEqualTo(smimeMime);
assertThat(msg.getSmimeType()).isEqualTo(smimeType);
assertThat(msg.getSmimeName()).isEqualTo(smimeName);
}
}

0 comments on commit 3c8b315

Please sign in to comment.