Last active
November 19, 2020 11:17
-
-
Save ShigeoTejima/f2997f57405010c3caca to your computer and use it in GitHub Desktop.
send mail to AWS SES using spring boot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cloud.aws.credentials.accessKey=# write accessKey | |
cloud.aws.credentials.secretKey=# write secretKey |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- add --> | |
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-starter-aws</artifactId> | |
</dependency> |
@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
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