Sun's Blog

Apache Common MailSender 본문

ETC

Apache Common MailSender

버스는그만 2023. 8. 6. 18:23

의존성 추가

		<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
		<dependency>
		    <groupId>org.apache.commons</groupId>
		    <artifactId>commons-email</artifactId>
		    <version>1.5</version>
		</dependency>

간단한 코드

SimpleEmail email = new SimpleEmail();
		  // setHostName에 실제 메일서버정보
	 
		  email.setCharset("utf-8"); // 한글 인코딩  
		  email.setSmtpPort(587);
		  email.setHostName("smtp.naver.com"); //SMTP서버 설정
		  email.setStartTLSEnabled(true);
		  try {
			email.addTo("수신자@naver.com", "받는 사람"); // 수신자 추가
		} catch (EmailException e) {
			e.printStackTrace();
		}
		  try {
			email.setFrom("송신자@naver.com", "보내는 사람"); // 보내는 사람
			email.setAuthenticator(new DefaultAuthenticator("아이디", "비밀번호"));
		} catch (EmailException e) {
			e.printStackTrace();
		}
		  email.setSubject("Test message"); // 메일 제목
		  email.setContent("simple 메일 Test입니다", "text/plain; charset=utf-8");
		  try {
			email.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}

 

겪었던 오류

  • setSSL 메서드가 deprecated 되어서 잠시 했었음. 대체로 setStartTLSEnabled 메서드가 생김

 

 

공식 홈페이지

https://commons.apache.org/proper/commons-email/userguide.html

 

Commons Email – Examples

A simple text email Our first example will create a basic email message to "John Doe" and send it through your Google Mail (GMail) account. Email email = new SimpleEmail(); email.setHostName("smtp.googlemail.com"); email.setSmtpPort(465); email.setAuthenti

commons.apache.org