我尝试使用 SES V2 AMAZON 发送电子邮件,但失败并出现以下错误
我有兴趣使用 WebIdentityTokenCredentialsProvider(): 这部分错误说: “要使用 Web 身份令牌,‘sts’服务模块必须位于类路径上。”
我该如何解决:
使用此代码:
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代码:
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;
}
}
堆栈跟踪:
无法发送邮件并出现错误,无法从链中的任何提供程序加载凭证 AwsCredentialsProviderChain(credentialsProviders=[SystemPropertyCredentialsProvider(), EnvironmentVariableCredentialsProvider(), WebIdentityTokenCredentialsProvider(), ProfileCredentialsProvider(profileName=default, profileFile=ProfileFile(profilesAndSectionsMap=[ ])), ContainerCredentialsProvider(), InstanceProfileCredentialsProvider()]) : [SystemPropertyCredentialsProvider(): 无法从系统设置加载凭据。 必须通过环境变量 (AWS_ACCESS_KEY_ID) 或系统属性 (aws.accessKeyId) 指定访问密钥。,
EnvironmentVariableCredentialsProvider():无法从系统设置加载凭据。必须通过环境变量 (AWS_ACCESS_KEY_ID) 或系统属性 (aws.accessKeyId) 指定访问密钥。WebIdentityTokenCredentialsProvider():要使用 Web 身份令牌, “sts”服务模块必须位于类路径上。, ProfileCredentialsProvider(profileName=default, profileFile=ProfileFile(profilesAndSectionsMap=[])):配置文件不包含配置文件“默认”的凭证:ProfileFile(profilesAndSectionsMap=[])、ContainerCredentialsProvider():无法从容器获取凭证 - 未设置 AWS_CONTAINER_CREDENTIALS_FULL_URI 或 AWS_CONTAINER_CREDENTIALS_RELATIVE_URI 环境变量。 InstanceProfileCredentialsProvider():无法从 IMDS 加载凭据。]
我尝试添加此解决方案
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sts</artifactId>
<version>2.17.288</version>
<scope>test</scope>
</dependency>
如此处提供的,但它没有解决我的错误 https://github.com/gkatzioura/CloudStorageMaven/issues/23
而不是
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sts</artifactId>
<version>2.17.288</version>
<scope>test</scope>
</dependency>
我做到了
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sts</artifactId>
<version>2.17.288</version>
</dependency>
解决了错误
我正在维护类似的代码和依赖关系。对我来说这不起作用。
我收到以下错误,它没有向我提供正确的异常详细信息。
software.amazon.awssdk.core.internal.http.CombinedResponseHandler.handleErrorResponse(CombinedResponseHandler.java:125)
有人可以帮忙吗。