AWS SES邮件发送本地正常Tomcat部署失败问题及替代实现方案咨询
SimpleEmailServiceJavaMailSender for Amazon SES Email Delivery Absolutely! You don’t have to stick with SimpleEmailServiceJavaMailSender to send emails via Amazon SES—this class ties you to Spring Cloud AWS, which is causing the dependency conflict you’re seeing (the missing CacheFactory bean error comes from auto-configuration logic in Spring Cloud AWS that you don’t need for just SES). Here are two robust, dependency-light alternatives:
1. Use AWS SDK for Java v2 (Recommended)
AWS SDK v2 is the modern, maintained version of AWS’s Java library, and it works seamlessly without Spring Cloud AWS dependencies. It’s faster, more modular, and easier to configure.
Step 1: Add Dependency
For Maven, add this to your pom.xml:
<dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>ses</artifactId> <version>2.25.0</version> <!-- Use the latest stable version --> </dependency>
Step 2: Implement Email Sender
Create a service class to handle SES email sending:
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ses.SesClient; import software.amazon.awssdk.services.ses.model.*; import org.springframework.stereotype.Service; @Service public class SesEmailService { private final SesClient sesClient; public SesEmailService() { // Configure SES client with your region and default credentials (auto-picks up IAM role, env vars, or credentials file) this.sesClient = SesClient.builder() .region(Region.US_EAST_1) // Replace with your SES region .credentialsProvider(DefaultCredentialsProvider.create()) .build(); } public void sendEmail(String fromAddress, String toAddress, String subject, String body) { SendEmailRequest request = SendEmailRequest.builder() .source(fromAddress) .destination(Destination.builder().toAddresses(toAddress).build()) .message(Message.builder() .subject(Content.builder().data(subject).charset("UTF-8").build()) .body(Body.builder() .text(Content.builder().data(body).charset("UTF-8").build()) .build()) .build()) .build(); try { sesClient.sendEmail(request); System.out.println("Email sent successfully to " + toAddress); } catch (SesException e) { System.err.println(e.awsErrorDetails().errorMessage()); throw new RuntimeException("Failed to send email via SES", e); } } }
2. Use AWS SDK for Java v1 (Legacy)
If your project still uses the older AWS SDK v1, you can also use it directly without Spring Cloud AWS:
Step 1: Add Dependency
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-ses</artifactId> <version>1.12.500</version> <!-- Use latest compatible version --> </dependency>
Step 2: Implement Email Sender
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; import com.amazonaws.regions.Regions; import com.amazonaws.services.simpleemail.AmazonSimpleEmailService; import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder; import com.amazonaws.services.simpleemail.model.*; import org.springframework.stereotype.Service; @Service public class LegacySesEmailService { private final AmazonSimpleEmailService sesClient; public LegacySesEmailService() { this.sesClient = AmazonSimpleEmailServiceClientBuilder.standard() .withRegion(Regions.US_EAST_1) // Replace with your region .withCredentials(DefaultAWSCredentialsProviderChain.getInstance()) .build(); } public void sendEmail(String fromAddress, String toAddress, String subject, String body) { SendEmailRequest request = new SendEmailRequest() .withSource(fromAddress) .withDestination(new Destination().withToAddresses(toAddress)) .withMessage(new Message() .withSubject(new Content(subject)) .withBody(new Body().withText(new Content(body)))); try { sesClient.sendEmail(request); System.out.println("Email sent successfully"); } catch (AmazonSimpleEmailServiceException e) { System.err.println(e.getErrorMessage()); throw new RuntimeException("SES email failed", e); } } }
Key Notes to Avoid Issues
- IAM Permissions: Ensure the IAM entity (user/role) running your Tomcat server has the
ses:SendEmailorses:SendRawEmailpermission (depending on whether you send plain text or raw emails with attachments). - Credentials Configuration: The SDKs automatically pick up credentials from:
- IAM roles (if running on EC2/EKS/ECS)
- Environment variables (
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEY) - AWS credentials file (
~/.aws/credentials)
- Region: Make sure you use the same region where your SES identity (email/domain) is verified.
By using either of these direct SDK approaches, you’ll eliminate the Spring Cloud AWS dependency entirely, which should fix the NoSuchBeanDefinitionException you’re hitting during Tomcat deployment.
内容的提问来源于stack exchange,提问作者deetchiga soundherraj

