iT邦幫忙

DAY 15
2

JSP 學習分享系列 第 15

JSP and JavaMail part1

  • 分享至 

  • xImage
  •  

使用JavaMail來發信
1.簡單的發信功能
=== mailInput.jsp ===

<%@page contentType="text/html" pageEncoding="UTF-8"%>

    
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Mail Input</title>
    
    
        <h1>Send Mail</h1>
        <form method="post" action="sendMail.jsp">
          From Email:<input type="text" name="from" value="" /><br>
          To Email  :<input type="text" name="to" value=""><br>
          Subject   :<input type="text" name="subject" value=""><br>
          Content   :<textarea name="content" cols="80" rows="6"></textarea><br>
          <input type="submit" value="Send" name="send" />
          <input type="reset" value="Reset" name="reset" />
        </form>
    
  

2.需要使用幾個jar檔,activation.jar , mail.jar
剩下的說明寫在程式碼裡
=== 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.*" %>


  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Send Mail</title>
  
  
<%
  InternetAddress[] address = null;
  request.setCharacterEncoding("UTF-8");
  String mailhost   = "smtp.pchome.com.tw";                          
  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="XXXXX";//your id
  String password = "XXXX";//your password
  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="XXXXX";//your id
      String password = "XXXXX";//your password
      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);
  }
%>
  

上一篇
JSP 如何上傳檔案
下一篇
JSP 和Java Mail part2
系列文
JSP 學習分享30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
aquarius6913
iT邦新手 5 級 ‧ 2013-06-19 10:11:37

這位大大請問我依照你的源碼下去做

在mailInput.jsp輸入完畢按下送出

會出現HTTP Status 500

org.apache.jasper.JasperException: Unable to compile class for JSP

我用apache TOMCAT 6.0.18 + JDK1.7.0.21

煩請大大解惑!!

0
O口O
iT邦新手 4 級 ‧ 2019-08-25 22:53:45

我是顯示:
javax.mail.AuthenticationFailedException: 535 5.7.8 Error: authentication failed: authentication failure

我在username: 使用username@pchome.com.tw或username的格式都顯示以上訊息.
我在pchome的網頁上已用帳號密碼輸入過,確認可以.但用上方程式登入就不行,

我要留言

立即登入留言