aws-doc-sdk-examples icon indicating copy to clipboard operation
aws-doc-sdk-examples copied to clipboard

Send Email via SES failing

Open yashh opened this issue 1 year ago • 0 comments

Expected behavior


// Copied from
// https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/kotlin/services/ses/src/main/kotlin/com/example/ses/SendMessageEmail.kt
import aws.sdk.kotlin.runtime.auth.credentials.EnvironmentCredentialsProvider
import aws.sdk.kotlin.services.ses.SesClient
import aws.sdk.kotlin.services.ses.model.Body
import aws.sdk.kotlin.services.ses.model.Content
import aws.sdk.kotlin.services.ses.model.Destination
import aws.sdk.kotlin.services.ses.model.Message
import aws.sdk.kotlin.services.ses.model.SendEmailRequest

val client = SesClient {
    region = "us-west-2"
    credentialsProvider = EnvironmentCredentialsProvider()
}

suspend fun send(sender: String?, recipient: String, subjectVal: String?, bodyHTML: String?) {
    val destinationOb = Destination { toAddresses = listOf(recipient) }
    val contentOb = Content { data = bodyHTML }
    val subOb = Content { data = subjectVal }
    val bodyOb = Body { html = contentOb }
    val msgOb = Message {
        subject = subOb
        body = bodyOb
    }

    val emailRequest = SendEmailRequest {
        destination = destinationOb
        message = msgOb
        source = sender
    }

    println(client)
    client.use { sesClient ->
        println("Attempting to send an email through Amazon SES")
        sesClient.sendEmail(emailRequest)
    }
}

I am calling this function through a coroutine

2024-01-26 15:38:55.457 [DefaultDispatcher-worker-1] TRACE a.s.k.r.a.c.EnvironmentCredentialsProvider - Attempting to load credentials from env vars AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY/AWS_SESSION_TOKEN
2024-01-26 15:38:55.837 [DefaultDispatcher-worker-1] DEBUG httpTraceMiddleware - HttpResponse: 400: Bad Request```

Is there a way to send email using just `AWS_ACCESS_KEY_ID & AWS_SECRET_ACCESS_KEY`  ? I want to use the long term credentials. Is it possible?

### Actual behavior

When I call this send function from a ktor handler it works. But when I call this function from a job worker which is a coroutine in my ktor app, this error is happening

### Steps to reproduce

```bash
1. Calling the code from a coroutine
2.
3.

Logs / stacktrace (if applicable)

No response

Which SDK were you using?

Kotlin

Which OS were you using?

macOS

SDK version

1.0.43

OS version

Sonoma 14.3

yashh avatar Jan 26 '24 23:01 yashh