diff --git a/README.md b/README.md index baa6c2c..bc6d533 100644 --- a/README.md +++ b/README.md @@ -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) + diff --git a/build.gradle b/build.gradle index a0ce4e0..f29b294 100644 --- a/build.gradle +++ b/build.gradle @@ -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' @@ -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' diff --git a/src/main/java/jodd/mail/EMLParser.java b/src/main/java/jodd/mail/EMLParser.java index 3b89b4d..6238b95 100644 --- a/src/main/java/jodd/mail/EMLParser.java +++ b/src/main/java/jodd/mail/EMLParser.java @@ -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; @@ -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; } @@ -106,7 +105,7 @@ public ReceivedEmail parse(final File emlFile) throws FileNotFoundException, Mes try { return parse(fileInputStream); } finally { - StreamUtil.close(fileInputStream); + IOUtil.close(fileInputStream); } } @@ -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); } } -} \ No newline at end of file +} diff --git a/src/main/java/jodd/mail/EmailAddress.java b/src/main/java/jodd/mail/EmailAddress.java index d6dd898..efd1be2 100644 --- a/src/main/java/jodd/mail/EmailAddress.java +++ b/src/main/java/jodd/mail/EmailAddress.java @@ -25,7 +25,6 @@ package jodd.mail; -import jodd.core.JoddCore; import jodd.util.StringUtil; import javax.mail.Address; @@ -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()); } @@ -231,4 +230,4 @@ public static InternetAddress[] convert(final EmailAddress[] addresses) throws M } return address; } -} \ No newline at end of file +} diff --git a/src/main/java/jodd/mail/EmailAttachment.java b/src/main/java/jodd/mail/EmailAttachment.java index cd05637..d0a509f 100644 --- a/src/main/java/jodd/mail/EmailAttachment.java +++ b/src/main/java/jodd/mail/EmailAttachment.java @@ -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; @@ -255,11 +255,11 @@ protected EmailAttachment 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(); @@ -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); } } @@ -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); } } diff --git a/src/main/java/jodd/mail/EmailMessage.java b/src/main/java/jodd/mail/EmailMessage.java index 67f2753..8ae3753 100644 --- a/src/main/java/jodd/mail/EmailMessage.java +++ b/src/main/java/jodd/mail/EmailMessage.java @@ -25,8 +25,6 @@ package jodd.mail; -import jodd.core.JoddCore; - /** * Represents email message including the mime type and encoding. */ @@ -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 diff --git a/src/main/java/jodd/mail/EmailUtil.java b/src/main/java/jodd/mail/EmailUtil.java index abd60cf..fac1533 100644 --- a/src/main/java/jodd/mail/EmailUtil.java +++ b/src/main/java/jodd/mail/EmailUtil.java @@ -25,7 +25,6 @@ package jodd.mail; -import jodd.core.JoddCore; import jodd.util.CharUtil; import jodd.util.StringPool; @@ -33,8 +32,6 @@ 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; /** @@ -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) */ @@ -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; } @@ -194,4 +191,4 @@ public static boolean isEmptyFlags(Flags flags) { return true; } -} \ No newline at end of file +} diff --git a/src/main/java/jodd/mail/ReceivedEmail.java b/src/main/java/jodd/mail/ReceivedEmail.java index 79e5b4e..e04f25e 100644 --- a/src/main/java/jodd/mail/ReceivedEmail.java +++ b/src/main/java/jodd/mail/ReceivedEmail.java @@ -25,8 +25,6 @@ package jodd.mail; -import jodd.util.StringPool; - import javax.mail.Address; import javax.mail.Flags; import javax.mail.Flags.Flag; @@ -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; @@ -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(); @@ -522,4 +521,4 @@ public List attachedMessages() { return attachedMessages; } -} \ No newline at end of file +} diff --git a/src/test/java/jodd/mail/EMLParserTest.java b/src/test/java/jodd/mail/EMLParserTest.java index 475b064..3a3ef11 100644 --- a/src/test/java/jodd/mail/EMLParserTest.java +++ b/src/test/java/jodd/mail/EMLParserTest.java @@ -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; @@ -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; @@ -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()); @@ -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")); assertEquals(MimeTypes.MIME_TEXT_HTML, msg2.getMimeType()); - assertEquals(StringPool.US_ASCII.toLowerCase(), msg2.getEncoding()); + assertEquals(StandardCharsets.US_ASCII.name().toLowerCase(), msg2.getEncoding()); List> attachments = email.attachments(); assertNotNull(attachments); @@ -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")); 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); diff --git a/src/test/java/jodd/mail/EmailUtilTest.java b/src/test/java/jodd/mail/EmailUtilTest.java index 2c1c91d..3bfcf8f 100644 --- a/src/test/java/jodd/mail/EmailUtilTest.java +++ b/src/test/java/jodd/mail/EmailUtilTest.java @@ -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 { @@ -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 diff --git a/src/test/java/jodd/mail/SendMailTest.java b/src/test/java/jodd/mail/SendMailTest.java index fdfa3ce..3b64b58 100644 --- a/src/test/java/jodd/mail/SendMailTest.java +++ b/src/test/java/jodd/mail/SendMailTest.java @@ -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; @@ -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(); } }