AWS SES 스팸함 전송

coldrice99·2025년 5월 23일
0
post-thumbnail

문제 상황: 메일은 가지만 스팸함에 간다?

초기에는 간단하게 Spring Boot에서 구글 SMTP를 사용하여 메일을 보내고 있었다.
메일 전송은 성공적으로 되었지만, 중요한 문제가 있었다.

❌ 메일이 전부 스팸함으로 분류됨

운영 서비스에서는 치명적인 문제라 판단했고,
도메인 기반 인증이 가능한 AWS SES(Simple Email Service) 로 전환하기로 결정했다.


🔧 1차 삽질: MailConfig 수정 누락

application.yml과 IAM 사용자 설정 등 SES 전환을 잘 마쳤다고 생각했는데, 메일 전송 시 아래 에러가 발생했다:

jakarta.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
        java.io.IOException: Server is not trusted: email-smtp.ap-northeast-2.amazonaws.com

한참 TLS 인증 문제로 삽질했지만, 원인은 어이없게도...

// 기존 설정 (구글 SMTP)
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

// → AWS SES 변경 시 이 부분도 바꿔줘야 한다!
props.put("mail.smtp.ssl.trust", "email-smtp.ap-northeast-2.amazonaws.com");

📌 MailConfig에서 호스트 변경은 했지만 ssl.trust는 안 바꿔서 TLS 오류가 발생했던 것...


🔐 2차 삽질: 535 Authentication Credentials Invalid

TLS 에러를 해결하고 나니 이번엔 인증 오류가 떴다:

jakarta.mail.AuthenticationFailedException: 535 Authentication Credentials Invalid

그래서 AWS 공식 문서를 보고
Secret Access Key → SMTP 비밀번호로 변환하는 Python 스크립트를 사용했다.

#!/usr/bin/env python3

import hmac
import hashlib
import base64
import argparse

SMTP_REGIONS = [
    "us-east-2",  # US East (Ohio)
    "us-east-1",  # US East (N. Virginia)
    "us-west-2",  # US West (Oregon)
    "ap-south-1",  # Asia Pacific (Mumbai)
    "ap-northeast-2",  # Asia Pacific (Seoul)
    "ap-southeast-1",  # Asia Pacific (Singapore)
    "ap-southeast-2",  # Asia Pacific (Sydney)
    "ap-northeast-1",  # Asia Pacific (Tokyo)
    "ca-central-1",  # Canada (Central)
    "eu-central-1",  # Europe (Frankfurt)
    "eu-west-1",  # Europe (Ireland)
    "eu-west-2",  # Europe (London)
    "eu-south-1",  # Europe (Milan)
    "eu-north-1",  # Europe (Stockholm)
    "sa-east-1",  # South America (Sao Paulo)
    "us-gov-west-1",  # AWS GovCloud (US)
    "us-gov-east-1",  # AWS GovCloud (US)
]

# These values are required to calculate the signature. Do not change them.
DATE = "11111111"
SERVICE = "ses"
MESSAGE = "SendRawEmail"
TERMINAL = "aws4_request"
VERSION = 0x04


def sign(key, msg):
    return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()


def calculate_key(secret_access_key, region):
    if region not in SMTP_REGIONS:
        raise ValueError(f"The {region} Region doesn't have an SMTP endpoint.")

    signature = sign(("AWS4" + secret_access_key).encode("utf-8"), DATE)
    signature = sign(signature, region)
    signature = sign(signature, SERVICE)
    signature = sign(signature, TERMINAL)
    signature = sign(signature, MESSAGE)
    signature_and_version = bytes([VERSION]) + signature
    smtp_password = base64.b64encode(signature_and_version)
    return smtp_password.decode("utf-8")


def main():
    parser = argparse.ArgumentParser(
        description="Convert a Secret Access Key to an SMTP password."
    )
    parser.add_argument("secret", help="The Secret Access Key to convert.")
    parser.add_argument(
        "region",
        help="The AWS Region where the SMTP password will be used.",
        choices=SMTP_REGIONS,
    )
    args = parser.parse_args()
    print(calculate_key(args.secret, args.region))


if __name__ == "__main__":
    main()

하지만...

❌ 여전히 535 에러 발생

멘탈 붕괴 후… 그냥 CSV 파일에서 받은 Secret Access Key를 그대로 넣어봤는데…

✅ 성공했다.

즉, 변환할 필요 없음!!

SMTP 전용 자격 증명을 발급하면 그 자체가 비밀번호로 바로 사용 가능하다.


✅ 마무리: SPF 설정도 필수

메일이 받은편지함에 잘 도착하기 위해서는 도메인에 아래 TXT 레코드를 추가해야 한다:

TXT    @    "v=spf1 include:amazonses.com ~all"


💭 회고

🧠 "문서가 항상 맞는 건 아니다. 직접 해보는 것이 답이다."

Spring Boot에서 AWS SES를 설정할 땐 단순히 username/password만 입력한다고 끝이 아니라,
JavaMail 설정, 도메인 인증, 자격 증명 발급 방식까지 모두 정확히 이해하고 설정해야 한다.

profile
서두르지 않으나 쉬지 않고

0개의 댓글