首页 > 代码库 > Java Web总结二十三发送邮件

Java Web总结二十三发送邮件

一、所需jar包:

  1、mail.jar

  2、activation.jar

二、代码:

  1、MailUtil.java

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;
    }
}

2、LoginServlet.java

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);
    }

}

3、login.jsp

<%@ 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>