개인적인 정리

메일 테스트 java 본문

전자정부표준프레임워크

메일 테스트 java

yeon.Biju 2016. 12. 8. 15:56
http://commons.apache.org/proper/commons-email/userguide.html

//mail Test


import org.apache.commons.mail.*;



try {

SimpleEmail email = new SimpleEmail();

 // setHostName에 실제 메일서버정보

 

 email.setCharset("euc-kr"); // 한글 인코딩  

 

 email.setHostName("smtp.gmail.com"); //SMTP서버 설정

 email.setSmtpPort(465);

 email.setSSL(true);

 try {

email.addTo("aaa@aaaaaaaaaaaaaaa.com", "aaaaa"); // 수신자 추가

} catch (EmailException e) {

e.printStackTrace();

}

 try {

email.setFrom("bbbbbbbbbbbbbbbb@bbbbbbbbb.com", "bbbbbbbbbbbbbb"); // 보내는 사람

} catch (EmailException e) {

e.printStackTrace();

}

 email.setSubject("Test message"); // 메일 제목

 email.setContent("simple 메일 Test입니다", "text/plain; charset=euc-kr");

 

 email.setAuthentication("cccccccccccccc", "ddddddddddddd"); //메일인증

 email.send();

} catch (EmailException e) {

e.printStackTrace();

}




메일 테스트를 위해서 테스트용 메일서버 구축을 위해서

sendmail, james 등을 리눅스, 윈도에서 설치를 간단하게 진행해봤는데..


메일 테스트만을 위해서라면 gmail을 이용하는 것이 가장 좋은 것 같다.



html 버전 테스트



// Create the email message

HtmlEmail email = new HtmlEmail();

email.setCharset("euc-kr"); // 한글 인코딩  

email.setHostName("smtp.gmail.com");

email.setSmtpPort(465);

email.setSSL(true);

email.addTo("aaa@aaaaaaaaac.om", "이름인가");  //수신자

email.setFrom("bbbb@bbbbbbbbb.com", "이름인가"); //발신자

email.setSubject("메일 테스트 입니다. ");

  

// embed the image and get the content id

//URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");

//String cid = email.embed(url, "Apache logo");

  

// set the html message

///images/home/bul_citizen_icon01.png

StringBuffer strBuf = new StringBuffer();

strBuf.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"ko\">");

strBuf.append("<head>");

strBuf.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");

strBuf.append("<meta http-equiv=\"Content-Script-Type\" content=\"text/javascript\">");

strBuf.append("<meta http-equiv=\"Content-Style-Type\" content=\"text/css\">");

strBuf.append("<title>ㅁㅁㅁㅁㅁㅁㅁㅁㅁㅁㅁㅁㅁ</title>");

strBuf.append("</head>");

strBuf.append("<body style=\"margin:0;\">");

strBuf.append(" <table style=\"background-color:#fff;font-size:13px;color:#333;font-family:dotum;line-height:1.4\" align=\"center\" width=\"750\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");

strBuf.append(" <tbody>");

strBuf.append(" <!-- header -->");

strBuf.append(" <tr>");

strBuf.append(" <td>");

strBuf.append(" header ~~~~~");

strBuf.append(" </td>");

strBuf.append(" </tr>");

strBuf.append(" <!-- header //end -->");

strBuf.append(" <!-- content -->");

strBuf.append(" <tr>");

strBuf.append(" <td colspan=\"2\" align=\"center\" height=\"240\" style=\"position:relative;\">");

strBuf.append(" </td>");

strBuf.append(" </tr>");

strBuf.append(" <tr>");

strBuf.append(" <td height=\"30\">");

strBuf.append(" </td>");

strBuf.append(" </tr> ");

strBuf.append(" <tr>");

strBuf.append(" <td>");

strBuf.append(" 내용을 입력합니다.");

strBuf.append(" </td>");

strBuf.append(" </tr>");

strBuf.append(" <tr>");

strBuf.append(" <td height=\"40\">");

strBuf.append(" </td>");

strBuf.append(" </tr>");

strBuf.append(" <!-- content //end -->");

strBuf.append(" <!-- footer -->");

strBuf.append(" <tr>");

strBuf.append(" <td height=\"1\" bgcolor=\"#4d4d4d\"></td>");

strBuf.append(" </tr>");

strBuf.append(" <tr>");

strBuf.append(" <td height=\"107\" align=\"center\" style=\"color:#707070;\">");

strBuf.append("

strBuf.append(" </td>");

strBuf.append(" </tr>");

strBuf.append(" <!-- footer //end -->");

strBuf.append(" </tbody>");

strBuf.append(" </table>");

strBuf.append("</body>");

strBuf.append("</html>");

  

email.setHtmlMsg(strBuf.toString());


// set the alternative message

//email.setTextMsg("Your email client does not support HTML messages");


// send the email


email.setAuthentication("아이디", "비밀번호"); //메일인증


email.send();




















출처 : http://commons.apache.org/proper/commons-email/userguide.html

Comments