发送电子邮件
思路与技术
要运行本实例首先应该构建JavaMail的开发环境,在JDK中配置JavaMail的相关类和包。
在构建JavaMail开发环境中,需要mail.jar和activation.jar这两个文件。这两个文件的获得可以通过SUN公司的官方网站下载。
代码如下;
<html>
<head>
<title>发送电子邮件</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" type="text/css" href="../image/com.css"/></head>
<body>
<form method="post" name="form1" action="sendEmail.jsp" onSubmit="return email()">
<table width="480" height="393" border="1" align="center" cellpadding="0" cellspacing="0">
<tr bordercolor="#D4D0C8" bgcolor="#FEFFC9">
<td height="27" colspan="2" align="left"><div align="center"><strong>发送电子邮件</strong></div></td>
</tr>
<tr bgcolor="#FEFFC9">
<td width="68" height="27" align="left"><div align="right">收件人:</div></td>
<td width="406" height="27"><div align="center">
<input type="text" name="sname" size="51">
</div></td>
</tr>
<tr bgcolor="#FEFFC9">
<td height="27" align="left"><div align="right">发件人:</div></td>
<td height="27"> <div align="center">
<input type="text" name="jname" size="51">
</div></td>
</tr>
<tr bgcolor="#FEFFC9">
<td height="27" align="left"><div align="right">密 码:</div></td>
<td height="27"><div align="center"><input type="password" name="password" size="57"></div></td>
</tr>
<tr bgcolor="#FEFFC9">
<td height="27" align="left"><div align="right">主 题:</div></td>
<td height="27"><div align="center">
<input name="title" type="text" size="51">
</div></td>
</tr>
<tr align="left" bgcolor="#FEFFC9">
<td height="227" valign="top"><div align="right">内 容:</div></td>
<td height="227"><div align="center">
<textarea name="message" cols="50" rows="15"></textarea>
</div></td>
</tr>
<tr align="center" valign="middle" bordercolor="#D4D0C8" bgcolor="#FEFFC9">
<td height="29" colspan="2"><input type="submit" name="Submit" value="发送">
<input type="reset" name="Submit2" value="清除"></td>
</tr>
</table>
</form>
</body>
</html>
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import ="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>发送邮件</title>
</head>
<link rel="stylesheet" type="text/css" href="../image/com.css"/>
<%
try{
request.setCharacterEncoding("gb2312");
String from=request.getParameter("jname");
String to=request.getParameter("sname");
String subject=request.getParameter("title");
String messageText=request.getParameter("message");
String password=request.getParameter("password");
String S =request.getParameter("jname");
int n =S.indexOf('@');
int m=S.length() ;
String server =S.substring(n+1,m);
//建立邮件会话
Properties pro=new Properties();
pro.put("mail.smtp.host","smtp."+server);
pro.put("mail.smtp.auth","true");
Session sess=Session.getInstance(pro);
sess.setDebug(true);
MimeMessage message=new MimeMessage(sess); //新建一个消息对象
//设置发件人
InternetAddress from_mail=new InternetAddress(from);
message.setFrom(from_mail);
//设置收件人
InternetAddress to_mail=new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO ,to_mail);
message.setSubject(subject); //设置主题
message.setText(messageText); //设置内容
message.setSentDate(new Date());//设置发送时间
//发送邮件
message.saveChanges(); //保存邮件信息
Transport transport =sess.getTransport("smtp");
transport.connect("smtp."+server,from,password);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
out.print("邮件发送成功");
}catch (Exception e){
out.print("发送邮件失败,原因可能是<ul>");
out.println(e.getMessage());
}
%>
</html>
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="../error/error.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>发送电子邮件</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" type="text/css" href="../image/com.css"/></head>
<body>
<form method="post" name="form1" action="sendEmail.jsp" onSubmit="return email()">
<table width="480" height="393" border="1" align="center" cellpadding="0" cellspacing="0">
<tr bordercolor="#D4D0C8" bgcolor="#FEFFC9">
<td height="27" colspan="2" align="left"><div align="center"><strong>发送电子邮件</strong></div></td>
</tr>
<tr bgcolor="#FEFFC9">
<td width="68" height="27" align="left"><div align="right">收件人:</div></td>
<td width="406" height="27"><div align="center">
<input type="text" name="sname" size="51">
</div></td>
</tr>
<tr bgcolor="#FEFFC9">
<td height="27" align="left"><div align="right">发件人:</div></td>
<td height="27"> <div align="center">
<input type="text" name="jname" size="51">
</div></td>
</tr>
<tr bgcolor="#FEFFC9">
<td height="27" align="left"><div align="right">密 码:</div></td>
<td height="27"><div align="center"><input type="password" name="password" size="57"></div></td>
</tr>
<tr bgcolor="#FEFFC9">
<td height="27" align="left"><div align="right">主 题:</div></td>
<td height="27"><div align="center">
<input name="title" type="text" size="51">
</div></td>
</tr>
<tr align="left" bgcolor="#FEFFC9">
<td height="227" valign="top"><div align="right">内 容:</div></td>
<td height="227"><div align="center">
<textarea name="message" cols="50" rows="15"></textarea>
</div></td>
</tr>
<tr align="center" valign="middle" bordercolor="#D4D0C8" bgcolor="#FEFFC9">
<td height="29" colspan="2"><input type="submit" name="Submit" value="发送">
<input type="reset" name="Submit2" value="清除"></td>
</tr>
</table>
</form>
</body>
</html>
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import ="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>发送邮件</title>
</head>
<link rel="stylesheet" type="text/css" href="../image/com.css"/>
<%
try{
request.setCharacterEncoding("gb2312");
String from=request.getParameter("jname");
String to=request.getParameter("sname");
String subject=request.getParameter("title");
String messageText=request.getParameter("message");
String password=request.getParameter("password");
String S =request.getParameter("jname");
int n =S.indexOf('@');
int m=S.length() ;
String server =S.substring(n+1,m);
//建立邮件会话
Properties pro=new Properties();
pro.put("mail.smtp.host","smtp."+server);
pro.put("mail.smtp.auth","true");
Session sess=Session.getInstance(pro);
sess.setDebug(true);
//新建一个消息对象
MimeMessage message=new MimeMessage(sess);
//设置发件人
InternetAddress from_mail=new InternetAddress(from);
message.setFrom(from_mail);
//设置收件人
InternetAddress to_mail=new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO ,to_mail);
//设置主题
message.setSubject(subject);
//设置内容
message.setText(messageText);
//设置发送时间
message.setSentDate(new Date());
//发送邮件
message.saveChanges(); //保存邮件信息
Transport transport =sess.getTransport("smtp");
transport.connect("smtp."+server,from,password);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
out.print("发送邮件成功");
}catch(Exception e){
out.print("发送邮件失败,原因可能是<ul>");
out.println(e.getMessage());
}
%>
</html>
body {
background-color: #FFFFCC;
}
body, td, th, h3, h6 {
color: #330000;
}
h1, h2, h4, h5 {
color: #660000;
}
a {
color: #003366;
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<display-name>WebRoot</display-name>
</web-app>