2017-09-15 624 views
5

今天我一直为此奋战了几个小时。我从http://cloud.spring.io/spring-cloud-aws/spring-cloud-aws.html#_sending_mails的文档开始,对于具体步骤并没有多少说明。它只是说开发人员可以包含一个Bean XML,然后自动装载MailSender。我已经尝试过以及许多变体,并且无法使用spring-cloud-aws进行工作。我终于采取直接包括aws-java-sdk-ses并手动配置类。通过AWS SES让Spring Boot应用程序发送简单的电子邮件需要什么配置步骤?

下面是一个简单的项目证明什么,我已经试过: https://github.com/deinspanjer/aws-ses-test

该项目编译,但是当我运行它,我得到:

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邮件(https://github.com/deinspanjer/aws-ses-test/tree/try-with-javax-mail-api ),则错误更改:

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' 

对于这个错误,我尝试添加一个@Qualifier("simpleMailSender")注释到@Autowired,但它并没有帮助。

我希望有人能够引导我走向正确的方向。

+0

看起来您缺少在AwsSesTestApplication类中导入@ImportResource(“/ aws-mail.xml”)。 – skadya

+0

我只是尝试将该注释添加到应用程序类,但它不会更改错误消息。 – deinspanjer

回答

2

您可以尝试以下步骤来解决您的问题。我在forked repo上尝试了这些更改,它适用于我。

  1. pom.xml文件中添加依赖项“com.amazonaws:aws-java-sdk-ses”。
  2. 创建一个自动配置类来配置邮件发送者bean。下面是例子。 AWSCredentialsProviderspring-cloud-starter-aws开箱即用配置和提供。

@Configuration 
public class SimpleMailAutoConfig { 

    @Bean 
    public AmazonSimpleEmailService amazonSimpleEmailService(AWSCredentialsProvider credentialsProvider) { 
     return AmazonSimpleEmailServiceClientBuilder.standard() 
       .withCredentials(credentialsProvider) 
       // Replace US_WEST_2 with the AWS Region you're using for 
       // Amazon SES. 
       .withRegion(Regions.US_WEST_2).build(); 
    } 

    @Bean 
    public MailSender mailSender(AmazonSimpleEmailService ses) { 
     return new SimpleEmailServiceMailSender(ses); 
    } 
} 

3.使用spring API使用配置的邮件发件人发送邮件。

希望它有帮助。

+0

你可以协助用'JavaMailSender'而不是'MailSender'来定义mailsender bean吗?我正在尝试使用SES发送HTML电子邮件。但是'MailSender'只允许纯文本电子邮件。如何将此SES配置指定为'JavaMailSender'? –

0

前段时间我在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> 

然后我设置SMTP服务器参数我application.properties。在我来说,我使用这些属性:

spring.mail.host=email-smtp.eu-west-1.amazonaws.com 
spring.mail.port=465 
spring.mail.protocol=smtps 
spring.mail.smtps.auth=true 
spring.mail.smtp.ssl.enable=true 
spring.mail.username=<my-user> 
spring.mail.password=<my-password> 

注意:服务器主机和端口可能会有所不同。

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

也许这不是AWS SES与Spring Boot应用程序集成的最佳方法,但它对我来说工作正常。

+0

我曾想过这样做,但我想避免使用SES的smtp接口。感谢您的意见,但很可能它可能会帮助其他人。 – deinspanjer

+0

@deinspanjer你为什么要避免使用SMTP? (仅供参考...) – davioooh

+0

由于Amazon建议在可能的情况下使用SES API,主要是出于性能方面的考虑。请参阅本文档开头的注释:http://cloud.spring.io/spring-cloud-static/spring-cloud-aws/1.2.1.RELEASE/#_sending_mails – deinspanjer

0
  1. AWS-Java的SDK添加到您的项目
  2. 设置AWS证书(密钥),你的机器上
  3. 创建SendEmailRequest
  4. SendEmailRequest#.sendEmail(请求)

附:这里不需要任何JavaMail。你可以用任何想要的方式将它包装到Spring中。

+0

谢谢,我想我应该更清楚一点,我正在专门寻找帮助获得spring-cloud-aws的工作,正如我的示例项目中所示。 – deinspanjer

相关问题