[공식] 참조, 자료 다운로드 사이트
http://java.sun.com/products/javamail/
자바메일 전송을 위한 간단한 샘플소스입니다.
먼저 선행 되어야 하는 작업은 [서비스 이용법] 자바메일 API는 사용할 수는 없나요?
javamail이 작동될 수 있도록 자신의 환경이 되어야 가능합니다.
Tomcat4 환경에서는 기본적으로 자바메일(javamail)을 제공하고 있습니다.
-------------------- javamailsend.jsp -----------------------------------------
<%@ page contentType="text/html;charset=euc-kr"
import="java.util.*, javax.mail.*, javax.mail.internet.*"
%>
<%! // 함수 선언할 때는 ! 붙여줍니다.
public String kr(String s) {
try {
//s = (s == null) ? "" : new String(s.getBytes("8859_1"),"KSC5601");
s = (s == null) ? "" : new String(s.getBytes("8859_1"),"");
} catch (java.io.UnsupportedEncodingException uee) {}
return s;
}
%>
<html><head><title>메일전송결과</title>
<%
// 사용자가 입력한 메일 전송 자료를 저장
String mailFrom = null;
String mailTo = null;
String title = null;
String contents = null;
String htmltag = null;
String user = null;
String password = null;
// Resin 일 경우 kr을 뺍니다. 한글변환을 하지 않습니다.
// 톰캣일 경우 소스 그대로 사용합니다. 한글변환 필요합니다.
mailFrom = kr(request.getParameter("from"));
mailTo = kr(request.getParameter("to"));
title = kr(request.getParameter("title"));
contents = kr(request.getParameter("content"));
htmltag = "<font color=BLUE size=2>";
contents = htmltag + contents;
// Session을 생성하기 위해 java.util.Properties 클래스를
// 생성하고 자신이 해당하는 SMTP 호스트 주소를 할당합니다.
Properties props = new Properties();
// 가이드라인 웹서버에 SMTP 서버가 구축되어 있기에 localhost 가능하다.
props.put("mail.smtp.host", "localhost");
// Message 클래스의 객체를 Session을 이용해 생성합니다.
MimeMessage msg = new MimeMessage(msgSession);
InternetAddress from = new InternetAddress(mailFrom);
msg.setFrom(from);
InternetAddress to = new InternetAddress(mailTo);
msg.setRecipient(Message.RecipientType.TO, to);
msg.setSubject(title,"euc-kr");
msg.setContent(contents, "text/html; charset=euc-kr");
Transport.send(msg);