Skip to content

Commit

Permalink
feat: 즉시 메일 전송 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
nohy6630 committed May 8, 2024
1 parent 5d9e6d1 commit 7debba0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-mail'
}

dependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@
import com.yeongjin.alarmserver.domain.emailalarm.dto.request.ScheduledEmailReq;
import com.yeongjin.alarmserver.domain.emailalarm.entity.EmailAlarm;
import com.yeongjin.alarmserver.domain.emailalarm.repository.EmailAlarmRepository;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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
@Transactional(readOnly = true)
@Slf4j
public class EmailAlarmService {
private final EmailAlarmRepository emailAlarmRepository;
private final JavaMailSender javaMailSender;

@Transactional
public Long sendImmediateEmail(ImmediateEmailReq immediateEmailReq) {
Expand All @@ -23,6 +30,20 @@ public Long sendImmediateEmail(ImmediateEmailReq immediateEmailReq) {
immediateEmailReq.getContent()
)
);

MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, false, "UTF-8");
mimeMessageHelper.setTo(immediateEmailReq.getRecipients().toArray(new String[0]));
mimeMessageHelper.setSubject(immediateEmailReq.getSubject());
mimeMessageHelper.setText(immediateEmailReq.getContent(), false);
javaMailSender.send(mimeMessage);
log.info("Success");
} catch (MessagingException e) {
log.info("Fail");
throw new RuntimeException(e);
}

return emailAlarm.getId();
}

Expand Down

0 comments on commit 7debba0

Please sign in to comment.