1.index1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>发送文本型文件</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="javamail,keyword2,keyword3">
<meta http-equiv="description" content="send mail use javamail">
<meta http-equiv="content-type" content="text/html;charset="utf-8">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h2>
<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>
发件人邮箱:<input type="text" id="from" name="from"><br>
收件人邮箱:<input type="text" id="to" name="to"><br>
邮件主题:<input type="text" id="subject" name="subject"><br>
邮件内容:<textarea rows="5" cols="40" name="content"></textarea><br><br>
<input type="submit" name="submit" value="发送">
<input type="reset" name="reset" value="重置">
</form>
</h2>
</body>
</html>
2.SendHtmlMail.java
package com.lhb.mail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendHtmlMail {
String SMTPHost="";
String user="";
String password="";
String from="";
String to="";
String subject="";
String content="";
public SendHtmlMail(){
}
public String getSMTPHost() {
return SMTPHost;
}
public void setSMTPHost(String host) {
SMTPHost = host;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
try {
subject=new String(subject.getBytes("ISO8859-1"),"utf-8");
} catch (Exception e) {
e.printStackTrace();
}
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
try {
content=new String(content.getBytes("ISO8859-1"),"utf-8");
} catch (Exception e) {
e.printStackTrace();
}
this.content = content;
}
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);
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);
//指定邮箱内容及ContentType和编码方式
message.setContent(content, "text/html;charset=utf-8");
//指定邮件发送日期
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 e) {
e.printStackTrace();
return false;
}
}
}
3.SmtpAuth.java
package com.lhb.mail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SmtpAuth extends Authenticator {
String user,password;
//设置账号信息
void setAccount(String user,String password){
this.user=user;
this.password=password;
}
//取得PsswordAuthentication对象
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(user,password);
}
}
4.sendMail2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.lhb.mail.SendHtmlMail"%>
<jsp:useBean id="mySend" class="com.lhb.mail.SendHtmlMail"></jsp:useBean>
<jsp:setProperty name="mySend" property="*"/>
<%
boolean status=mySend.send();
if(status){
out.println("邮件发送成功");
}
else
{
out.println("邮件发送失败");
}
%>
posted on 2008-05-23 10:21
长春语林科技 阅读(4723)
评论(3) 编辑 收藏 所属分类:
util