diff --git a/modules/javamail/src/main/scala/emil/javamail/conv/BasicEncode.scala b/modules/javamail/src/main/scala/emil/javamail/conv/BasicEncode.scala index 5afc8662..b59de160 100644 --- a/modules/javamail/src/main/scala/emil/javamail/conv/BasicEncode.scala +++ b/modules/javamail/src/main/scala/emil/javamail/conv/BasicEncode.scala @@ -11,8 +11,10 @@ import emil.javamail.internal.{EmilMimeMessage, ThreadClassLoader} import jakarta.activation.{DataHandler, DataSource} import jakarta.mail._ import jakarta.mail.internet._ +import org.log4s.getLogger trait BasicEncode { + private val logger = getLogger implicit def flagEncode: Conv[Flag, Flags.Flag] = Conv { case Flag.Flagged => @@ -20,7 +22,24 @@ trait BasicEncode { } implicit def mailAddressEncode: Conv[MailAddress, InternetAddress] = - Conv(mailAddress => new InternetAddress(mailAddress.displayString)) + Conv { mailAddress => + Either.catchNonFatal(new InternetAddress(mailAddress.displayString)) match { + case Right(a) => a + case Left(ex) => + logger.warn(ex)(s"Error converting MailAddress '${mailAddress}' to javamail!") + Either.catchNonFatal(new InternetAddress(mailAddress.address)) match { + case Right(a) => + logger.warn("Using address without name part.") + a + case Left(ex2) => + logger.warn( + s"Using address without name '${mailAddress.address}' also failed: ${ex2.getMessage}" + ) + ex.addSuppressed(ex2) + throw ex + } + } + } implicit def attachmentEncode[F[_]: Sync]: Conv[Attachment[F], F[MimeBodyPart]] = Conv { attach => diff --git a/modules/javamail/src/test/scala/emil/javamail/MailConvTest.scala b/modules/javamail/src/test/scala/emil/javamail/MailConvTest.scala index d13debf6..d81ef6a3 100644 --- a/modules/javamail/src/test/scala/emil/javamail/MailConvTest.scala +++ b/modules/javamail/src/test/scala/emil/javamail/MailConvTest.scala @@ -7,7 +7,9 @@ import cats.effect._ import cats.effect.unsafe.implicits.global import emil._ import emil.builder._ +import emil.javamail.conv._ import emil.javamail.syntax._ +import jakarta.mail.internet.InternetAddress import munit._ class MailConvTest extends FunSuite { @@ -23,6 +25,21 @@ class MailConvTest extends FunSuite { ) } + test("write mail address") { + val ma1 = MailAddress.unsafe(Some("a;b;c"), "me@localhost") + val ma2 = MailAddress.unsafe(Some("a;b;c"), "me<@localhost") + val ma3 = MailAddress.unsafe(Some("Company name"), "some.name@some.domain.com") + val addrConv = encode.mailAddressEncode + + assertEquals(addrConv.convert(ma1), new InternetAddress("me@localhost")) + intercept[Exception](addrConv.convert(ma2)) + assertEquals( + addrConv.convert(ma3), + new InternetAddress("\"Company name\" ") + ) + assertEquals(addrConv.convert(ma3), new InternetAddress(ma3.displayString)) + } + test("write text mail") { val mail = MailBuilder.build[IO]( From("test@test.com"),