Hard Worker
Hard Worker

Reputation: 1111

Error when trying to send email with AMAZON SES using sts

I'm trying to send an email with SES V2 AMAZON , but it fails with the below error

I'm interested of using WebIdentityTokenCredentialsProvider(): and this part of the error says: "To use web identity tokens, the 'sts' service module must be on the class path.,"

how can I solve it:

using this code:

Maven: sesv2

    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>bom</artifactId>
        <version>2.17.288</version>
        <type>pom</type>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>sesv2</artifactId>
        <version>2.17.288</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-sts</artifactId>
    </dependency>

Java Code:

package com.company.core.service.util.email.sender;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.services.sesv2.SesV2Client;
import software.amazon.awssdk.services.sesv2.model.*;

@Service
public class CompanySesClient  {
    private static final Logger LOG = LoggerFactory.getLogger(CompanySesClient.class);

    private final SesV2Client client = SesV2Client.builder().build();

    @PostConstruct
    public void testSendMail(){
        try{
            LOG.info("about to try to send mail");
            sendEmail("[email protected]","[email protected]", "test subject", "test body");
            LOG.info("Sucess to send mail");
        }catch (Throwable error){
            LOG.error("failed to send mail with error,"+error.getMessage(), error.getMessage() , error.getStackTrace());
        }
    }

    public void sendEmail(String sender, String recipient, String emailSubject, String emailBody) {
        final Destination destination = Destination.builder()
                .toAddresses(recipient)
                .build();

        final EmailContent emailContent = getEmailContent(emailSubject, emailBody);
        final SendEmailRequest emailRequest = SendEmailRequest.builder()
                .destination(destination)
                .content(emailContent)
                .fromEmailAddress(sender)
                .build();
        try {
            LOG.info("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");
            client.sendEmail(emailRequest);
            LOG.info("email was sent");

        } catch (SesV2Exception e) {
            LOG.error("email sent error:" + e.awsErrorDetails().errorMessage());
            throw e;
        }
    }

    private static EmailContent getEmailContent(String emailSubject, String emailBody) {
        final Content subject = Content.builder()
                .data(emailSubject)
                .build();

        final Content content = Content.builder()
                .data(emailBody)
                .build();

        final Body body = Body.builder()
                .html(content)
                .build();

        final Message msg = Message.builder()
                .subject(subject)
                .body(body)
                .build();

        final EmailContent emailContent = EmailContent.builder()
                .simple(msg)
                .build();

        return emailContent;
    }
}

stack trace:

failed to send mail with error,Unable to load credentials from any of the providers in the chain AwsCredentialsProviderChain(credentialsProviders=[SystemPropertyCredentialsProvider(), EnvironmentVariableCredentialsProvider(), WebIdentityTokenCredentialsProvider(), ProfileCredentialsProvider(profileName=default, profileFile=ProfileFile(profilesAndSectionsMap=[])), ContainerCredentialsProvider(), InstanceProfileCredentialsProvider()]) : [SystemPropertyCredentialsProvider(): Unable to load credentials from system settings. Access key must be specified either via environment variable (AWS_ACCESS_KEY_ID) or system property (aws.accessKeyId).,

EnvironmentVariableCredentialsProvider(): Unable to load credentials from system settings. Access key must be specified either via environment variable (AWS_ACCESS_KEY_ID) or system property (aws.accessKeyId)., WebIdentityTokenCredentialsProvider(): To use web identity tokens, the 'sts' service module must be on the class path., ProfileCredentialsProvider(profileName=default, profileFile=ProfileFile(profilesAndSectionsMap=[])): Profile file contained no credentials for profile 'default': ProfileFile(profilesAndSectionsMap=[]), ContainerCredentialsProvider(): Cannot fetch credentials from container - neither AWS_CONTAINER_CREDENTIALS_FULL_URI or AWS_CONTAINER_CREDENTIALS_RELATIVE_URI environment variables are set., InstanceProfileCredentialsProvider(): Failed to load credentials from IMDS.]

I tried adding this solution

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>sts</artifactId>
    <version>2.17.288</version>
    <scope>test</scope>
</dependency>

as offered here, but it did not solve my error https://github.com/gkatzioura/CloudStorageMaven/issues/23

Upvotes: 1

Views: 1270

Answers (1)

Hard Worker
Hard Worker

Reputation: 1111

instead of

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>sts</artifactId>
    <version>2.17.288</version>
    <scope>test</scope>
</dependency>

i did

   <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>sts</artifactId>
        <version>2.17.288</version>
    </dependency>

Solved the error

Upvotes: 1

Related Questions