[Sampling] 2. Resend Email Verification Sampling

김미숙's avatar
Aug 06, 2025
[Sampling] 2. Resend Email Verification Sampling

1. Resend 회원가입

notion image

2. API 키 발급 + 복사

notion image
→ key는 환경변수 등록 RESEND_API_KEY
 

3. 프로젝트 생성

의존성 추가

implementation 'com.squareup.okhttp3:okhttp:4.12.0' implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.0'

CodeUtil

package com.metacoding; import java.security.SecureRandom; public class CodeUtil { private static final SecureRandom random = new SecureRandom(); private static final int CODE_LENGTH = 6; /** * 6자리 숫자 인증번호 생성 (ex. "583241") */ public static String generateAuthCode() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < CODE_LENGTH; i++) { sb.append(random.nextInt(10)); // 0~9 } return sb.toString(); } }

EmailService

package com.metacoding; import com.fasterxml.jackson.databind.ObjectMapper; import okhttp3.*; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class EmailService { private final String API_KEY = System.getenv("RESEND_API_KEY"); // ← Resend API Key private final OkHttpClient client = new OkHttpClient(); private final ObjectMapper objectMapper = new ObjectMapper(); public void sendVerificationEmail(String toEmail, String authCode) throws IOException { String url = "https://api.resend.com/emails"; // ✅ 이메일 내용 구성 Map<String, Object> payload = new HashMap<>(); payload.put("from", "인증센터 <onboarding@resend.dev>"); payload.put("to", toEmail); payload.put("subject", "이메일 인증번호 안내"); // ← subject 추가 필수 payload.put("html", "<p>당신의 인증번호는 <strong>" + authCode + "</strong> 입니다.</p>"); // ✅ JSON 변환 String json = objectMapper.writeValueAsString(payload); RequestBody body = RequestBody.create(json, MediaType.get("application/json")); // ✅ 요청 생성 Request request = new Request.Builder() .url(url) .header("Authorization", "Bearer " + API_KEY) .post(body) .build(); // ✅ 요청 실행 및 응답 처리 try (Response response = client.newCall(request).execute()) { System.out.println("응답 코드: " + response.code()); if (!response.isSuccessful()) { String errorBody = response.body() != null ? response.body().string() : "No response body"; throw new IOException("이메일 전송 실패: " + errorBody); } System.out.println("✅ 이메일 전송 성공!"); } } }

Main

package com.metacoding; //TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or // click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter. public class Main { public static void main(String[] args) throws Exception { try { String email = "nuit0535@gmail.com"; // 🔹 인증번호 생성 String authCode = CodeUtil.generateAuthCode(); System.out.println("생성된 인증번호: " + authCode); // 🔹 이메일 전송 EmailService service = new EmailService(); service.sendVerificationEmail(email, authCode); } catch (Exception e) { e.printStackTrace(); // 어떤 예외가 나는지 확인 } } }
notion image
notion image
Share article

parangdajavous