forked from moyeothon/momento_back
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request moyeothon#18 from kduoh99/Feat/moyeothon#17
Feat: 메일 전송 구현
- Loading branch information
Showing
6 changed files
with
139 additions
and
44 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
50 changes: 50 additions & 0 deletions
50
src/main/java/com/hackathon/momento/global/config/EmailConfig.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,50 @@ | ||
package com.hackathon.momento.global.config; | ||
|
||
import java.util.Properties; | ||
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; | ||
|
||
@Configuration | ||
public class EmailConfig { | ||
|
||
@Value("${spring.mail.host}") | ||
private String host; | ||
|
||
@Value("${spring.mail.port}") | ||
private int port; | ||
|
||
@Value("${spring.mail.username}") | ||
private String username; | ||
|
||
@Value("${spring.mail.password}") | ||
private String password; | ||
|
||
@Bean | ||
public JavaMailSender javaMailSender() { | ||
JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); | ||
mailSender.setPort(port); | ||
mailSender.setHost(host); | ||
mailSender.setUsername(username); | ||
mailSender.setPassword(password); | ||
mailSender.setDefaultEncoding("UTF-8"); | ||
mailSender.setJavaMailProperties(getMailProperties()); | ||
|
||
return mailSender; | ||
} | ||
|
||
private Properties getMailProperties() { | ||
Properties properties = new Properties(); | ||
properties.put("mail.smtp.auth", true); | ||
properties.put("mail.smtp.starttls.enable", true); | ||
properties.put("mail.smtp.starttls.required", true); | ||
properties.put("mail.smtp.connectiontimeout", 5000); | ||
properties.put("mail.smtp.timeout", 5000); | ||
properties.put("mail.smtp.writetimeout", 5000); | ||
|
||
return properties; | ||
} | ||
} | ||
|
44 changes: 0 additions & 44 deletions
44
src/main/java/com/hackathon/momento/notification/domain/Notification.java
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
src/main/java/com/hackathon/momento/team/application/EmailService.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,43 @@ | ||
package com.hackathon.momento.team.application; | ||
|
||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import java.io.UnsupportedEncodingException; | ||
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; | ||
|
||
@Slf4j | ||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class EmailService { | ||
|
||
private static final String FROM_EMAIL = "[email protected]"; | ||
private static final String TEAM_NAME = "TeamUp"; | ||
|
||
private final JavaMailSender mailSender; | ||
|
||
public void sendMessage(String to, String body) { | ||
try { | ||
MimeMessage message = mailSender.createMimeMessage(); | ||
|
||
String subject = "[TeamUp] 요청하신 팀 빌딩이 완료되었습니다, 팀 정보를 확인해주세요."; | ||
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true); | ||
|
||
mimeMessageHelper.setTo(to); | ||
mimeMessageHelper.setFrom(FROM_EMAIL, TEAM_NAME); | ||
mimeMessageHelper.setSubject(subject); | ||
mimeMessageHelper.setText(body, true); | ||
|
||
mailSender.send(message); | ||
log.info("Email sent to: {}", to); | ||
|
||
} catch (MessagingException | UnsupportedEncodingException e) { | ||
log.error("Email send failed to {}", to, e); | ||
} | ||
} | ||
} |
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