✉️ 이메일 전송 예제
- jakarta mail api 사용
implementation 'com.sun.mail:jakarta.mail:2.0.1' implementation 'com.sun.activation:jakarta.activation:2.0.1'
public class EmailSender { private final String host = "email-smtp.ap-northeast-2.amazonaws.com"; private final String user = "{acceess-key}"; private final String password = "{secret-key}"; public void send() { Session session = createProps(); String text = "Hello world!"; String subject = "Hello world!"; try { MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress("haerong22@gmail.com")); msg.setRecipient(Message.RecipientType.TO, new InternetAddress("haerong22@gmail.com")); msg.setSubject(subject, "utf-8"); msg.setContent(text, "text/html; charset=utf-8"); Transport transport = session.getTransport(); transport.connect(host, user, password); transport.sendMessage(msg, msg.getAllRecipients()); } catch (Exception e) { e.printStackTrace(); } } private Session createProps() { Properties props = System.getProperties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.port", 587); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.auth", "true"); return Session.getDefaultInstance(props); } }
smtp_credentials_generate.py
#!/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)
'sa-east-1', # South America (Sao Paulo)
'us-gov-west-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()
python path/to/smtp_credentials_generate.py {IAM계정 secret key} {region}