Examples
First, some imports and setup:
import cats.effect._
import emil._, emil.builder._
/* javamail backend */
import emil.javamail._
Creating an e-mail
Create a simple mail:
val mail: Mail[IO] = MailBuilder.build(
From("me@test.com"),
To("test@test.com"),
Subject("Hello!"),
CustomHeader(Header("User-Agent", "my-email-client")),
TextBody("Hello!\n\nThis is a mail."),
HtmlBody("<h1>Hello!</h1>\n<p>This <b>is</b> a mail.</p>"),
AttachUrl[IO](getClass.getResource("/files/Test.pdf")).
withFilename("test.pdf").
withMimeType(MimeType.pdf)
)
// mail: Mail[IO] = Mail(
// header = MailHeader(
// id = "",
// messageId = None,
// folder = None,
// recipients = Recipients(
// to = List(MailAddress(name = None, address = "test@test.com")),
// cc = List(),
// bcc = List()
// ),
// sender = None,
// from = Some(value = MailAddress(name = None, address = "me@test.com")),
// replyTo = None,
// originationDate = None,
// subject = "Hello!",
// received = List(),
// flags = Set()
// ),
// additionalHeaders = Headers(
// all = List(
// Header(
// name = "User-Agent",
// value = NonEmptyList(head = "my-email-client", tail = List())
// )
// )
// ),
// body = HtmlAndText(
// text = Pure(
// value = StringContent(
// asString = """Hello!
//
// This is a mail."""
// )
// ),
// html = Pure(
// value = StringContent(
// asString = """<h1>Hello!</h1>
// <p>This <b>is</b> a mail.</p>"""
// )
// )
// ),
// attachments = Attachments(
// all = Vector(
// Attachment(
// filename = Some(value = "test.pdf"),
// mimeType = MimeType(
// primary = "application",
// sub = "pdf",
// params = Map()
// ...
Sending Mails
In order to do something with it, a connection to a server is necessary and a concrete emil:
val myemil = JavaMailEmil[IO]()
// myemil: Emil[IO] = emil.javamail.JavaMailEmil@11c80834
val smtpConf = MailConfig("smtp://devmail:25", "dev", "dev", SSLType.NoEncryption)
// smtpConf: MailConfig = MailConfig(
// url = "smtp://devmail:25",
// user = "dev",
// password = "dev",
// sslType = NoEncryption,
// enableXOAuth2 = false,
// disableCertificateCheck = false,
// timeout = 10 seconds
// )
Finally, create a program that sends the mail:
val sendIO = myemil(smtpConf).send(mail)
// sendIO: IO[cats.data.NonEmptyList[String]] = Uncancelable(
// body = cats.effect.IO$$$Lambda$1486/0x00000008017023c0@22b771d1,
// event = cats.effect.tracing.TracingEvent$StackTrace
// )
Accessing Mails
The JavaMail backend implements IMAP access to mailboxes. First, a connection to an imap server is necessary:
val imapConf = MailConfig("imap://devmail:143", "dev", "dev", SSLType.NoEncryption)
// imapConf: MailConfig = MailConfig(
// url = "imap://devmail:143",
// user = "dev",
// password = "dev",
// sslType = NoEncryption,
// enableXOAuth2 = false,
// disableCertificateCheck = false,
// timeout = 10 seconds
// )
Then run an operation from the email.access
interface:
val readIO = myemil(imapConf).run(myemil.access.getInbox)
// readIO: IO[MailFolder] = Uncancelable(
// body = cats.effect.IO$$$Lambda$1486/0x00000008017023c0@3fd006e8,
// event = cats.effect.tracing.TracingEvent$StackTrace
// )