Skip to content

Commit

Permalink
Merge pull request #342 from mikesname/feature/commons-email2
Browse files Browse the repository at this point in the history
Switch over dependencies to commons-email2 / Jakarta (WIP)
  • Loading branch information
mkurz authored Oct 8, 2024
2 parents 371841a + 4b01a8c commit a6ccece
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Use the `@Inject` annotation on the constructor, service of your component or co
```scala
import play.api.libs.mailer._
import java.io.File
import org.apache.commons.mail.EmailAttachment
import org.apache.commons.mail2.jakarta.EmailAttachment
import javax.inject.Inject

class MailerService @Inject() (mailerClient: MailerClient) {
Expand Down
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ lazy val commonSettings = Seq(
),
)

val previousVersion: Option[String] = Some("10.0.0")
val previousVersion: Option[String] = None

lazy val `play-mailer` = (project in file("play-mailer"))
.enablePlugins(Common)
Expand All @@ -45,7 +45,7 @@ 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-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,8 @@
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package controllers
import java.io.File

import play.api.Environment
import org.apache.commons.mail.EmailAttachment
import org.apache.commons.mail2.jakarta.EmailAttachment
import play.api.libs.mailer._
import play.api.mvc._

Expand Down
2 changes: 1 addition & 1 deletion samples/runtimeDI/app/controllers/ApplicationJava.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package controllers;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail2.jakarta.EmailAttachment;
import play.api.libs.mailer.MailerClient;
import play.libs.mailer.Email;
import play.mvc.Controller;
Expand Down
2 changes: 1 addition & 1 deletion samples/runtimeDI/app/controllers/ApplicationScala.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package controllers
import java.io.File
import javax.inject.Inject

import org.apache.commons.mail.EmailAttachment
import org.apache.commons.mail2.jakarta.EmailAttachment
import play.api.Environment
import play.api.libs.mailer._
import play.api.mvc._
Expand Down

0 comments on commit a6ccece

Please sign in to comment.