使用Servlet发送邮件
使用Servlet发送邮件,需要使用官方提供的JavaMail工具包。javax.mail和javax.mail.internet软件包包含发送和接收电子邮件需要的所有类。
1 下载并导入JavaMail工具包
要使用JavaMail API发送电子邮件,您需要加载两个jar文件:
2 编写index.jsp
index.jsp用于编写邮件内容:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>一点教程网-使用Servlet发送邮件</title>
</head>
<body>
<h1>使用Servlet发送邮件</h1>
<form action="SendMail" method="post">
收件人:<input type="text" name="to"/><br/>
主题:<input type="text" name="subject"><br/>
正文:<textarea rows="10" cols="70" name="msg"></textarea><br/>
<input type="submit" value="发送"/>
</form>
</body>
</html>
3 编写SendMailServlet
SendMailServlet用于接收用户的参数,调用邮件工具类:
import com.oreilly.servlet.MultipartRequest;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class SendMailServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String to=request.getParameter("to");
String subject=request.getParameter("subject");
String msg=request.getParameter("msg");
try {
MailUtil.sendMsg(to, subject, msg);
} catch (Exception e) {
e.printStackTrace();
}
out.print("邮件已经发送完成!");
out.close();
}
}
4 编写MailUtil工具类
MailUtil是JavaMail核心的发送邮件的逻辑:
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class MailUtil {
//实现邮件发送的方法
public static void sendMsg(String to, String subject, String content) throws Exception {
//发件人账户和密码(需自行修改)
String username = "xxx@sina.cn";
String pass = "xxx";
//一、连接邮箱服务器
Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.sina.cn"); //设置主机地址(需自行修改) smtp.qq.com smtp.126.com
props.setProperty("mail.smtp.auth", "true");//认证
//2.产生一个用于邮件发送的Session对象
Session session = Session.getInstance(props);
//二、创建邮件对象,设置邮件的参数
//3.产生一个邮件的消息对象
MimeMessage message = new MimeMessage(session);
//4.设置消息的发送者
Address fromAddr = new InternetAddress(username);
message.setFrom(fromAddr);
//5.设置消息的接收者
Address toAddr = new InternetAddress(to);
//TO 直接发送 CC抄送 BCC密送
message.setRecipient(MimeMessage.RecipientType.TO, toAddr);
//6.设置主题
message.setSubject(subject);
//7.设置正文
//设置邮件内容类型
message.setContent(content,"text/html;charset=utf-8");
//三、设置账户信息,进行发送
//8.准备发送,得到火箭
Transport transport = session.getTransport("smtp");
//9.设置火箭的发射目标
transport.connect("smtp.sina.cn", username, pass);
//10.发送
transport.sendMessage(message, message.getAllRecipients());
//11.关闭
transport.close();
}
}
注意:工具类中的邮件服务器信息和账户信息需要自行根据实际情况来修改!
5 配置web.ml
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>SendMailServlet</servlet-name>
<servlet-class>SendMailServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SendMailServlet</servlet-name>
<url-pattern>/SendMail</url-pattern>
</servlet-mapping>
</web-app>
6 运行测试
检查收件人邮箱,查看邮件是否成功发送:
热门文章
优秀文章