Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 让Spring Boot应用程序通过AWS SES发送简单电子邮件需要哪些配置步骤?_Spring Boot_Amazon Ses_Aws Java Sdk_Spring Cloud Aws - Fatal编程技术网

Spring boot 让Spring Boot应用程序通过AWS SES发送简单电子邮件需要哪些配置步骤?

Spring boot 让Spring Boot应用程序通过AWS SES发送简单电子邮件需要哪些配置步骤?,spring-boot,amazon-ses,aws-java-sdk,spring-cloud-aws,Spring Boot,Amazon Ses,Aws Java Sdk,Spring Cloud Aws,我今天已经为此奋斗了好几个小时了。我从文档开始,在文档中并没有对具体步骤说太多。它只是说开发人员可以包含一个Bean XML,然后自动连接邮件发送器。我已经试过了,也试过了很多变体,但没有能够使用SpringCloudAWS让它工作。我最终求助于直接包括aws java sdk ses并手动配置该类 下面是一个简单的项目,演示了我的尝试: 此项目可以编译,但当我运行它时,我得到: Parameter 0 of constructor in com.example.awssestest.AwsS

我今天已经为此奋斗了好几个小时了。我从文档开始,在文档中并没有对具体步骤说太多。它只是说开发人员可以包含一个Bean XML,然后自动连接邮件发送器。我已经试过了,也试过了很多变体,但没有能够使用SpringCloudAWS让它工作。我最终求助于直接包括aws java sdk ses并手动配置该类

下面是一个简单的项目,演示了我的尝试:

此项目可以编译,但当我运行它时,我得到:

Parameter 0 of constructor in com.example.awssestest.AwsSesTestApplication required a bean of type 'org.springframework.mail.MailSender' that could not be found.
- Bean method 'mailSender' not loaded because @ConditionalOnClass did not find required class 'javax.mail.internet.MimeMessage'
- Bean method 'simpleMailSender' not loaded because @ConditionalOnClass did not find required class 'com.amazonaws.services.simpleemail.AmazonSimpleEmailService'
- Bean method 'javaMailSender' not loaded because @ConditionalOnClass did not find required class 'com.amazonaws.services.simpleemail.AmazonSimpleEmailService'
如果我尝试添加javax邮件,则错误会更改为:

Parameter 0 of constructor in com.example.awssestest.AwsSesTestApplication required a bean of type 'org.springframework.mail.MailSender' that could not be found.
- Bean method 'mailSender' not loaded because AnyNestedCondition 0 matched 2 did not; NestedCondition on MailSenderAutoConfiguration.MailSenderCondition.JndiNameProperty @ConditionalOnProperty (spring.mail.jndi-name) did not find property 'jndi-name'; NestedCondition on MailSenderAutoConfiguration.MailSenderCondition.HostProperty @ConditionalOnProperty (spring.mail.host) did not find property 'host'
- Bean method 'simpleMailSender' not loaded because @ConditionalOnClass did not find required class 'com.amazonaws.services.simpleemail.AmazonSimpleEmailService'
- Bean method 'javaMailSender' not loaded because @ConditionalOnClass did not find required class 'com.amazonaws.services.simpleemail.AmazonSimpleEmailService'
相反,如果我尝试显式添加对aws java sdk ses的依赖,则会出现以下错误:

Parameter 0 of constructor in com.example.awssestest.AwsSesTestApplication required a bean of type 'org.springframework.mail.MailSender' that could not be found.
- Bean method 'mailSender' not loaded because @ConditionalOnClass did not find required class 'javax.mail.internet.MimeMessage'
- Bean method 'javaMailSender' in 'MailSenderAutoConfiguration' not loaded because @ConditionalOnClass did not find required class 'javax.mail.Session'
- Bean method 'simpleMailSender' in 'MailSenderAutoConfiguration' not loaded because @ConditionalOnMissingClass found unwanted class 'org.springframework.cloud.aws.mail.simplemail.SimpleEmailServiceJavaMailSender'
对于此错误,我尝试将@QualifierSimpleEmailSender注释添加到@Autowired,但没有帮助


我希望有人能把我引向正确的方向。

不久前,我在Spring Boot web项目中使用了AWS SES,但我还没有使用Spring Cloud AWS将我的应用程序与邮件服务集成

相反,我只是将spring boot starter mail包含在项目依赖项pom.xml中:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
注意:服务器主机和端口可能会有所不同

SpringBoot将创建一个默认的JavaMailSender实例,并使用previos参数对其进行配置。您可以使用此对象发送电子邮件

这可能不是AWS SES与Spring Boot应用程序集成的最佳方法,但对我来说效果很好

将AWS Java sdk添加到项目中 在计算机上设置AWS凭据密钥 创建SendEmailRequest SendEmailRequest.SendEmailRequest
p、 在美国,您不需要任何JavaMail。您可以在Spring中以任何想要的方式将其包装到bean中。

您可以尝试以下步骤来解决您的问题。我从你那里尝试了这些变化,它对我有效

在pom.xml文件中添加依赖项com.amazonaws:aws java sdk ses。 创建一个自动配置类来配置邮件发送器bean。下面是一个例子。AWSCredentialsProvider由spring cloud starter aws开箱即用配置和提供。

三,。使用SpringAPI使用配置的邮件发送器发送邮件

希望能有帮助

编辑:

例如,如果要发送附件时需要使用JavaMailSender而不是MailSender,只需配置SimpleMailServiceJavaMailSender而不是SimpleMailServiceMailSender

像这样:

    @Bean
    public JavaMailSender mailSender(AmazonSimpleEmailService ses) {
        return new SimpleEmailServiceJavaMailSender(ses);
    }

您也可以通过Application.properties执行此操作。确保您不在AWS SES中的沙箱中

验证您通过SES发送电子邮件的电子邮件地址或域。转到SES仪表板并选择电子邮件地址。对于测试,您可以从SES仪表板->电子邮件地址->发送测试电子邮件发送演示电子邮件。 创建“访问密钥Id”和“机密访问密钥”。没有这些密钥,您无法发送邮件。您将得到535个错误。 3.现在将密钥放入application.properties中。如下图所示

`spring.mail.host=email-smtp.us-east-1.amazonaws.com(Your Hosting region)
spring.mail.username=<Access Key Id>
spring.mail.password=<Secret access key>`

希望有帮助。

如果有人使用aws的smtp接口而不是aws sdk api,只需在@davioooh answer中添加一些内容- 如果您使用的是端口465,则将协议用作smtps 但是,如果您使用的是端口25587,则使用协议作为smtp 这是强制性的

如果在使用端口465时未使用SMTPS,则springboot应用程序中将出现异常。因为在使用端口465时,客户端必须在传输邮件时启动TLS,因此必须在application.properties文件中提及SMTPS


类似地,使用端口25587时不需要使用SMTPS,因为在这种情况下,服务器首先响应客户端是否启用tls,然后客户端启动tls加密。因此,在端口25587的情况下,您只需使用SMTP。

我也考虑过这样做,但我想避免使用SMTP接口进行SES。不过,感谢您的输入,这很可能会帮助其他人。@deinspanjer为什么要避免使用SMTP?仅供参考…因为Amazon建议在可能的情况下使用SESAPI,主要是出于性能考虑。请看本文档开头的注释:看看这个要点:它可能很有用……是的,我确实看到了这一点,它帮助我开始了,但正如评论员所说,API自发布以来似乎发生了变化,而且它不再是开箱即用的。似乎您缺少在AWSSestStatApplication类上导入@ImportResource/aws-mail.xml。我刚刚尝试将该注释添加到应用程序类,但它不会更改错误消息。谢谢,我想我应该更清楚一点,我是专门为让spring cloud aws按照我的示例项目中所示的方式工作而寻求帮助的。您能帮助定义使用JavaMailSender而不是mailsender的mailsender bean吗?。我正在尝试用SES发送HTML电子邮件。但是M
ailSender只允许发送纯文本电子邮件。如何将此SES配置指定给JavaMailSender?什么是SimpleMailServiceMailSender?我找不到这个类。这个类来自图书馆。您可以参考forked repo中的pom.xml以了解依赖项需要什么样的spring引导版本?另外,如何获取邮件发送者依赖关系?您可以参考分叉存储库中的pom.xml。我想是1.5.x。
    @Bean
    public JavaMailSender mailSender(AmazonSimpleEmailService ses) {
        return new SimpleEmailServiceJavaMailSender(ses);
    }
`spring.mail.host=email-smtp.us-east-1.amazonaws.com(Your Hosting region)
spring.mail.username=<Access Key Id>
spring.mail.password=<Secret access key>`
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setFrom("yourmailID@yourDomain.com");