CARVIEW |
Select Language
HTTP/2 200
date: Sat, 11 Oct 2025 06:52:20 GMT
content-type: text/html; charset=UTF-8
server: cloudflare
x-frame-options: DENY
x-content-type-options: nosniff
x-xss-protection: 1;mode=block
vary: accept-encoding
cf-cache-status: DYNAMIC
content-encoding: gzip
set-cookie: _csrf-frontend=a10ea910412bd7b95b11e0b779d65d89836a3d98587db8a1b9effe40b1d464b6a%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%22tX4YGe1KkBG4f9go_OX-yN7FFRpe7c4G%22%3B%7D; HttpOnly; Path=/
cf-ray: 98cc6f246f4dc16f-BLR
Simple jakarta.mail wrapper with Scala3 and Cats - Pastebin.com
SHARE
TWEET

Simple jakarta.mail wrapper with Scala3 and Cats
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cats.*
- import cats.effect.*
- import cats.effect.std.*
- import cats.syntax.all.*
- import io.odin.Logger // can be any logger, log4cats, tofu, etc, odin just for example
- import jakarta.mail.*
- import jakarta.mail.internet.{InternetAddress, MimeMessage}
- import scala.concurrent.ExecutionContext
- trait Mailer[F[_]]:
- def send(subject: String, content: String, recipient: String): F[Unit]
- object Mailer:
- case class MailConfig(
- queueSize: Int,
- user: String,
- password: String,
- sender: String,
- host: String,
- port: Int,
- )
- def make[F[_]: Async](blockingEC: ExecutionContext, config: MailConfig, logger: Logger[F]): Resource[F, Mailer[F]] =
- case class Mail(subject: String, content: String, recipient: String)
- def send(queue: Queue[F, Mail], sender: InternetAddress, session: Session): F[Unit] =
- queue.take.flatMap { mail =>
- val message = MimeMessage(session)
- message.setFrom(sender)
- message.setRecipients(
- Message.RecipientType.TO,
- InternetAddress.parse(mail.recipient).asInstanceOf[Array[Address]],
- )
- message.setSubject(mail.subject)
- message.setContent(mail.content, "text/html")
- message.saveChanges()
- Async[F].start {
- Async[F].blocking(Transport.send(message))
- .attempt.flatMap {
- case Right(_) =>
- logger.debug(s"Sent message to ${mail.recipient} with subject ${mail.subject}.")
- case Left(exc) =>
- logger.error(s"Can't send message to ${mail.recipient} with subject ${mail.subject}.", exc)
- }
- }
- } >> send(queue, sender, session)
- end send
- for
- queue <- Resource.eval {
- Queue.bounded[F, Mail](config.queueSize)
- }
- props = System.getProperties
- _ = props.setProperty("mail.smtp.host", config.host)
- _ = props.setProperty("mail.smtp.port", config.port.toString)
- _ = props.setProperty("mail.smtp.auth", true.toString)
- _ = props.setProperty("mail.smtp.starttls.enable", true.toString)
- auth = new Authenticator {
- override protected def getPasswordAuthentication: PasswordAuthentication =
- new PasswordAuthentication (config.user, config.password)
- }
- session = Session.getInstance(props, auth)
- sender = new InternetAddress(config.sender)
- _ <- Async[F].backgroundOn(
- send(queue, sender, session),
- blockingEC,
- )
- yield new Mailer[F] {
- override def send(subject: String, content: String, recipient: String): F[Unit] =
- queue.offer(
- Mail(
- subject,
- content,
- email,
- )
- ) *> logger.debug(s"Sending message to ${mail.recipient} with subject ${mail.subject}.")
- }
- end make
- end Mailer
Advertisement
Add Comment
Please, Sign In to add comment
-
✅ Make $2500 in 20 minutes⭐⭐⭐ H
JavaScript | 1 sec ago | 0.24 KB
-
⭐✅ MAKE $2000 INSTANTLY ✅ NEVER SEEN BEFORE ⭐...
JavaScript | 9 sec ago | 0.24 KB
-
⭐⭐⭐Crypto Accounts⭐⭐
Java | 12 sec ago | 0.10 KB
-
⭐⭐Exchange Exploit⭐⭐ M
JavaScript | 12 sec ago | 0.24 KB
-
⭐ Free Crypto Method ✅ NEVER SEEN BEFORE ⭐⭐⭐
JavaScript | 18 sec ago | 0.24 KB
-
⭐⭐⭐MAKE $900 INSTANTLY⭐⭐
Java | 23 sec ago | 0.10 KB
-
✅⭐ Make huge profits on trading ⭐⭐ 4
JavaScript | 23 sec ago | 0.24 KB
-
✅⭐ Make huge profits on trading ✅ NEVER SEEN...
JavaScript | 27 sec ago | 0.24 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand