类、方法,,,,
package test;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
public class SendTextMail {
String SMTPHost=""; //SMTP服务器
String user=""; //登录SMTP服务器的帐号
String password=""; //登录SMTP服务器的密码
String from =""; //发件人邮箱
String to =""; //收件人邮箱
String subject =""; //邮件标题
String content =""; //邮件内容
//无参数构造方法
public SendTextMail() {}
public String getContent() {
return content;
}
public void setContent(String content) {
try{
//解决内容的中文问题
content = new String(content.getBytes("ISO8859-1"),"gb2312");
}catch(Exception ex){
ex.printStackTrace();
}
this.content = content;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSMTPHost() {
return SMTPHost;
}
public void setSMTPHost(String host) {
SMTPHost = host;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
try{
//解决标题的中文问题
subject = new String(subject.getBytes("ISO8859-1"),"gb2312");
}catch(Exception ex){
ex.printStackTrace();
}
this.subject = subject;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
//发送邮件
public boolean send(){
//创建一个属性对象
Properties props = new Properties();
//指定SMTP服务器
props.put("mail.smtp.host", SMTPHost);
//指定是否需要SMTP验证
props.put("mail.smtp.auth", "true");
try{
//创建一个授权验证对象
SmtpAuth auth = new SmtpAuth();
auth.setAccount(user,password);
//创建一个Session对象
Session mailSession = Session.getDefaultInstance(props,auth);
mailSession.setDebug(true);
//创建一个Message对象
Message message=new MimeMessage(mailSession);
//指定发件人邮箱
message.setFrom(new InternetAddress(from));
//指定收件人邮箱
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
//指定邮件主题
message.setSubject(subject);
//指定邮件内容
message.setText(content);
//指定邮件发送日期
message.setSentDate(new Date());
//指定邮件优先级 1:紧急 3:普通 5:缓慢
message.setHeader("X-Priority","1");
message.saveChanges();
//创建一个Transport对象
Transport transport = mailSession.getTransport("smtp");
//连接SMTP服务器
transport.connect(SMTPHost, user, password);
//发送邮件
transport.sendMessage(message, message.getAllRecipients());
transport.close();
return true;
}catch(Exception ex){
ex.printStackTrace();
return false;
}
}
//定义一个SMTP授权验证类
static class SmtpAuth extends Authenticator{
String user,password;
//设置帐号信息
void setAccount(String user,String password){
this.user = user;
this.password = password;
}
//取得PasswordAuthentication对象
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(user,password);
}
}
}
普通页面,,,,,
<html>
<head>
<title>发送HTML型邮件</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<h2>发送HTML型邮件</h2>
<hr>
<form name="form1" method="post" action="sendMail2.jsp">
SMTP服务器:<input type="text" id="SMTPHost" name="SMTPHost"><br>
登录帐号:<input type="text" id="user" name="user"><br>
登录密码:<input type="password" id="password" name="password"><br>
<br>
发件人邮箱:<input type="text" id="from" name="from"><br>
收件人邮箱:<input type="text" id="to" name="to"><br>
邮件标题:<input type="text" id="subject" name="subject"><br>
邮件内容HTML代码:<textarea id="content" name="content" rows="5" cols="40"></textarea><br>
<br>
<input type="submit" name="submit" value="发送">
<input type="reset" name="reset" value="重填">
</form>
</body>
</html>
JSP页面,
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="test.SendTextMail"%>
<jsp:useBean id="mySend" class="test.SendTextMail"></jsp:useBean>
<jsp:setProperty name="mySend" property="*"/>
<%
//注意这里我们通过JavaBean的自省机制完成了对其属性的赋值
boolean status = mySend.send();
if (status){
out.println("恭喜您,邮件发送成功!");
}else{
out.println("对不起,邮件发送失败!");
}
%>