当前位置 : 主页 > 编程语言 > java >

JSP 发送邮件

来源:互联网 收集:自由互联 发布时间:2021-06-25
参考,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.*" % % Str

参考,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 = "[email protected]"; // 发件人的电子邮件 String from = "[email protected]"; // 假设你是从本地主机发送电子邮件 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
上一篇:servlet&jsp
下一篇:JSP数据交互
网友评论