Skip to content

Instantly share code, notes, and snippets.

@ShigeoTejima
Last active November 19, 2020 11:17
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save ShigeoTejima/f2997f57405010c3caca to your computer and use it in GitHub Desktop.
Save ShigeoTejima/f2997f57405010c3caca to your computer and use it in GitHub Desktop.
send mail to AWS SES using spring boot
package org.test.demo;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Autowired
AmazonSimpleEmailService amazonSimpleEmailService(AmazonSimpleEmailService amazonSimpleEmailService) {
// if use ses region change, write below.
amazonSimpleEmailService.setRegion(Region.getRegion(Regions.US_EAST_1));
return amazonSimpleEmailService;
}
}
cloud.aws.credentials.accessKey=# write accessKey
cloud.aws.credentials.secretKey=# write secretKey
package org.test.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;
@Service
public class MailSenderService {
private final MailSender mailSender;
@Autowired
public MailSenderService(MailSender mailSender) {
this.mailSender = mailSender;
}
public void sendMessage() {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom("from@mailaddress");
simpleMailMessage.setTo("to@mailaddress");
simpleMailMessage.setSubject("subject");
simpleMailMessage.setText("text");
this.mailSender.send(simpleMailMessage);
}
}
<!-- add -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws</artifactId>
</dependency>
@matthewsommer
Copy link

I think this might be broken in the latest release of the AWS SDK. amazonSimpleEmailService is immutable now and the region can't be changed.

@hajdukd
Copy link

hajdukd commented Oct 13, 2017

For anyone wondering why "spring-cloud-starter-aws" dependency is not enough for this example to work:

Most of the required stuff from spring-cloud-aws-context is optional.. If you want to use that stuff (MailSender etc) within your project, you have to include optional dependencies in your project also.

For Newbies

@matthewsommer can be changed by static region

@RejiMohan
Copy link

@hajdukd Can I specify the keys and region within the configuration as follows? Also I'm trying to use JavaMailSender instead of MailSender. as I need to send HTML formatted emails.

  @Autowired
    AmazonSimpleEmailService amazonSimpleEmailService(AmazonSimpleEmailService amazonSimpleEmailService) {
  	
      amazonSimpleEmailService = AmazonSimpleEmailServiceClientBuilder.standard()
              .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(ACCESSKEY, SECRETKEY)))
              .withRegion(Regions.US_EAST_1).build();
  }

Also can I replace the artifact 'spring-cloud-starter-aws' with some minimal required dependencies to work with SES on springboot?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment