Skip to content

Commit

Permalink
fix: 메일 전송 객체 매번 생성,, #3
Browse files Browse the repository at this point in the history
  • Loading branch information
623nana committed Aug 27, 2021
1 parent 7df739c commit 924c463
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.nexters.covid.base.BaseResponse;
import com.nexters.covid.letter.service.MailService;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.ExecutionException;
import javax.mail.MessagingException;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -18,7 +17,7 @@ public class MailController {

@GetMapping("/mail/{sendOptionId}")
public BaseResponse<?> sendLetters(@PathVariable("sendOptionId") Long sendOptionId)
throws MessagingException, ExecutionException, InterruptedException {
throws MessagingException, UnsupportedEncodingException {
mailService.sendLetters(sendOptionId);
return new BaseResponse<>(200, 0, "", null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.CompletableFuture;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.mail.javamail.JavaMailSender;
Expand All @@ -19,17 +20,20 @@
@RequiredArgsConstructor
public class MailAsyncService {

private final JavaMailSender javaMailSender;
private final LetterRepository letterRepository;

@Async("threadPoolTaskExecutor")
@Transactional
public CompletableFuture<Boolean> sendLetter(JavaMailSender javaMailSender, MimeMessage msg,
MimeMessageHelper msgHelper, Letter letter, LetterText text) {
public CompletableFuture<Boolean> sendLetter(Letter letter, LetterText text)
throws MessagingException {

MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper msgHelper = new MimeMessageHelper(msg, true, "UTF-8");
try {
msgHelper.setFrom(letter.getEmail(), "안녕, 나야");
msgHelper.setFrom("[email protected]", "안녕, 나야");
msgHelper.setTo(letter.getLetterTo());
msgHelper.setSubject(generateLetterTitle(letter.getCreatedDate()));
msgHelper.setText(generateLetterTitle(letter.getCreatedDate()));
msgHelper.setText(text.generateLetterBody(letter), true);

javaMailSender.send(msg);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package com.nexters.covid.letter.service;

import com.nexters.covid.base.Constant;
import com.nexters.covid.base.LetterText;
import com.nexters.covid.letter.domain.Letter;
import com.nexters.covid.letter.domain.LetterRepository;
import com.nexters.covid.letter.domain.State;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class MailService {

private final JavaMailSender javaMailSender;
private final LetterRepository letterRepository;
private final MailAsyncService mailAsyncService;

Expand All @@ -39,12 +34,10 @@ public List<Letter> findLettersByOptionIdAndState(Long sendOptionId) {
public void sendLetters(Long sendOptionId)
throws MessagingException {
List<Letter> letters = findLettersByOptionIdAndState(sendOptionId);
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper msgHelper = new MimeMessageHelper(msg, true, "UTF-8");
LetterText text = new LetterText(letterUrl, stickerUrl);

for (Letter letter : letters) {
mailAsyncService.sendLetter(javaMailSender, msg, msgHelper, letter, text);
mailAsyncService.sendLetter(letter, text);
}
}
}

0 comments on commit 924c463

Please sign in to comment.