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
長春語林科技 閱讀(4727)
評論(3) 編輯 收藏 所屬分類:
util