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

所属分类: 网络编程 / JSP编程 阅读数: 1866
收藏 0 赞 0 分享
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>

更多精彩内容其他人还在看

张孝祥JAVA全集 Ftp下载[100%能下载]

张孝祥JAVA全集 Ftp下载[100%能下载]
收藏 0 赞 0 分享

JSP一句话木马代码

JSP一句话木马代码
收藏 0 赞 0 分享

写一个对搜索引擎友好的文章SEO分页类

写一个对搜索引擎友好的文章SEO分页类
收藏 0 赞 0 分享

卖jsp编程技巧的那个垃圾的所有实例的答案全部已收集,现将他人收集的实例答案公布出来,大家鉴赏!

卖jsp编程技巧的那个垃圾的所有实例的答案全部已收集,现将他人收集的实例答案公布出来,大家鉴赏!
收藏 0 赞 0 分享

ajax+jsp草稿自动保存的实现代码

ajax+jsp草稿自动保存的实现代码
收藏 0 赞 0 分享

一个jsp+AJAX评论系统第1/2页

一个jsp+AJAX评论系统
收藏 0 赞 0 分享

什么是eclipse,eclipse的意思解析

什么是eclipse,eclipse的意思解析
收藏 0 赞 0 分享

经常听朋友说什么J2EE,终于知道点什么是J2EE了,汗一个

经常听朋友说什么J2EE,终于知道点什么是J2EE了,汗一个
收藏 0 赞 0 分享

搭建SSH时的思考和遇到的几个问题的解决方法

搭建SSH时的思考和遇到的几个问题的解决方法
收藏 0 赞 0 分享

RMI使用学习 小结

RMI使用学习 小结
收藏 0 赞 0 分享
查看更多