首页 > 编程语言 > 详细

Java Web总结二十三发送邮件

时间:2014-05-29 13:18:35      阅读:493      评论:0      收藏:0      [点我收藏+]

一、所需jar包:

  1、mail.jar

  2、activation.jar

二、代码:

  1、MailUtil.java

bubuko.com,布布扣
package com.gnnuit.web.mail.util;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailUtil {

    // 发送邮件
    public static void sendMail(String email, String username) throws Exception {
        Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.host", "127.0.0.1");
        Session session = Session.getDefaultInstance(props);
        Transport transport = session.getTransport();
        transport.connect("aaa@zhaojun.com", "123456");
        Message message = createMessage(session, email, username);
        Thread.sleep(10 * 1000);
        Transport.send(message);
        transport.close();
    }

    // 创建邮件
    public static Message createMessage(Session session, String mail,
            String username) throws Exception {
        MimeMessage message = new MimeMessage(session);
        // 网站管理员
        message.setFrom(new InternetAddress("aaa@zhaojun.com"));
        message.setRecipient(RecipientType.TO, new InternetAddress(mail));
        message.setSubject("BBS论坛");
        message.setContent("恭喜您," + username + "注册成功",
                "text/html;charset=utf-8");
        return message;
    }
}
bubuko.com,布布扣

2、LoginServlet.java

bubuko.com,布布扣
package com.gnnuit.web.mail.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gnnuit.web.mail.util.MailUtil;

public class LoginServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        final String username = request.getParameter("username");
        final String email = request.getParameter("email");
        // 一个线程发邮件
        new Thread() {

            @Override
            public void run() {
                try {
                    // 发送邮件
                    MailUtil.sendMail(email, username);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }.start();
        // 一个线程显示文件
        request.setAttribute("message", "注册成功");
        request.getRequestDispatcher("/message.jsp").forward(request, response);
    }

}
bubuko.com,布布扣

3、login.jsp

bubuko.com,布布扣
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    
  <body>
    <form action="${pageContext.request.contextPath }/LoginServlet" method="post">
        <table border="1" align="center">
            <caption>用户注册</caption>
            <tr>
                <th>用户名</th>
                <td><input type="text" name="username"/></td>
            </tr>
            <tr>
                <th>邮箱</th>
                <td><input type="text" name="email"/></td>                
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" value="提交"/>
                </td>
            </tr>
        </table>
    </form>
  </body>
</html>
bubuko.com,布布扣

 

Java Web总结二十三发送邮件,布布扣,bubuko.com

Java Web总结二十三发送邮件

原文:http://www.cnblogs.com/FlySheep/p/3756463.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!