---
title: "使用JavaMail发送文本型邮件"
author: "hulin"
date: "2008-07-04T18:05:18Z"
tags: []
categories: ["企业架构"]
slug: "javamail"
draft: false
lastmod: "2008-07-04T18:05:18Z"
id: 211744
url: "https://linres.iteye.com/blog/211744"
---

文本型邮件是应用是广泛的一种邮件类型，本实例将使用JavaMail发送文本型邮件。用户通过在邮件书写页面textMail.htm中填写表单，然后提交给邮件发送页面sendMail.jsp,由sendMail.jsp调用封装邮件发送功能的SendTextMail实现邮件的发送
    首先导入activation.jar和mail.jar 
 文本型邮件书写页面textMail.htm
   <html>
	<head>
		<title>发送文本型邮件</title>
	</head>
	<body>
		<h2>
			发送文本型邮件
		</h2>
		<hr>
		<form name="form1" method="post" action="sendMaill.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>
			邮件内容：
			<textarea id="content" name="content" rows="5" cols="40"></textarea>
			<br>
			<br>
			<input type="submit" name="submit" value="发送">
			&nbsp;
			<input type="reset" name="reset" value="重填">
		</form>
	</body>
</html>

封装发送文本型邮件功能的SendTextMail.java
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(content.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);
		}
	}
}
发送文本型邮件的Jsp页面sendMail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="test.SendTextMail"%>
<jsp:useBean id="mySend" class="test.SendTextMail"></jsp:useBean>
<jsp:setProperty property="*" name="mySend" />
<%
	boolean status = mySend.send();
	if (status) {
		out.println("恭喜你邮件发送成功");
	} else {
		out.println("对不起邮件发送失败");
	}
%>

  完成！！你可以到textMail.htm看效果了