메일 서버 구축후
메일 라이브러리를 통해서
간편하게 메일을 전송할수 있는 JSP파일을 작성한다.
JAF(JavaBeans Activation Framework)와 JavaMail 이 필요합니다.
JAF 다운로드 jaf-1_1_1.zip -> mail.jar
http://www.oracle.com/technetwork/java/jaf11-139815.html
javaMail 다운로드 javamail1_4_4.zip -> activation.jar
http://www.oracle.com/technetwork/java/index-138643.html
파일명 : mailForm.jsp
-------------------------------------------------------
<%@ page contentType="text/html; charset=euc-kr" %>
<html>
<head>
<title>메일보내기</title>
</head>
<body leftmargin="0" topmargin="0">
<div align="center">메일 보내기</div>
<form name='mailForm' method='post' action="./mailProc.jsp">
<table border="0" align="center">
<tr>
<th bgcolor='silver'>받는 사람</th>
<td><input type="text" name="to" size="61" value='prettyimo@naver.com'></td>
</tr><tr>
<th bgcolor="silver">보내는 사람</th>
<td><input type="text" name="from" size="61" value='test@prettyimo.cafe24.com'></td>
</tr><tr>
<th bgcolor='silver'>제 목</th>
<td><input type="text" name="subject" size="61" value="메일을 보냅니다."></td>
</tr>
<tr>
<th bgcolor='silver'>편지 내용</th>
<td><textarea name="msgText" rows="15" cols="60"></textarea></td>
</tr>
</table>
<div align="center">
<input type="submit" value="보내기">
<input type="button" value="취소" onclick="javascript:history.back()">
</div>
</form>
</body>
</html>
파일명 : mailProc.jsp
-------------------------------------------------------
<%@ page contentType = "text/html; charset=euc-kr" %>
<%@ page import="java.util.*,java.io.*,javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%
request.setCharacterEncoding("euc-kr");
%>
<html>
<head>
<title>mailProc.jsp</title>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
</head>
<body leftmargin="0" topmargin="0">
<div align="center">
<%
// javamail lib 이 필요합니다.
class MyAuthentication extends Authenticator {
PasswordAuthentication pa;
public MyAuthentication(){
pa = new PasswordAuthentication("test@prettyimo.cafe24.com", "test1004");
//ex) ID:cafe24@cafe24.com PASSWD:1234
}
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
}
String subject = request.getParameter("subject"); // 제목
String msgText = request.getParameter("msgText"); // 내용
msgText = msgText.replace("\n", "<BR>");
// mw-002.cafe24.com
String host = "mw-002.cafe24.com"; // smtp mail server(서버관리자)
String from = request.getParameter("from"); // 보내는 주소
String to = request.getParameter("to"); // 받는 사람
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
Authenticator auth = new MyAuthentication();
Session sess = Session.getInstance(props, auth); // 계정 인증 검사
try {
Message msg = new MimeMessage(sess);
msg.setFrom(new InternetAddress(from)); // 보낸 사람
// 한명에게만 보냄
InternetAddress[] address = {new InternetAddress(to)};
// 다중 전송
// InternetAddress[] address = {new InternetAddress(to), new InternetAddress(to2)};
msg.setRecipients(Message.RecipientType.TO, address); // 수령인
msg.setSubject(subject); // 제목
msg.setSentDate(new Date()); // 보낸 날짜
// msg.setText(msgText); // 글을 문자만 보낼 경우
// 글을 HTML 형식으로 보낼 경우
msg.setContent(msgText, "text/html;charset=euc-kr");
Transport.send(msg); // 전송
out.println(to + "님에게 메일을 발송했습니다.");
} catch (MessagingException mex) {
out.println(mex.getMessage()+"<br>");
out.println(to + "님에게 메일 발송을 실패 했습니다.");
}
%>
</div>
</body>
</html>
'..열심히 공부하세.. > JSP' 카테고리의 다른 글
JSP기초 및 자바스크립트 유효성 검사 (0) | 2012.05.17 |
---|---|
[28] 자바 메일보내기 (첨부파일) (0) | 2012.03.06 |
[파일업로드] UploadManager.java (0) | 2012.03.05 |
[26] PdsDTO와 PdsDAO (0) | 2012.03.05 |
[25] 파일업로드 테이블 설계 (0) | 2012.03.05 |