Skip to content

Instantly share code, notes, and snippets.

@tombasche
Created August 18, 2021 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tombasche/be4cf6f5c654564e0f496e94991433cd to your computer and use it in GitHub Desktop.
Save tombasche/be4cf6f5c654564e0f496e94991433cd to your computer and use it in GitHub Desktop.
Basic email client using AWS SES + Spring + Kotlin
package com.myapplication.email
import arrow.core.Either
import org.springframework.stereotype.Component
import software.amazon.awssdk.services.ses.SesClient
import software.amazon.awssdk.services.ses.model.Body
import software.amazon.awssdk.services.ses.model.Content
import software.amazon.awssdk.services.ses.model.Destination
import software.amazon.awssdk.services.ses.model.Message
import software.amazon.awssdk.services.ses.model.SendEmailRequest
@Component
class EmailClient(
val awsEmailClient: SesClient
) {
fun send(email: Email): Either<Throwable, String> {
val destination = Destination.builder()
.toAddresses(email.to)
.build()
val content = Content.builder()
.data(email.body)
.build()
val sub = Content.builder()
.data(email.subject)
.build()
val body = Body.builder()
.html(content)
.build()
val msg = Message.builder()
.subject(sub)
.body(body)
.build()
val emailRequest = SendEmailRequest.builder()
.destination(destination)
.message(msg)
.source(email.from)
.build()
return Either.catch {
awsEmailClient.sendEmail(emailRequest).messageId()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment