① maven 에서 javamail API 와 JavaBeans(TM) Activation Framework ( jdk 8 이상일 경우만) 라이브러리 다운받아 점프 시킴 (module path 말고 classpath로 점프시켜야함) - 간단하게 구현해보기 위해 JavaMail API는 compat 버전 사용 , 현재 jdk - 11 쓰기 때문에 javax.activation 추가로 다운 받음
2 ) properties 활용해서 SMTP 설정값 세팅 - 파일을 따로 만들지 않고 do에서 바로 생성
//메일서버 SMTP 세팅 설정 값
Properties pp = new Properties();
//메일서버 도메인주소
pp.put("mail.smtp.host", host);
//메일서버 전송 port 번호
pp.put("mail.smtp.port", 587);
//로그인에 대한 암호화 사용
pp.put("mail.smtp.auth", "true");
//메일 회신 주소가 잘못 되어 반송 되는 사항
pp.put("mail.smtp.debug", "true");
//메일 발송에 대한 소켓 포트 통신
pp.put("mail.smtp.socketFactoty.port", 587);
//TLS(전송통신보안 정책)
pp.put("mail.smtp.ssl.protocols", "TLSv1.2");
3 ) 해당 서비스에 자동 로그인으로 서버에 접근하는 암호화 형태의 코드
- session 클래스 로드시 유의!
Session ss = Session.getDefaultInstance(pp, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication(login_user,login_pass);
}
});
4 ) 실제 발송 실행하는 코드
try {
//MIME => 인터넷 프로토콜 서비스 HTTP로 서로 통신하는 전자우편
MimeMessage msg = new MimeMessage(ss);
//보내는 사람의 정보 (이메일,이름)
msg.setFrom(new InternetAddress(fromemail,ename));
//받는사람의 정보
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to_mail));
//메일 제목
msg.setSubject(subject);
//메일 내용
msg.setText(mailtext);
//메일 발송
Transport.send(msg);
//최종 결과값 출력
PrintWriter pw = response.getWriter();
pw.write("<script>"
+ "alert('메일 발송이 정사적으로 되었습니다.');"
+ "location.href='./qamail.jsp';"
+ "</script>");
pw.close();
} catch (Exception e) {
System.out.println("메일 발송 실패");
}
5 ) 발송 실행
6 ) 전체 코드
public class qamailok extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String ename=request.getParameter("ename");
String fromemail=request.getParameter("fromeamil");
String epass=request.getParameter("epass");
String subject=request.getParameter("subject");
String mailtext=request.getParameter("mailtext");
//SMTP (메일 발송하는 서버),
//POP3(메일을 수신 받을 경우 PC에 저장됨/단,서버에는 저장하지 X)
//IMAP3(메일을 수신받을 경우 PC와 서버에 저장)
//메일 서버 (기본정보)
String host = "smtp.naver.com";
String login_user = "네이버아이디";
String login_pass = "";
String to_mail = "네이버메일@naver.com"; // 받는 메일주소
//메일서버 SMTP 세팅 설정값
Properties pp = new Properties();
pp.put("mail.smtp.host", host);
pp.put("mail.smtp.port", 587);
pp.put("mail.smtp.auth", "true");
pp.put("mail.smtp.debug", "true");
pp.put("mail.smtp.socketFactoty.port", 587);
//TLS(전송통신보안 정책)
pp.put("mail.smtp.ssl.protocols", "TLSv1.2");
//해당 서비스에 자동 로그인으로 서버에 접근하는 암호화 형태의 코드
Session ss = Session.getDefaultInstance(pp, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication(login_user,login_pass);
}
});
try {
//MIME => 인터넷 프로토콜 서비스 HTTP로 서로 통신하는 전자우편
MimeMessage msg = new MimeMessage(ss);
msg.setFrom(new InternetAddress(fromemail,ename));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to_mail));
msg.setSubject(subject);
msg.setText(mailtext);
Transport.send(msg);
PrintWriter pw = response.getWriter();
pw.write("<script>"
+ "alert('메일 발송이 정상적으로 되었습니다.');"
+ "location.href='./qamail.jsp';"
+ "</script>");
pw.close();
} catch (Exception e) {
System.out.println("메일 발송 실패");
}
}
}