AWS的SES服务发送邮件并与Java对接

最近产品需要接入AWS邮件服务。
1、首先登陆改地址:
https://us-east-2.signin.aws.amazon.com/oauth?client_id=arn%3Aaws%3Asignin%3A%3A%3Aconsole%2Fcanvas&code_challenge=HsrfjfsCmxsR1IdmR6nc01ieKjAcd0Nbwm9W2zuUVGY&code_challenge_method=SHA-256&response_type=code&redirect_uri=https%3A%2F%2Fconsole.aws.amazon.com%2Fconsole%2Fhome%3FhashArgs%3D%2523%26isauthcode%3Dtrue%26oauthStart%3D1749606336744%26state%3DhashArgsFromTB_us-east-2_471b63a83f7041d1

2、输入Account ID、用户名和密码。
3、切换到你已经移出沙盒的地区。

注:是否已经移出沙盒可以看下这个页面,点击上图的最近访问的Amazon Simple Email Service,如果最近访问没有,则在左上角搜SES,然后看下图的位置,沙盒中的状态,会限制每秒发送邮件的次数和每天发送邮件的条数,移出沙盒则会没有这些限制。
下图中新加坡在沙盒中,雅加达没在沙盒中。


4、如何创建访问密码

5、点击创建访问密码(不知道是不是因为我刚点了创建访问密码的原因,现在没法点了)

6、一路点下去就可以把accessKey和secretKey创建好了。
7、java接口对接AWS的SDK
Java环境:SpringBoot2.3.4.RELEAS,JDK1.8
需要导入这三个包:

        <!-- AWS SDK for Java - SES -->
		<dependency>
			<groupId>com.amazonaws</groupId>
			<artifactId>aws-java-sdk-ses</artifactId>
			<version>1.12.470</version>
		</dependency>

		<!-- AWS SDK Core (如果上面的依赖没有自动包含) -->
		<dependency>
			<groupId>com.amazonaws</groupId>
			<artifactId>aws-java-sdk-core</artifactId>
			<version>1.12.470</version>
		</dependency>
		<dependency>
			<groupId>com.sun.mail</groupId>
			<artifactId>javax.mail</artifactId>
			<version>1.6.0</version>
		</dependency>

具体代码:
toAccountNumber是要发送的目的地邮箱地址,randomNumber是一个随机数,也可以写你要给toAccountNumber发送的内容。
withRegion(Regions.AP_SOUTHEAST_3)的内容是你在第三步沙盒移出的区域编号,比如我选择的是雅加达,它的编号就是ap-southeast-3。SUBJECT 是邮箱的主题。FROM你发送者的邮箱地址,需要在AWS做验证。

static final String SUBJECT = "Verification code";
static final String FROM = "你已经验证的邮箱";
public static boolean sendAmazonEmail(String toAccountNumber,String randomNumber){
        //第6步创建的accessKey和secretKey
        BasicAWSCredentials awsCreds =
                new BasicAWSCredentials( "你的accessKey","你的secretKey");
        try {
            AmazonSimpleEmailService client =
                    AmazonSimpleEmailServiceClientBuilder.standard()
                            .withRegion(Regions.AP_SOUTHEAST_3)
                            .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                            .build();
            SendEmailRequest request = new SendEmailRequest()
                    .withDestination(
                            new Destination().withToAddresses(toAccountNumber))
                    .withMessage(new Message()
                            .withBody(new Body()
                                    .withText(new Content()
                                            .withCharset("UTF-8").withData(getContent(randomNumber))))
                            .withSubject(new Content()
                                    .withCharset("UTF-8").withData(SUBJECT)))
                    .withSource(FROM)
                    ;
            client.sendEmail(request);
            System.out.println("Email sent!");
            return true;
        } catch (Exception ex) {
            System.out.println("The email was not sent. Error message: "
                    + ex.getMessage());
        }
        return false;
    }

以上就是AWS与Java接口的过程。

posted @ 2025-06-11 10:12  Charlie-Pang  阅读(118)  评论(0)    收藏  举报