首页 > 学院 > 开发设计 > 正文

使用Servlet发送电子邮件

2019-11-18 16:09:01
字体:
来源:转载
供稿:网友
     使用javaMail实现收发电子邮件功能  一文中介绍了如何安装JavaMail,使用JavaMail实现简单的邮件收发功能。本文介绍如何使用Servlet发送电子邮件。由于如何使用JavaMail已经在上文中有所介绍,因此本文将不做过多的原理介绍。

     用Servlet实现邮件发送非常简单,我们只是需要构建一个jsp文件,提供给用户提交邮件信息的表单。
使用Servlet发送电子邮件

 

 

 

 

 

 

 

<%@ page contentType="text/Html;charset=gb2312"%>
<html>
 <head>
  <title>Welcome</title>
 </head>
 <body>
  <center>
  <p>Welcome</p>
  <form name="form1" method="post" action="maildo">
    <p>收件人:
      <input type="text" name="send">
    </p>
    <p>主 题:
      <input type="text" name="subject">
    </p>
    <p>内 容:
      <textarea name="content"></textarea>
    </p>
    <p>
      <input type="submit" name="Submit" value="send">
    </p>
  </form>
  <p>&nbsp;</p>
</center>
 </body>
</html>

    浏览器收集到用户输入的信息后传递给Servlet来处理,Servlet通过request.getParameter()方法得到相关的数据后,通过JavaMail提供的API发送电子邮件。

package com.j2medev.servletmail;

import java.io.IOException;
import java.util.PRoperties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.*;
import java.net.*;

public class MailServlet extends HttpServlet
{
    private static String host;

    private static String from;

    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
        host = config.getInitParameter("host");
        from = config.getInitParameter("from");
        System.out.println(host + from);
    }


    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {

        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
      
        String send = request.getParameter("send");
        String subject = request.getParameter("subject");
        String content = request.getParameter("content");

        try
        {
            Properties props = System.getProperties();
            // Setup mail server
            props.put("mail.smtp.host", host);
            // Get session
            Session session = Session.getDefaultInstance(props, null);
            // Define message
            MimeMessage message = new MimeMessage(session);
            // Set the from address
            message.setFrom(new InternetAddress(from));
            // Set the to address
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(
                    send));
            // Set the subject
            message.setSubject(subject);
            // Set the content
            message.setText(content);
            // Send message
            Transport.send(message);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}


    接下来我们编辑web.xml文件,注意在init-param中写上host和from的参数,以便Servlet初始化的时候可以读取。
<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
 <servlet>
  <servlet-name>ServletMail</servlet-name>
  <servlet-class>com.j2medev.servletmail.MailServlet</servlet-class>
  <init-param>
   <param-name>host</param-name>
   <param-value>smtp.263.net</param-value>
     </init-param>
     <init-param>
      <param-name>from</param-name>
      <param-value>eric.zhan@263.net</param-value>
     </init-param>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>ServletMail</servlet-name>
  <url-pattern>/maildo</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

 <error-page>
  <error-code>404</error-code>
  <location>/error.jsp</location>
 </error-page>

</web-app>
    由于我们需要用到JavaMail和JAF的支持,因此我们需要把相关的包放到YOURAPP/WEB_INF/lib下。在浏览器输入http://localhost:8080/yourapp/index.jsp 发送电子邮件。

(出处:http://www.VeVb.com)



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表