1. 메일 발송폼(sendmail.html)

<html>

 <head><title>Sending a Custom Email - HTML Form</title></head>

 <body>

  <p align="center"><b>A custom email utility.</b></p>

  <form name="form" action="sendmail.jsp" method="post">

   <table align="center">

<tr><td>From</td><td><input name="from" size="50" value=""></td></tr>

     <tr><td>To</td><td><input name="to" size="50"  value="chchu@ihelpers.co.kr"></td></tr>

     <tr><td>Subject</td><td>

      <input name="subject" size="50" value="[JavaMail Example - http://www.ihelpers.co.kr]">

     </td></tr>

     <tr><td colspan="2">

      <textarea name="text" cols="50" rows="20">Hello from JavaMail!</textarea>

     </td></tr>

     <tr><td colspan="2" align="center"><input type="submit" value="Send Email"></td></tr>

   </table>

  </form>

 </body>

</html>

2. 메일 발송 JSP 페이지(sendmail.jsp)

<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>

<%

             String subject = new String(request.getParameter("subject").getBytes("8859_1"),"KSC5601");

             String text = new String(request.getParameter("text").getBytes("8859_1"),"KSC5601");

             Properties props = new Properties();

             String host = "mail.your.server"; // SMTP 서버 설정

             props.put("mail.smtp.host", host);

             Session s = Session.getInstance(props,null);

             MimeMessage message = new MimeMessage(s);

             String fromAddress = request.getParameter("from");        //보내는이

             InternetAddress from = new InternetAddress(fromAddress);

             message.setFrom(from); // 보내는이 설정

             String toAddress = request.getParameter("to");

             InternetAddress to = new InternetAddress(toAddress); // 받는이 설정

             message.addRecipient(Message.RecipientType.TO, to);  

             message.setSubject(subject); // 제목

             message.setContent(text, "text/html; charset=EUC-KR"); // content Type 설정

             message.setText(text); // 본문

             Transport.send(message); // 메일 발송

%>

<html>

<p align="center">The Message has been sent.<br>Check your inbox.</p>

<p align="center"><a href="sendmail.html">Click here to send another!</a></p>

</html>

 

위의 부분에서 특히 눈여겨 보실 부분은 SMTP 서버로 사용될 서버를 지정하는 부분

(String host = "mail.your.server") 과 메시지의 한글화 처리 부분입니다.

그리고 메일의 content 타입을 설정하는 부분입니다. (message.setContent(text, "text/html; charset=EUC-KR");)

단순히 텍스트로만 발송할경우에는 text/html 대신에 text/plain으로 바꾸시면 됩니다.

 

그밖의 부분은 자바메일 설치시 함께 제공되는 문서를 참조하시기 바랍니다.

 

- www.okjsp.pe.kr

- java.sun.com

- www.hotscripts.com