티스토리 뷰
Java MailAPI (Gmail SMTP)
- Gmail SMTP 프로토콜을 사용하여 이메일을 보내는 방법에 대해서 알아보겠습니다.
- SMTP 프로토콜이란?
- 두 메일시스템이 이메일을 교환할 수 있게 하는 비교적 간단한 메시지 전송용 프로토콜
구현방법
javax.mail-api 라이브러리
- JavaMail API는 플랫폼, 프로토콜에 독립적인 프레임워크를 제공하여 메일, 메시징 응용프로그램을 빌드합니다.
- 빌드 방법에 맞게 적용해줍니다.
Gradle
compile group: 'javax.mail', name: 'javax.mail-api', version: '1.6.2'
Maven
<!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
JAR
Maven Repository: javax.mail » javax.mail-api » 1.6.2
javax.mail javax.mail-api 1.6.2 // https://mvnrepository.com/artifact/javax.mail/javax.mail-api compile group: 'javax.mail', name: 'javax.mail-api', version: '1.6.2' // https://mvnrepository.com/artifact/javax.mail/javax.mail-api libraryDependencies += "ja
mvnrepository.com
예제 코드
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* @author Crunchify.com
*
*/
public class CrunchifyJavaMailExample {
static Properties mailServerProperties;
static Session getMailSession;
static MimeMessage generateMailMessage;
public static void main(String args[]) throws AddressException, MessagingException {
generateAndSendEmail();
System.out.println("\n\n ===> Your Java Program has just sent an Email successfully. Check your email..");
}
public static void generateAndSendEmail() throws AddressException, MessagingException {
// Step1
System.out.println("\n 1st ===> setup Mail Server Properties..");
mailServerProperties = System.getProperties();
mailServerProperties.put("mail.smtp.port", "587");
mailServerProperties.put("mail.smtp.auth", "true");
mailServerProperties.put("mail.smtp.starttls.enable", "true");
System.out.println("Mail Server Properties have been setup successfully..");
// Step2
System.out.println("\n\n 2nd ===> get Mail Session..");
getMailSession = Session.getDefaultInstance(mailServerProperties, null);
generateMailMessage = new MimeMessage(getMailSession);
generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("test1@crunchify.com"));
generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress("test2@crunchify.com"));
generateMailMessage.setSubject("Greetings from Crunchify..");
String emailBody = "Test email by Crunchify.com JavaMail API example. " + "<br><br> Regards, <br>Crunchify Admin";
generateMailMessage.setContent(emailBody, "text/html");
System.out.println("Mail Session has been created successfully..");
// Step3
System.out.println("\n\n 3rd ===> Get Session and Send mail");
Transport transport = getMailSession.getTransport("smtp");
// Enter your correct gmail UserID and Password
// if you have 2FA enabled then provide App Specific Password
transport.connect("smtp.gmail.com", "<----- Your GMAIL ID ----->", "<----- Your GMAIL PASSWORD ----->");
transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
transport.close();
}
}
Trouble Shooting
- 실행을 하다보면 아래와 같은 에러가 발생할 때가 있습니다.
"AuthenticationFailedException: 535-5.7.8 Username and Password not accepted"
- 계정상 설정 문제로 아래 페이지를 참고하여 해결하도록 합니다.
(Java) Gmail SMTP 구현시 "AuthenticationFailedException: 535-5.7.8 Username and Password not accepted" 에러 해결법
들어가며 Gmail SMTP를 이용하여 Java Mail API를 통한 이메일 전송기능을 구현하는 중에 메일을 내보는데 실패하며 아래와 같은 에러 메시지가 발생하였다. Exception in thread "main" javax.mail.Authenticatio..
jinseongsoft.tistory.com
참고
Java MailAPI Example - Send an Email via GMail SMTP (TLS Authentication) • Crunchify
In this Java Tutorial we will see how to send an email using GMail SMTP protocol in Java. I'm using JavaMail API v1.6.2. It is very robust solution
crunchify.com
SMTP [정보통신기술용어해설]
www.ktword.co.kr
JavaMail
The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. The JavaMail API is available as an optional package for use with the Java SE platform and is also included in the Java EE platfor
javaee.github.io
관련글
(Java) 장비, 시스템 메모리(RAM) 값 알아내는 방법 (feat. OSHI 라이브러리 사용법)
들어가며 장비 하드웨어 상태를 모니터링하는 애플리케이션을 개발하다 보면 하드웨어 상태 정보를 알아내는 기능이 필요합니다. RAM, CPU, DISK, Network 등등 Java를 통해 OSHI (Operating System & Hardware In
jinseongsoft.tistory.com
(Java) 파일 FTP 프로토콜 전송 기능 구현방법 (FTP Client 구현)
FTP Client 구현방법 Java 파일 전송 시에 FTP 프로토콜로 FTP Server에 접속하여 파일을 전송하는 기능구현 방법을 공유하고자 한다. 구현방법 FTP Server에 접속하여 대상파일 저장하는 기능이다. public vo
jinseongsoft.tistory.com
(Java) BufferedImage 이미지 파일(PNG, JPEG, TIFF)로 출력하는 방법
Java 이미지 파일 출력 방법 메모리상의 BufferedImage 인스턴스를 외부로 이미지 파일 형태로 출력하는 방법에 대해서 공유하고자 한다. 구현방법 TIFF 출력방법 public void saveToImage(File toSave, String fo..
jinseongsoft.tistory.com
(Java) Image 파일(로컬, URL) 읽기 (Java File to Image)
들어가며 파일로 부터 Java의 BufferedImage 인스턴스로의 변환방법에 대해서 공유하고자 한다. 구현방법 ImageIO 클래스를 이용하면 간단하게 읽기가 가능하다. try { BufferedImage image; //로컬 파일을 사��
jinseongsoft.tistory.com
- Total
- Today
- Yesterday
- 일본 자전거 여행
- effectivejava
- git
- JavaFX 종료
- JavaFX Table View
- 자바
- 스프링부트
- 배낭여행
- JavaFX
- 방통대 과제물
- 일본 여행
- 이펙티브자바
- 자전거 여행
- 일본여행
- springboot
- effective java
- 배낭 여행
- 일본 배낭여행
- 이펙티브 자바
- 텐트
- windows
- intelij
- JavaFX Window Close
- JavaFX 테이블뷰
- 인텔리제이
- 자전거
- 이펙티브
- java
- TableView
- Java UI
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |