/* * Created on 2003/04/15 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package test; import java.io.IOException; import java.io.PrintWriter; import java.text.DateFormat; import java.util.Date; import javax.mail.Message; import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @web:servlet name="JavaMailServlet" * display-name="JavaMail TEST Servlet" * load-on-startup="1" * @web:servlet-mapping url-pattern="/JavaMailTest/*" * @web:resource-ref * name="TheMailSession" * type="javax.mail.Session" auth="Application" * @author tkyn * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class JavaMailServlet extends HttpServlet { //protected public void doGet(ServletRequest request, ServletResponse response) throws ServletException, IOException { //TODO Method stub generated by Lomboz } /* Servlet Spec. doGet() method is... public void doGet( HttpServletRequest request, HttpServletResponse response ) throws IOException, ServletException { } */ public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // ContentTypeを設定 response.setContentType("text/html; charset=euc-jp"); // 出力用PrintWriterを取得 PrintWriter out = response.getWriter(); // 出力 out.println(""); out.println(""); out.println("Hello World Servlet"); out.println(""); out.println(""); out.println("Hello World
"); sendNotice("-SendMailAddress-"); out.println("Sent mail."); out.println(""); out.println(""); } private static final String mailer = "JavaMailer"; private void sendNotice(String recipient) throws ServletException { try { Context initial = new InitialContext(); Session session = (Session) initial.lookup("java:comp/env/TheMailSession"); Message msg = new MimeMessage(session); msg.setFrom(); msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse(recipient, false)); msg.setSubject("Test Message from ConfirmerServlet"); DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT); Date timeStamp = new Date(); String messageText = "Thank you for your order." + '\n' + "We received your order on " + dateFormatter.format(timeStamp) + "."; msg.setText(messageText); msg.setHeader("X-Mailer", mailer); msg.setSentDate(timeStamp); javax.mail.Transport.send(msg); } catch (Exception e) { throw new ServletException(e.getMessage()); } } }