[Sendgrid/intellij/JAVA] JAVA SMTP 발송 소스
다음 사이트를 참고하여 셋팅함
https://docs.microsoft.com/ko-kr/azure/store-sendgrid-java-how-to-send-email
SendGrid 메일 서비스를 사용하는 방법(Java)
Azure에서 SendGrid 메일 서비스를 사용하여 메일을 보내는 방법을 알아봅니다. 코드 샘플은 Java로 작성되었습니다.
docs.microsoft.com
처음엔 다음과 같이 다른 SMTP 제공 사이트과 동일하게 UserId, PWD를 이용해 시도해보았으나, 오류가 발생했습니다.
private static final String SMTP_AUTH_USER = "[Sandgrid 로그인 아이디]";
private static final String SMTP_AUTH_PWD = "[Sandgrid 로그인 비밀번호]";
<오류내용>
535 Authentication failed: Basic authentication is not allowed with 2FA enabled.
To fix, see
https://sendgrid.com/docs/for-developers/sending-email/authentication/#basic-authentication
해결방법
private static final String SMTP_AUTH_USER = "apikey"; //
private static final String SMTP_AUTH_PWD = "[SG. 로 시작하는 API_KEY]";
로 변경해주니 정상동작했습니다.
[Sendgrid] API KEY 발급
https://hea1peak.tistory.com/227
[Sendgrid] API_KEY 발급 + JAVA 메일 발송 401, 403 오류
Sendgrid 메일을 JAVA에서 적용하는 경우, 401 오류 : The provided authorization grant is invalid, expired, or revoked 오류는, {"errors":[{"message":"The provided authorization grant is invalid, expire..
hea1peak.tistory.com
private static final String SMTP_HOST_NAME = "smtp.sendgrid.net"; // 그냥 텍스트
private static final String SMTP_AUTH_USER = "apikey"; // 그냥 텍스트
// sandgrid 사이트에서 발급받은 API_KEY (https://app.sendgrid.com/settings/api_keys)
private static final String SMTP_AUTH_PWD = "[API_KEY]";
private static class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
public static boolean SendMail() throws Exception {
String fromMail = "[보내는사람Email(Sandgrid 에서 등록한, 검증된 이메일 정보 사용)]";
String fromName = "[보내는사람이름]";
String toMail = "[받는사람Email]";
String title = "제목";
String content = "<p>안녕,</p> <p><b>테스트</b>중이다</p><p>고마워,<br>존</br></p>";
// SMTP 정보 셋팅
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", SMTP_HOST_NAME);
properties.put("mail.smtp.port", 587);
properties.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(properties, auth);
try {
MimeMessage message = new MimeMessage(mailSession);
// 보낸사람 (Sandgrid 에서 등록한, 검증된 이메일 정보 사용)
InternetAddress from = new InternetAddress(fromMail, fromName, "UTF-8");
message.setFrom(from);
// 받는사람
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toMail));
// 제목
message.setSubject(title);
message.setSentDate(new java.util.Date());
Multipart multipart = new MimeMultipart("alternative");
// 내용(본문)
BodyPart part = new MimeBodyPart();
part.setContent(content, "text/html; charset=utf-8"); // 한글 깨지지 않도록 utf-8 추가
multipart.addBodyPart(part);
message.setContent(multipart);
// 발송
Transport transport = mailSession.getTransport();
transport.connect();
transport.sendMessage(message, message.getAllRecipients());
transport.close();
return true;
} catch (MessagingException ex){
System.out.println("메일 발송 에러 : " + ex);
}
return false;
}
https://sendgrid.com/docs/for-developers/sending-email/authentication/#basic-authentication
Authentication
Authenticating with the SendGrid API.
sendgrid.com