Skip to content

Commit

Permalink
Merge pull request #135 from Soongsil-CoffeeChat/ref/#133
Browse files Browse the repository at this point in the history
[Feat] 메일 전송 요청시 SQS에 메시지 전달 로직 추가
  • Loading branch information
KimKyoHwee authored Aug 27, 2024
2 parents 76b35ba + 390af84 commit f907123
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
import io.awspring.cloud.sqs.operations.SqsTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sqs.SqsAsyncClient;

@Configuration
public class SqsConfig {
@Value("${spring.cloud.aws.region.static}")
@Value("${cloud.aws.region.static}")
private String region;

@Value("${spring.cloud.aws.credentials.access-key}")
@Value("${cloud.aws.credentials.access-key}")
private String accessKey;

@Value("${spring.cloud.aws.credentials.secret-key}")
@Value("${cloud.aws.credentials.secret-key}")
private String secretKey;
// 클라이언트 설정
@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
}

response.setStatus(HttpStatus.OK.value());
response.sendRedirect("http://localhost:8080/swagger-ui/index.html"); //서버 로컬 테스트용
//response.sendRedirect("https://localhost:3000/callback");
//response.sendRedirect("http://localhost:8080/swagger-ui/index.html"); //서버 로컬 테스트용
response.sendRedirect("https://localhost:3000/callback");
//response.sendRedirect("https://coffeego-ssu.web.app/callback");
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/soongsil/CoffeeChat/util/email/EmailMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.soongsil.CoffeeChat.util.email;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class EmailMessage {
private String toEmail;
private String subject;
private String htmlContent;
}
41 changes: 40 additions & 1 deletion src/main/java/com/soongsil/CoffeeChat/util/email/EmailUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import java.time.LocalTime;
import java.util.concurrent.CompletableFuture;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.awspring.cloud.sqs.operations.SqsTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
Expand All @@ -21,6 +24,40 @@ public class EmailUtil {

private final JavaMailSender javaMailSender;
private final ApplicationContext applicationContext;
private final SqsTemplate sqsTemplate;
private final ObjectMapper objectMapper;
@Value("${cloud.aws.sqs.queue-name}")
private String queueName;

// 인증 이메일 발송 메서드
public String sendAuthenticationEmail(String receiver) throws InterruptedException {
String code = String.valueOf((int) ((Math.random() * 900000) + 100000));

// 이메일 메시지를 JSON 형식으로 생성
EmailMessage emailMessage = new EmailMessage(receiver, "[COGO] 이메일 인증번호입니다.",
createAuthMessageTemplate("[COGO] 이메일 인증 안내", "이메일 인증을 완료하려면 아래의 인증 번호를 사용하여 계속 진행하세요:", code));

try {
// 메시지를 JSON으로 변환
String messageBody = objectMapper.writeValueAsString(emailMessage);

// SQS로 메시지 전송
sqsTemplate.send(queueName, messageBody);
System.out.println("메시지가 SQS로 전송되었습니다: " + messageBody);
} catch (Exception e) {
// 예외가 발생했을 때 로그를 남기고, 기본 코드 반환 등을 처리
System.out.println("SQS 메시지 전송 실패: " + e.getMessage());
}

return code;
}

private String createAuthMessageTemplate(String subject, String body, String code) {
return "<h1>" + subject + "</h1>" +
"<p>" + body + "</p>" +
"<h2>인증 번호: " + code + "</h2>";
}


// 비동기 메일 발송 메서드
@Async("mailExecutor")
Expand All @@ -31,7 +68,7 @@ public void sendMail(String receiver, String subject, String content) throws Mes
message.setText(content, "utf-8", "html");
javaMailSender.send(message);
}

/*
// 인증 이메일 발송 메서드
public String sendAuthenticationEmail(String receiver) throws MessagingException, InterruptedException {
String code = String.valueOf((int) ((Math.random() * 900000) + 100000));
Expand All @@ -49,6 +86,8 @@ public String sendAuthenticationEmail(String receiver) throws MessagingException
return code;
}
*/

public void sendApplicationMatchedEmail(String receiver, String mentorName, String menteeName, LocalDate date,
LocalTime startTime, LocalTime endTime) throws MessagingException {
sendMail(receiver, "[COGO] 매칭이 성사되었습니다.",
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ cloud:
stack:
auto: false
sqs:
queue-name: COGO_Email_SQS
queue-name: ${SQS_NAME}

logging.level:
org.hibernate.SQL: debug
Expand Down

0 comments on commit f907123

Please sign in to comment.