JSP发送EMAIL

使用JSP发送email学习笔记

在这里可以下载JAVAMAIL包 http://java.sun.com/products/javamail/

在解压后,将activation.jar和mail.jar复制到 /WEB-INF/lib 目录后重启服务器,JAVAMAIL便可以正常使用了

下面javamail.jsp 内容如下

<%@page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<html>
<head>
<title>JavaMail 电子邮件发送系统</title>
</head>
<body>

<%
// 以下变量为用户根据自己的情况设置
String smtphost = "smtp.now.net.cn"; // 发送邮件服务器
String user = "yauboo"; // 邮件服务器登录用户名
String password = "xxx"; // 邮件服务器登录密码
String from = "yauboo@now.net.cn"; // 发送人邮件地址
String to = "yauboo@now.net.cn"; // 接受人邮件地址
String subject = "Java Mail Subject!"; // 邮件标题
String body = "Java Mail Body!"; // 邮件内容


// 以下为发送程序,用户无需改动
try {
Properties props = new Properties();
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.auth","true");
Session ssn = Session.getInstance(props, null);

MimeMessage message = new MimeMessage(ssn);

InternetAddress fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);

message.setSubject(subject);
message.setText(body);

Transport transport = ssn.getTransport("smtp");
transport.connect(smtphost, user, password);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
//transport.send(message);
transport.close();
%><p>你的邮件已发送,请返回。</p><%
} catch(Exception m) {
out.println(m.toString());
}%>
</BODY>
</HTML>

<script type="text/javascript"> google_ad_client = "pub-2624228443779279"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_channel =""; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "000000"; google_color_url = "000000"; google_color_text = "666666"; </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭