Skip to content

Commit

Permalink
Using jodd-util.
Browse files Browse the repository at this point in the history
  • Loading branch information
igr committed Sep 15, 2020
1 parent 63b12a9 commit 0438ef0
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 51 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# jodd-mail
Simple Java Mail library
# Jodd Mail

Simple Java Mail library.

![GitHub release](https://img.shields.io/github/release/oblac/jodd-mail.svg)
![Maven Central](https://img.shields.io/maven-central/v/org.jodd/jodd-mail)
[![Build Status](https://img.shields.io/travis/oblac/jodd.svg)](https://travis-ci.org/oblac/jodd-mail)
[![codecov](https://codecov.io/gh/oblac/jodd-mail/branch/master/graph/badge.svg)](https://codecov.io/gh/oblac/jodd-mail)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/14a1a4c731774b2994f66be1f6bc18c0)](https://www.codacy.com/gh/oblac/jodd-mail?utm_source=github.com&utm_medium=referral&utm_content=oblac/jodd-mail&utm_campaign=Badge_Grade)
[![Stack Overflow](https://img.shields.io/badge/stack%20overflow-jodd-4183C4.svg)](https://stackoverflow.com/questions/tagged/jodd)
[![BSD License](https://img.shields.io/badge/license-BSD--2--Clause-blue.svg)](https://github.com/oblac/jodd-mail/blob/master/LICENSE)

6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ plugins {

repositories {
mavenCentral()
mavenLocal()
// mavenLocal()
}

group = 'org.jodd'
//version = '6.0.0.' + timestamp() + "-SNAPSHOT"
version = '6.0.0'
version = '6.0.1'

rootProject.description = 'Java Mail client'

Expand All @@ -54,7 +54,7 @@ java {

dependencies {

implementation 'org.jodd:jodd-core:5.1.+'
implementation 'org.jodd:jodd-util:6.0.+'
implementation 'jakarta.mail:jakarta.mail-api:1.6.5'
implementation 'com.sun.mail:jakarta.mail:1.6.5'

Expand Down
11 changes: 5 additions & 6 deletions src/main/java/jodd/mail/EMLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@

package jodd.mail;

import jodd.core.JoddCore;
import jodd.io.StreamUtil;
import jodd.io.IOUtil;

import javax.mail.MessagingException;
import javax.mail.Session;
Expand Down Expand Up @@ -74,7 +73,7 @@ public ReceivedEmail parse(final String emlContent, final String charset) throws
*/
public ReceivedEmail parse(final String emlContent) throws MessagingException {
try {
return parse(emlContent, JoddCore.encoding);
return parse(emlContent, "UTF-8");
} catch (final UnsupportedEncodingException ignore) {
return null;
}
Expand Down Expand Up @@ -106,7 +105,7 @@ public ReceivedEmail parse(final File emlFile) throws FileNotFoundException, Mes
try {
return parse(fileInputStream);
} finally {
StreamUtil.close(fileInputStream);
IOUtil.close(fileInputStream);
}
}

Expand All @@ -126,7 +125,7 @@ protected ReceivedEmail parse(final InputStream emlContentInputStream) throws Me
final MimeMessage message = new MimeMessage(getSession(), emlContentInputStream);
return new ReceivedEmail(message, false, null);
} finally {
StreamUtil.close(emlContentInputStream);
IOUtil.close(emlContentInputStream);
}
}
}
}
5 changes: 2 additions & 3 deletions src/main/java/jodd/mail/EmailAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

package jodd.mail;

import jodd.core.JoddCore;
import jodd.util.StringUtil;

import javax.mail.Address;
Expand Down Expand Up @@ -157,7 +156,7 @@ public String toString() {
*/
public InternetAddress toInternetAddress() throws AddressException {
try {
return new InternetAddress(email, personalName, JoddCore.encoding);
return new InternetAddress(email, personalName, "UTF-8");
} catch (final UnsupportedEncodingException ueex) {
throw new AddressException(ueex.toString());
}
Expand Down Expand Up @@ -231,4 +230,4 @@ public static InternetAddress[] convert(final EmailAddress[] addresses) throws M
}
return address;
}
}
}
20 changes: 10 additions & 10 deletions src/main/java/jodd/mail/EmailAttachment.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

package jodd.mail;

import jodd.io.FastByteArrayOutputStream;
import jodd.io.StreamUtil;
import jodd.io.IOUtil;

import javax.activation.DataSource;
import javax.mail.internet.MimeUtility;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -255,11 +255,11 @@ protected EmailAttachment<T> setSize(final int size) {
* @return byte array with content of the attachment.
*/
public byte[] toByteArray() {
final FastByteArrayOutputStream out;
final ByteArrayOutputStream out;
if (size != -1) {
out = new FastByteArrayOutputStream(size);
out = new ByteArrayOutputStream(size);
} else {
out = new FastByteArrayOutputStream();
out = new ByteArrayOutputStream();
}
writeToStream(out);
return out.toByteArray();
Expand All @@ -277,14 +277,14 @@ public void writeToFile(final File destination) {
input = getDataSource().getInputStream();
output = new FileOutputStream(destination);

StreamUtil.copy(input, output);
IOUtil.copy(input, output);
}
catch (final IOException ioex) {
throw new MailException(ioex);
}
finally {
StreamUtil.close(input);
StreamUtil.close(output);
IOUtil.close(input);
IOUtil.close(output);
}
}

Expand All @@ -298,13 +298,13 @@ public void writeToStream(final OutputStream out) {
try {
input = getDataSource().getInputStream();

StreamUtil.copy(input, out);
IOUtil.copy(input, out);
}
catch (final IOException ioex) {
throw new MailException(ioex);
}
finally {
StreamUtil.close(input);
IOUtil.close(input);
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/jodd/mail/EmailMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

package jodd.mail;

import jodd.core.JoddCore;

/**
* Represents email message including the mime type and encoding.
*/
Expand Down Expand Up @@ -61,13 +59,13 @@ public EmailMessage(final String content, final String mimeType, final String en
}

/**
* Uses UTF-8 email content by default (as per {@link JoddCore#encoding}.
* Uses UTF-8 email content by default.
*
* @param content The content as a {@link String}.
* @param mimeType The MIME type as a as a {@link String}.
*/
public EmailMessage(final String content, final String mimeType) {
this(content, mimeType, JoddCore.encoding);
this(content, mimeType, "UTF-8");
}

// ---------------------------------------------------------------- getters
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/jodd/mail/EmailUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@

package jodd.mail;

import jodd.core.JoddCore;
import jodd.util.CharUtil;
import jodd.util.StringPool;

import javax.mail.*;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;

/**
Expand Down Expand Up @@ -102,7 +99,7 @@ public static String extractEncoding(final String contentType) {
*
* @param contentType content type.
* @param defaultEncoding Default encoding to be used if extract returns {@code null}.
* If defaultEncoding is {@code null}, {@link JoddCore#encoding} will be used.
* If defaultEncoding is {@code null}, default encoding will be used.
* @return Encoding from the content type.
* @see #extractEncoding(String)
*/
Expand All @@ -111,7 +108,7 @@ public static String extractEncoding(final String contentType, String defaultEnc

if (encoding == null) {
if (defaultEncoding == null) {
defaultEncoding = JoddCore.encoding;
defaultEncoding = "UTF-8";
}
encoding = defaultEncoding;
}
Expand Down Expand Up @@ -194,4 +191,4 @@ public static boolean isEmptyFlags(Flags flags) {
return true;
}

}
}
7 changes: 3 additions & 4 deletions src/main/java/jodd/mail/ReceivedEmail.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

package jodd.mail;

import jodd.util.StringPool;

import javax.mail.Address;
import javax.mail.Flags;
import javax.mail.Flags.Flag;
Expand All @@ -41,6 +39,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -223,7 +222,7 @@ private void processMultipart(final Multipart mp) throws MessagingException, IOE
*/
private void addStringContent(final Part part, final String content) throws MessagingException, UnsupportedEncodingException {
final String contentType = part.getContentType();
final String encoding = EmailUtil.extractEncoding(contentType, StringPool.US_ASCII);
final String encoding = EmailUtil.extractEncoding(contentType, StandardCharsets.US_ASCII.name());

final String disposition = part.getDisposition();

Expand Down Expand Up @@ -522,4 +521,4 @@ public List<ReceivedEmail> attachedMessages() {
return attachedMessages;
}

}
}
14 changes: 7 additions & 7 deletions src/test/java/jodd/mail/EMLParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
package jodd.mail;

import jodd.net.MimeTypes;
import jodd.util.StringPool;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -35,6 +34,7 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.sql.Date;
import java.time.Instant;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -71,8 +71,8 @@ void testParseEML() throws FileNotFoundException, MessagingException {
assertEquals("test!", email.subject());

// the time is specified in GMT zone
LocalDateTime jdt = LocalDateTime.of(2010, 3, 27, 12, 11, 21, 0);
Instant instant = jdt.atZone(ZoneId.of("GMT")).toInstant();
final LocalDateTime jdt = LocalDateTime.of(2010, 3, 27, 12, 11, 21, 0);
final Instant instant = jdt.atZone(ZoneId.of("GMT")).toInstant();

// compare
assertEquals(Date.from(instant), email.sentDate());
Expand All @@ -86,12 +86,12 @@ void testParseEML() throws FileNotFoundException, MessagingException {
EmailMessage msg1 = messages.get(0);
assertEquals("Test", msg1.getContent().trim());
assertEquals(MimeTypes.MIME_TEXT_PLAIN, msg1.getMimeType());
assertEquals(StringPool.US_ASCII.toLowerCase(), msg1.getEncoding());
assertEquals(StandardCharsets.US_ASCII.name().toLowerCase(), msg1.getEncoding());

EmailMessage msg2 = messages.get(1);
assertTrue(msg2.getContent().contains("Test<o:p>"));
assertEquals(MimeTypes.MIME_TEXT_HTML, msg2.getMimeType());
assertEquals(StringPool.US_ASCII.toLowerCase(), msg2.getEncoding());
assertEquals(StandardCharsets.US_ASCII.name().toLowerCase(), msg2.getEncoding());

List<EmailAttachment<? extends DataSource>> attachments = email.attachments();
assertNotNull(attachments);
Expand All @@ -118,12 +118,12 @@ void testParseEML() throws FileNotFoundException, MessagingException {
msg1 = messages.get(0);
assertEquals("test", msg1.getContent().trim());
assertEquals(MimeTypes.MIME_TEXT_PLAIN, msg1.getMimeType());
assertEquals(StringPool.US_ASCII.toLowerCase(), msg1.getEncoding());
assertEquals(StandardCharsets.US_ASCII.name().toLowerCase(), msg1.getEncoding());

msg2 = messages.get(1);
assertTrue(msg2.getContent().contains("test</TITLE>"));
assertEquals(MimeTypes.MIME_TEXT_HTML, msg2.getMimeType());
assertEquals(StringPool.US_ASCII.toLowerCase(), msg2.getEncoding());
assertEquals(StandardCharsets.US_ASCII.name().toLowerCase(), msg2.getEncoding());

attachments = email.attachments();
assertNotNull(attachments);
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/jodd/mail/EmailUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@

package jodd.mail;

import jodd.util.StringPool;
import jodd.net.MimeTypes;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import javax.mail.Flags;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class EmailUtilTest {

Expand All @@ -60,11 +62,11 @@ void testExtractContentType() {

contentType = "text/html;\n\tcharset=\"us-ascii\"";
assertEquals(MimeTypes.MIME_TEXT_HTML, EmailUtil.extractMimeType(contentType));
assertEquals(StringPool.US_ASCII.toLowerCase(), EmailUtil.extractEncoding(contentType));
assertEquals(StandardCharsets.US_ASCII.name().toLowerCase(), EmailUtil.extractEncoding(contentType));

contentType = "TEXT/PLAIN; charset=US-ASCII; name=example.eml";
assertEquals(MimeTypes.MIME_TEXT_PLAIN.toUpperCase(), EmailUtil.extractMimeType(contentType));
assertEquals(StringPool.US_ASCII, EmailUtil.extractEncoding(contentType));
assertEquals(StandardCharsets.US_ASCII.name(), EmailUtil.extractEncoding(contentType));
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/jodd/mail/SendMailTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

package jodd.mail;

import jodd.io.StreamUtil;
import jodd.io.IOUtil;
import jodd.net.MimeTypes;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -293,7 +293,7 @@ private Message createMessage(final Email email) throws MessagingException {

private byte[] read(final DataSource dataSource) throws IOException {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
StreamUtil.copy(dataSource.getInputStream(), os);
IOUtil.copy(dataSource.getInputStream(), os);
return os.toByteArray();
}
}

0 comments on commit 0438ef0

Please sign in to comment.