jsp发送javamail的例子
时间:2007-01-12 来源:wuyan
<%@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.163.com"; // 发送邮件服务器
String user = "jianmin19831013"; // 邮件服务器登录用户名
String password = "19831013"; // 邮件服务器登录密码
String from = "[email protected]"; // 发送人邮件地址
String to = "[email protected]"; // 接受人邮件地址
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>
即可实现