/*-------------------------------------------------------------------------
MailForm.jsp
--------------------------------------------------------------------------*/
<%@ page contentType="text/html;charset=euc-kr" %>
<HTML> <HEAD> <TITLE>메일 보내기</TITLE> <STYLE> <!-- td{font-size: 12pt; font-weight: bold;} //--> </STYLE> <SCRIPT language = "JavaScript"> function isValue(obj) { if(obj == null || obj.value.length <= 0) return false; else return true; }
function mailcheck(str){ if ( str.value.search(/(\S+)@(\S+)\.(\S+)/) == -1 ){ // 이메일 주소 alert("올바른 이메일주소를 입력하세요.\n 예)whlions@hanmail.net"); return false; } else return true; }
function funcProcess(command) { var form = document.mail; if( command == 'MAIL') { if(!isValue(form.name)) { alert("보내는 사람 이름을 입력하세요~"); form.name.focus(); return false; } if(!isValue(form.from)) { alert("송신메일주소를 입력하세요~"); form.from.focus(); return false; }else{ if(!mailcheck(form.from)) { form.from.focus(); return false; } } if(!isValue(form.to)) { alert("수신메일주소를 입력하세요~"); form.to.focus(); return false; }else{ if(!mailcheck(form.to)) { form.to.focus(); return false; } } if(isValue(form.cc)) { if(!mailcheck(form.cc)) { form.cc.focus(); return false; } }
if(!isValue(form.subject)) { alert("제목을 입력하세요~"); form.subject.focus(); return false; } if(!isValue(form.body)) { alert("내용을 입력하세요~"); form.body.focus(); return false; } }
form.action = 'mailSend.jsp'; form.submit(); } </SCRIPT> </HEAD>
<BODY text = "#804000" >
<FORM method="post" name="mail"> <BR> <BR> <BR> <H2><CENTER><Font color = 'red' face = '굴림체'> 메일을 함 보내봅시다 </Font></CENTER></H2> <TABLE width = "570" height = "500" bgcolor = "#EEEE88" align = "center" valign = "middle" cellpadding = "0" cellspacing = "0" border = "0"> <TR> <TD> <TABLE width = "560" align = "center" cellpadding = "1" cellspacing = "1" border = "0" bgColor = "#ffff99"> <TR> <TD width = "259" align = "right">메일 서버 : </TD> <TD width = "389" algin = "left"> <INPUT type = "text" name = "server" > </TD> </TR> <TR> <TD width = "259" align = "right"> 내 이름 : </TD> <TD width = "389" align = "left"> <INPUT type = "text" name = "name" maxlength = "30"> </TD> </TR> <TR> <TD width = "259" align = "right"> 내 메일 : </TD> <TD width = "389" align = "left"> <INPUT type = "text" name = "from" maxlength = "30"> </TD> </TR> <TR> <TD width = "259" align = "right"> 받는사람 메일 : </TD> <TD width = "389" align = "left"> <INPUT type = "text" name = "to" maxlength = "30"> </TD> </TR> <TR> <TD width = "259" align = "right" >제 목 : </TD> <TD width = "389" align = "left"> <INPUT type = "text" name = "subject" maxlength = "30"> </TD> </TR>
<TR> <TD width = "259" align = "right" valign = "top" height = "87">내용 : </TD> <TD width = "389" height = "87"> <TEXTAREA name = "body" rows = "12" cols = "50"></TEXTAREA> </TD> </TR> <TR> <TD width = "259"> </TD> <TD width = "389" align = "left"> <INPUT type = "button" onClick = "funcProcess('MAIL')" value = "보내기"> <INPUT type = "reset" value = "다시작성"> </TD> </TR> </TABLE> </TD> </TR> </TABLE> </FORM> </BODY> </HTML>
/*-------------------------------------------------------------------------
mailSend.jsp
--------------------------------------------------------------------------*/
<%@ page contentType="text/html;charset=euc-kr" %> <%@ page import="javax.mail.*, javax.mail.internet.*, java.util.Properties" %>
<% //0. 폼의 화면으로 부터 입력값 얻어오기
// String server = request.getParameter("server"); String server = "localhost"; String name = "aa"; String from = "bb@bb.co.kr"; String to = "aa@aa.co.kr"; String subject = "aa"; String body = "cccccc";
//------------------------------------------------------------------------- // ### 메세지 보내기 위한 절차 ### // // 1. Properties 객체 얻어오기 // 2. Session.getInstance() 로 메일 세션을 시작 // 3. Message 객체의 구현 클래스를 초기화하여 Message 객체를 생성 // 4. Message의 From 주소를 설정 // 5. Message의 To 주소를 설정 // 6. Message의 제목을 설정 // 7. Message의 내용을 설정 // 8. Transport 객체를 얻어와서 sendMessage()를 이용하여 Message를 전송
Properties props = System.getProperties();
Session sess = Session.getInstance( props, null );
Message msg = new MimeMessage( sess);
try{
Address fromAddr = new InternetAddress( from ); msg.setFrom( fromAddr );
Address toAddr = new InternetAddress( to ); msg.setRecipient( Message.RecipientType.TO , toAddr );
msg.setSubject( subject );
msg.setContent( body, "text/plain;charset=euc-kr" );
Transport tran = sess.getTransport("smtp"); tran.connect( server, "", "" ); tran.sendMessage( msg, msg.getAllRecipients() ); tran.close();
out.print(" 멜 갔는지..확인해보시오....^^");
} catch( Exception ex ){ out.print("멜이 실패했소 -> " + ex.toString() ); ex.printStackTrace( ); } %>
|