공지사항과 같이 여러명에게 이메일을 전송할 때,
메일 수신자가 받는 사람이 자신만을 표기하기 위한 로직.
SendEmailRequest emailRequest = new SendEmailRequest().withSource(from)
.withDestination(new Destination().withToAddresses(receivers))
.withMessage(message);
위와 같이 수신자들을(receivers) 한 번에 담아서 보내면 단점이 있다.
- 수신자들 중 한명이라도 이메일 형태가 잘못되면 모두 못 보낸다.(Error Code : InvalidParameterValue)
- 이메일 받는사람에 모든 수신자들이 표기되어 보안정책이 빡세면 개인정보 노출이 될 수 있다.
따라서 Credential을 먼저 생성한 후에, 사용자 List로 반복하여 발송하도록 처리하였다.
또한 비동기 처리를 하여 Timeout이 발생하지 않도록 하였다.
@Async
public void sendEmailToSeveral(List<User> users){
AmazonSimpleEmailServiceAsyncClient client = (AmazonSimpleEmailServiceAsyncClient) AmazonSimpleEmailServiceAsyncClient
.asyncBuilder()
.withCredentials(DefaultAWSCredentialsProviderChain.getInstance())
.withRegion("ap-northeast-2")
.build();
// 발신자, 제목, 내용 구성
String senderEmail = "no.reply@seunggil.yang";
String mailBody = "Hello, I'm seunggil yang. Bye";
Content subject = new Content().withData("Mail subject");
Content htmlBody = new Content().withData(mailBody);
Body body = new Body().withHtml(htmlBody);
Message message = new Message().withSubject(subject).withBody(body);
// 전송
users.stream().forEach(user -> {
Destination destination = new Destination().withToAddresses(user.getUserEmail());
SendEmailRequest request = new SendEmailRequest().withSource(senderEmail).withDestination(destination).withMessage(message);
client.sendEmail(request);
log.debug("Send Success. Receiver : {}", user.getUserEmail());
});
}
추가로 해결할 과제는
SES는 이메일 발신 로깅을 하지 않으므로 보낼때마다 Table Insert를 하던지,
CloudWatch등을 이용해 확인하는 방법을 알아야 된다.
'꺼내먹어요' 카테고리의 다른 글
bash grep, cut, >(Redirection) 활용 (0) | 2023.01.31 |
---|---|
Java List를 Map으로 변경 (0) | 2023.01.25 |
Collectors.groupingBy() - Java List 특정 기준으로 그룹 나누기 (0) | 2023.01.01 |
nginx 경로별 다른 API 주소 rewirte (0) | 2023.01.01 |
Linux 네트워크, 프로세스 상태 (0) | 2016.08.21 |