邮件发送简单例子-jsp文件

发布时间:2022-04-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了邮件发送简单例子-jsp文件脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
MailExample.jsp

<htML>
<head>
<tITle>JSP JavaMail Example </title>
</head>

<body>

<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>

<%
String host = "yourmailhost";
String to = request.getParameter("to");
String From = request.getParameter("from");
String subject = request.getParameter("subject");
String messageText = request.getParameter("body");
boolean sessionDebug = false;

// Create some PRoPErties and get the default Session.
Properties props = System.getProperties();
props.put(";mail.host", host);
props.put("mail.transport.protocol", "smtp");

Session mailSession = Session.getDefaultInstance(props, null);

// Set debug on the Session so we can see what is going on
// Passing false will not echo debug info, and passing true
// will.
mailSession.setDebug(sessionDebug);

// Instantiate a new MimeMessage and fill it with the
// required information.
Message msg = new MimeMessage(mailSession);

msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);

// Hand the message to the default transport service
// for delivery.
Transport.send(msg);

out.println("Mail was sent to " + to);
out.println(" from " + from);
out.println(" using host " + host + ".");

%>
</table>
</body>
</html>

脚本宝典总结

以上是脚本宝典为你收集整理的邮件发送简单例子-jsp文件全部内容,希望文章能够帮你解决邮件发送简单例子-jsp文件所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: