Skip to content

Commit

Permalink
WIP: switch over dependencies to commons-email2 Jakarta
Browse files Browse the repository at this point in the history
This addresses playframework#261 but is not yet ready for merging.
  • Loading branch information
mikesname committed Sep 4, 2024
1 parent 826c407 commit 9ac596e
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 27 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ lazy val `play-mailer` = (project in file("play-mailer"))
"javax.inject" % "javax.inject" % "1",
"com.typesafe" % "config" % "1.4.3",
"org.slf4j" % "slf4j-api" % "2.0.16",
"org.apache.commons" % "commons-email" % "1.5",
"org.apache.commons" % "commons-email2-core" % "2.0.0-M1",
"org.apache.commons" % "commons-email2-jakarta" % "2.0.0-M1",
"org.playframework" %% "play" % Dependencies.PlayVersion % Test,
"org.playframework" %% "play-specs2" % Dependencies.PlayVersion % Test
),
Expand Down
2 changes: 1 addition & 1 deletion play-mailer/src/main/java/play/libs/mailer/Attachment.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package play.libs.mailer;

import javax.activation.DataSource;
import jakarta.activation.DataSource;
import java.io.File;
import java.net.URL;

Expand Down
2 changes: 1 addition & 1 deletion play-mailer/src/main/java/play/libs/mailer/Email.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package play.libs.mailer;

import javax.activation.DataSource;
import jakarta.activation.DataSource;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package play.api.libs.mailer

import jakarta.activation.DataSource

import java.io.File
import java.net.URL
import javax.activation.DataSource

sealed trait Attachment

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package play.api.libs.mailer

import java.io.FilterOutputStream
import java.io.PrintStream

import javax.mail.internet.InternetAddress
import javax.mail.Session
import org.apache.commons.mail.DefaultAuthenticator
import org.apache.commons.mail.EmailAttachment
import org.apache.commons.mail.HtmlEmail
import org.apache.commons.mail.MultiPartEmail
import jakarta.activation.URLDataSource
import jakarta.mail.Session
import jakarta.mail.internet.InternetAddress
import org.apache.commons.mail2.jakarta.{ DefaultAuthenticator, EmailAttachment, HtmlEmail, MultiPartEmail }
import org.slf4j.LoggerFactory

import javax.activation.URLDataSource
import java.io.{ FilterOutputStream, PrintStream }
import java.time
import scala.collection.JavaConverters._
import scala.util.control.NonFatal

Expand Down Expand Up @@ -39,8 +35,8 @@ abstract class CommonsMailer(conf: SMTPConfiguration) extends MailerClient {
data.headers.foreach {
header => email.addHeader(header._1, header._2)
}
conf.timeout.foreach(email.setSocketTimeout)
conf.connectionTimeout.foreach(email.setSocketConnectionTimeout)
conf.timeout.foreach(timeout => email.setSocketTimeout(java.time.Duration.ofMillis(timeout)))
conf.connectionTimeout.foreach(timeout => email.setSocketConnectionTimeout(time.Duration.ofMillis(timeout)))
data.attachments.foreach {
case attachmentData: AttachmentData =>
handleAttachmentData(email, attachmentData)
Expand Down Expand Up @@ -131,7 +127,7 @@ abstract class CommonsMailer(conf: SMTPConfiguration) extends MailerClient {
private def handleAttachmentData(email: MultiPartEmail, attachmentData: AttachmentData): Unit = {
val description = attachmentData.description.getOrElse(attachmentData.name)
val disposition = attachmentData.disposition.getOrElse(EmailAttachment.ATTACHMENT)
val dataSource = new javax.mail.util.ByteArrayDataSource(attachmentData.data, attachmentData.mimetype)
val dataSource = new jakarta.mail.util.ByteArrayDataSource(attachmentData.data, attachmentData.mimetype)
attachmentData.contentId match {
case Some(cid) =>
email match {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package play.api.libs.mailer

import javax.inject.Inject
import org.apache.commons.mail2.jakarta.{ HtmlEmail, MultiPartEmail }

import org.apache.commons.mail.{ HtmlEmail, MultiPartEmail }
import javax.inject.Inject

class SMTPMailer @Inject() (smtpConfiguration: SMTPConfiguration) extends MailerClient {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package play.api.libs.mailer

import java.io.File
import javax.mail.Part

import com.typesafe.config.{ Config, ConfigFactory }
import org.apache.commons.mail.{ EmailConstants, HtmlEmail, MultiPartEmail }
import jakarta.mail.Part
import org.apache.commons.mail2.core.EmailConstants
import org.apache.commons.mail2.jakarta.{ HtmlEmail, MultiPartEmail }
import org.specs2.mutable._

import java.io.File

class MailerPluginSpec extends Specification {

object SimpleMailerClient extends MailerClient {
Expand All @@ -18,8 +19,6 @@ class MailerPluginSpec extends Specification {
override def getContainer = super.getContainer
}
class MockHtmlEmail extends HtmlEmail {
def getHtml = this.html
def getText = this.text
override def getPrimaryBodyPart = super.getPrimaryBodyPart
override def getContainer = super.getContainer
}
Expand Down Expand Up @@ -67,8 +66,8 @@ class MailerPluginSpec extends Specification {
subject = "Subject",
from = "John Doe <[email protected]>"
))
email.getSocketTimeout mustEqual EmailConstants.SOCKET_TIMEOUT_MS
email.getSocketConnectionTimeout mustEqual EmailConstants.SOCKET_TIMEOUT_MS
email.getSocketTimeout mustEqual EmailConstants.SOCKET_TIMEOUT.toMillis.toInt
email.getSocketConnectionTimeout mustEqual EmailConstants.SOCKET_TIMEOUT.toMillis.toInt
}

"configure the SMTP local host if configured" in {
Expand Down

0 comments on commit 9ac596e

Please sign in to comment.