메뉴 건너뛰기

조회 수 256 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

0. 들어가기 전에 

 

어떤 작업을 시행 한 후, 결과나 로그를 메일로 보내야 할 경우가 있습니다. 요즘에는 가입 확인 메일을 보낼 때나 인증을 처리할 떄도 자주 사용합니다. 이런 경우 SMTP를 통해서 메일을 보낼 수 있습니다. 여기에서는 Gmail을 이용하여 짧게 기능을 구현해 보겠습니다.

 

maven dependency

 

com.sun.mail

javax.mail

1.5.2

jar 파일도 첨부합니다. 

 

 

1. 전송 부분 구현

 public class MailUtil {

public static void sendMail(Session session, String toMail, String subject, String content){

try {

MimeMessage msg = new MimeMessage(session);

msg.addHeader("Content-type", "text/HTML charset=UTF-8");

msg.addHeader("format", "flowed");

msg.addHeader("Content-Transfer-Encoding", "8bit");

msg.setFrom(new InternetAddress("no_reply@gmail.com", "NoReply")); //보내는 사람 이름

msg.setSubject(subject, "UTF-8");

msg.setText(content, "UTF-8");

msg.setSentDate(new Date());

msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toMail));

System.out.println("send email");

Transport.send(msg);

System.out.println("suceess");

} catch (Exception e) {

System.out.println("error");

e.printStackTrace();

}

}

 

MailUtil이라는 클래스를 생성하여 sendMail 이라는 static 메소드를 만들었습니다. 

session, 받는 사람 메일 주소, 메일 제목, 메일 내용을 파라미터로 받게 해놓았습니다.

 

 

2. 실행부 구현

 public class Main {

 

public static void main(String[] args) {

final String toMailID = "받을 사람의 메일 주소";

final String fromMailID = "보내는 사람 계정의 메일 주소(GMail 계정으로)";

final String password = "보내는 사람 계정의 비밀 번호";

Properties props = System.getProperties();

props.put("mail.smtp.host", "smtp.gmail.com");

props.put("mail.smtp.port", "587");

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.starttls", "true");

Authenticator auth = new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication(){

return new PasswordAuthentication(fromMailID, password);

}

};

Session session = Session.getInstance(props, auth);

MailUtil.sendMail(session, toMailID, "testMail", "test contents");

}

 

}

 

GMail을 이용하므로, 보낼 사람의 계정 아이디와 비밀번호를 입력해주어야 합니다. 

GMail 말고 다른 회사의 SMTP를 사용하시려면, 그 회사가 지원해주는 host와 port를 조사하셔서 mail.smtp.host와 mail.smtp.port에 적어주시면 됩니다. 

 

그외의 방법으로 컴퓨터 자체에 메일 서버를 설치하시면, 인증 절차를 거치지 않고 보낼 수 있습니다. 

 


List of Articles
번호 제목 날짜 조회 수
31 log4j에서 로그가 출력되지 않는 문제 수정 2021.03.25 405
30 java에서 이전 URL 알아내기 2021.03.25 691
29 Reflection을 활용한 메서드, 필드 값 불러오기. 2021.03.31 124
28 jstl <c:url value=""> 사용시 ;jsessionid= 붙는 현상 file 2021.03.31 230
27 자바 - 공백 문자 제거하기 (trim, replaceAll) file 2021.03.31 173
26 자바 String Class 문자열 처리 함수에 대한 정리 2021.03.31 106
25 TCP 소켓 프로그래밍 01 - Server/Client 일대일 연결 file 2021.03.31 119
24 쓰레드 (Thread) 사용하기 file 2021.03.31 104
23 [객체 지향 언어의 이해] 업캐스팅과 다운캐스팅 file 2021.03.31 157
22 HashMap 사용하기 file 2021.03.31 134
21 자바 대소문자 확인하는 방법 file 2023.02.15 131
20 자바 int 값 자리수 구하기 file 2023.02.15 74
19 자바 배열 복사하는 방법 file 2023.02.15 63
18 자바에서 문자열 비교 시 == 가 아닌 equals를 써야하는 이유 file 2023.02.15 114
17 자바 메소드(Method)란 무엇인가? file 2023.02.15 78
16 자바 Statement PreparedStatement 차이 알아보기 file 2023.02.15 53
15 자바 오버라이드, 오버로드 차이 알아보기 file 2023.02.15 70
14 자바 this, super 차이 알아보기 file 2023.02.15 72
13 자바 객체화(인스턴스화) 알아보기 file 2023.02.15 69
12 자바 클래스, 객체, 인스턴스 구분하기 file 2023.02.15 52
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved