Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/co/cfly/email/impl/util/MailUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Enumeration;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -290,8 +291,9 @@ public static String decodeString(String value) {
/**
* Determines the content type of the part, or empty if it cannot determine
*/
@SuppressWarnings("SwitchStatementWithTooFewBranches")
public static Charset determineCharset(Part part, Charset defaultValue) throws MessagingException {
return Arrays.stream(part.getHeader("Content-Type")).findFirst().map(value -> {
return Optional.ofNullable(part.getHeader("Content-Type")).flatMap(headers -> Arrays.stream(headers).findFirst()).map(value -> {
Matcher matcher = CHARSET_EXTRACT.matcher(value);
if (matcher.find()) {
String charsetName = matcher.group(1).trim().toUpperCase();
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/co/cfly/email/MailUtilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public void determineCharset() throws MessagingException {
Assert.assertEquals(StandardCharsets.UTF_8, MailUtility.determineCharset(part, StandardCharsets.UTF_8));
}

@Test
public void determineCharsetNoContentType() throws MessagingException {
MimeBodyPart part = new MimeBodyPart();
Assert.assertEquals(StandardCharsets.UTF_8, MailUtility.determineCharset(part, StandardCharsets.UTF_8));
}

@Test
public void determineCharsetQuoted() throws MessagingException {
MimeBodyPart part = new MimeBodyPart();
Expand Down