参考,https://www.runoob.com/jsp/jsp-sending-email.html
<%@ page import="java.io.*,java.util.*,javax.mail.*"%> <%@ page import="javax.mail.internet.*,javax.activation.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %> <% String result; // 收件人的电子邮件 String to = "abcd@gmail.com"; // 发件人的电子邮件 String from = "mcmohd@gmail.com"; // 假设你是从本地主机发送电子邮件 String host = "localhost"; // 获取系统属性对象 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); // 获取默认的Session对象。 Session mailSession = Session.getDefaultInstance(properties); try{ // 创建一个默认的MimeMessage对象。 MimeMessage message = new MimeMessage(mailSession); // 设置 From: 头部的header字段 message.setFrom(new InternetAddress(from)); // 设置 To: 头部的header字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置 Subject: header字段 message.setSubject("This is the Subject Line!"); // 现在设置的实际消息 message.setText("This is actual message"); // 发送消息 Transport.send(message); result = "Sent message successfully...."; }catch (MessagingException mex) { mex.printStackTrace(); result = "Error: unable to send message...."; } %> <html> <head> <title>Send Email using JSP</title> </head> <body> <center> <h1>Send Email using JSP</h1> </center> <p align="center"> <% out.println("Result: " + result + "\n"); %> </p> </body> </html>
如上代码,在网上很容易找到,但是关于mail.jar;
activation.jar 放置位置都没有,我也是初次接触,现记录下
$TOMCAT_HOME
bin
conf
lib
...
webapps
<your app>
test.jsp (Can be in any directory under <your app> except WEB-INF)
WEB-INF
lib
mail.jar
activation.jar