完整源代码如下:
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.util.*,javax.mail.*,javax.mail.internet.*"%>
<%
Properties props=new Properties();
props.put("mail.smtp.host","smtp.163.com");
props.put("mail.smtp.auth","true");
//设定发送邮件时需要进行身份验证
Session s=Session.getInstance(props);
MimeMessage message=new MimeMessage(s);
InternetAddress from=new InternetAddress("letsflytogether@163.com");
message.setFrom(from);
InternetAddress to=new InternetAddress("letsflytogether@163.com");
message.addRecipient(Message.RecipientType.TO,to);
message.setSubject("Test from JavaMail");
message.setText("Hello from JavaMail");
try{
Transport transport=s.getTransport("smtp");
transport.connect("smtp.163.com","letsflytogether","****");
transport.sendMessage(message,message.getAllRecipients());
//这句是必须的,我曾尝试用transport.send(message);,但没能成功。
transport.close();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>untitled</title>
</head>
<body>
<P>Mail has been sent.
<a href="http://mail.163.com">Check your Inbox</a></P>
<P>
<a href="sendmail.jsp">Click here to send another!</a>
</P>
</body>
</html>