[JSP] 發送MAIL之一

  • 2616
  • 0

摘要:[JSP] 發送MAIL之一

最近遇到JSP要發送MAIL功能

許久沒接觸JSP了

只好把學校所學的書籍搬出來看看

不多說直接看CODE

使用JSP發送MAIL需要用到2個JAR檔

為activation.jar與mail.jar如底下附件

把lib.zip解壓縮放置WEB-INF底下即可

lib.zip

 

 

#mailInput.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Mail Input</title>
    </head>
    <body>
        <h1>Send Mail</h1>
        <form method="post" action="sendMail.jsp">
          From Email:<input type="text" name="from" value="" />

          To Email  :<input type="text" name="to" value="">

          Subject   :<input type="text" name="subject" value="">

          Content   :<textarea name="content" cols="80" rows="6"></textarea>

          <input type="submit" value="Send" name="send" />
          <input type="reset" value="Reset" name="reset" />
        </form>
    </body>
</html> 

 

#sendMail.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%@ page import="java.util.*,java.io.*" %>

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Send Mail</title>
  </head>
  <body>
<%
  InternetAddress[] address = null;
  request.setCharacterEncoding("UTF-8");
  String mailhost   = "XXXX";         //輸入MAIL HOST                 
  String from          = request.getParameter("from");               
  String to               = request.getParameter("to");
  String subject     = request.getParameter("subject");
  String content     = request.getParameter("content");
  boolean sessionDebug = true;
  String userName="XXX";         //輸入帳號
  String password = "XXX";         //輸入密碼
  try {
   
    java.util.Properties props = System.getProperties();
    props.put("mail.smtp.host",mailhost);
    props.put("mail.smtp.auth", "true");
    
    Authenticator auth = new javax.mail.Authenticator() {
      String userName = "XXX";         //輸入帳號
      String password = "XXX";         //輸入密碼
      protected PasswordAuthentication getPasswordAuthentication(){
        return new PasswordAuthentication(userName, password);
      }
    };
    javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(props,auth);
    mailSession.setDebug(sessionDebug);
    Message msg = new MimeMessage(mailSession);
    // set mail from
    msg.setFrom(new InternetAddress(from));
    // set to email
    address = InternetAddress.parse(to,false);
    msg.setRecipients(Message.RecipientType.TO, address);
    // set mail subject
    msg.setSubject(subject);
    // set send date
    msg.setSentDate(new Date());
    // set content
    msg.setText(content);
    // Get Transport use userName and password
    Transport trans = mailSession.getTransport("smtp");
    trans.connect(userName, password);
    trans.send(msg);
    %>Send OK<%
  }
  catch (Exception ex) {
    out.println(ex);
  }
%>
  </body>
</html>
  

 

 

 

 

 






Y2J's Life:http://kimenyeh.blogspot.tw/