-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
BE/#235 알림 발생 시 이메일 전송 기능 추가
- Loading branch information
Showing
11 changed files
with
504 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
52 changes: 52 additions & 0 deletions
52
backend/src/main/java/com/graphy/backend/domain/notification/service/MailingService.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.graphy.backend.domain.notification.service; | ||
|
||
import com.graphy.backend.domain.notification.domain.Notification; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.MimeMessageHelper; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
import org.thymeleaf.context.Context; | ||
import org.thymeleaf.spring5.SpringTemplateEngine; | ||
|
||
import javax.mail.MessagingException; | ||
import javax.mail.internet.MimeMessage; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class MailingService { | ||
|
||
private final JavaMailSender javaMailSender; | ||
private final SpringTemplateEngine templateEngine; | ||
|
||
private static final String EMAIL_TITLE_PREFIX = "[Graphy] "; | ||
|
||
@Async | ||
public void sendNotificationEmail(Notification notification) throws MessagingException { | ||
MimeMessage message = javaMailSender.createMimeMessage(); | ||
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8"); | ||
|
||
messageHelper.setSubject(EMAIL_TITLE_PREFIX + notification.getContent()); | ||
messageHelper.setTo(notification.getMember().getEmail()); | ||
|
||
HashMap<String, String> emailValues = new HashMap<>(); | ||
emailValues.put("content", notification.getContent()); | ||
String text = setContext(emailValues); | ||
|
||
messageHelper.setText(text, true); | ||
|
||
messageHelper.addInline("logo", new ClassPathResource("static/images/image-2.png")); | ||
messageHelper.addInline("notice-icon", new ClassPathResource("static/images/image-1.png")); | ||
|
||
javaMailSender.send(message); | ||
} | ||
|
||
private String setContext(Map<String, String> emailValues) { | ||
Context context = new Context(); | ||
emailValues.forEach(context::setVariable); | ||
return templateEngine.process("email/index", context); | ||
} | ||
} |
This file contains 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
This file contains 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
40 changes: 40 additions & 0 deletions
40
backend/src/main/java/com/graphy/backend/global/config/JavaMailConfig.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.graphy.backend.global.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.JavaMailSenderImpl; | ||
|
||
import java.util.Properties; | ||
|
||
@Configuration | ||
public class JavaMailConfig { | ||
|
||
@Value("${spring.mail.username}") | ||
private String username; | ||
@Value("${spring.mail.password}") | ||
private String password; | ||
@Value("${spring.mail.host}") | ||
private String host; | ||
|
||
@Bean | ||
public JavaMailSender javaMailService() { | ||
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); | ||
|
||
javaMailSender.setProtocol("smtp"); | ||
javaMailSender.setHost(host); | ||
javaMailSender.setUsername(username); | ||
javaMailSender.setPassword(password); | ||
javaMailSender.setJavaMailProperties(getMailProperties()); | ||
|
||
return javaMailSender; | ||
} | ||
|
||
private Properties getMailProperties() { | ||
Properties properties = new Properties(); | ||
properties.setProperty("mail.smtp.auth", "true"); | ||
properties.setProperty("mail.smtp.starttls.enable", "true"); | ||
return properties; | ||
} | ||
} |
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.