| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > Linux频道 > Linux开发区 > WEB开发

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

时间:2007-07-27 11:51:46  来源:Linux联盟收集整理  作者:
MailExample.jspZaMLinux联盟
ZaMLinux联盟
<html>ZaMLinux联盟
<head>ZaMLinux联盟
<title>JSP JavaMail Example </title>ZaMLinux联盟
</head>ZaMLinux联盟
ZaMLinux联盟
<body>ZaMLinux联盟
ZaMLinux联盟
<%@ page import="java.util.*" %>ZaMLinux联盟
<%@ page import="javax.mail.*" %>ZaMLinux联盟
<%@ page import="javax.mail.internet.*" %>ZaMLinux联盟
<%@ page import="javax.activation.*" %>ZaMLinux联盟
ZaMLinux联盟
<%ZaMLinux联盟
String host = "yourmailhost";ZaMLinux联盟
String to = request.getParameter("to");ZaMLinux联盟
String from = request.getParameter("from");ZaMLinux联盟
String subject = request.getParameter("subject");ZaMLinux联盟
String messageText = request.getParameter("body");ZaMLinux联盟
boolean sessionDebug = false;ZaMLinux联盟
ZaMLinux联盟
// Create some properties and get the default Session.ZaMLinux联盟
Properties props = System.getProperties();ZaMLinux联盟
props.put("mail.host", host);ZaMLinux联盟
props.put("mail.transport.protocol", "smtp");ZaMLinux联盟
ZaMLinux联盟
Session mailSession = Session.getDefaultInstance(props, null);ZaMLinux联盟
ZaMLinux联盟
// Set debug on the Session so we can see what is going onZaMLinux联盟
// Passing false will not echo debug info, and passing trueZaMLinux联盟
// will.ZaMLinux联盟
mailSession.setDebug(sessionDebug);ZaMLinux联盟
ZaMLinux联盟
// Instantiate a new MimeMessage and fill it with theZaMLinux联盟
// required information.ZaMLinux联盟
Message msg = new MimeMessage(mailSession);ZaMLinux联盟
ZaMLinux联盟
msg.setFrom(new InternetAddress(from));ZaMLinux联盟
InternetAddress[] address = {new InternetAddress(to)};ZaMLinux联盟
msg.setRecipients(Message.RecipientType.TO, address);ZaMLinux联盟
msg.setSubject(subject);ZaMLinux联盟
msg.setSentDate(new Date());ZaMLinux联盟
msg.setText(messageText);ZaMLinux联盟
ZaMLinux联盟
// Hand the message to the default transport serviceZaMLinux联盟
// for delivery.ZaMLinux联盟
Transport.send(msg);ZaMLinux联盟
ZaMLinux联盟
out.println("Mail was sent to " + to);ZaMLinux联盟
out.println(" from " + from);ZaMLinux联盟
out.println(" using host " + host + ".");ZaMLinux联盟
ZaMLinux联盟
%>ZaMLinux联盟
</table>ZaMLinux联盟
</body>ZaMLinux联盟
</html>ZaMLinux联盟
ZaMLinux联盟
来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
栏目更新
栏目热门