今天闲着无事 看了看用java发送邮件的相关知识

代码参考自<<精通Java Web整合开发(JSP+AJAX+Struts+Hibernate)>>(第2版)

不多说 先上图

聊聊javaMail_smtp

聊聊javaMail_javamail_02

聊聊javaMail_qq邮箱_03

所用jar包如下:

activation.jar

mail.jar

smartupload.jar

其中第一个jar包支持的是带附件的邮件

前台textMail.html代码如下

<html>
<head>
<title>发送文本型邮件</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>

<body>
<h2>发送文本型邮件</h2>
<hr>
<form name="form1" method="post" action="sendMail1.jsp">
SMTP服务器:<input type="text" id="SMTPHost" name="SMTPHost">如smtp.qq.com<br>
登录帐号:<input type="text" id="user" name="user">若为qq邮箱 只用写qq号即可 不用写@qq.com 其他邮箱服务器类似<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="发送">
<input type="reset" name="reset" value="重填">
</form>
</body>
</html>


sendMail1.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("对不起,邮件发送失败!");
}
%>


这里面调用的SendTextMail 代码如下


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 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);
}
}
public void setContent(String content) {
try{
//解决内容的中文问题
content = new String(content.getBytes("ISO8859-1"),"gb2312");
}catch(Exception ex){
ex.printStackTrace();
}
this.content = content;
}



public void setSubject(String subject) {
try{
//解决标题的中文问题
subject = new String(subject.getBytes("ISO8859-1"),"gb2312");
}catch(Exception ex){
ex.printStackTrace();
}
this.subject = subject;
}
//省略部分getset方法
}


注意 要开启发送方邮箱服务器的pop/smtp服务


(在qq邮箱中 设置如下 邮箱设置--账户)


聊聊javaMail_qq邮箱_04

至于带附件的邮件如何发送 大家可参考

<<精通Java Web整合开发(JSP+AJAX+Struts+Hibernate)>>(第2版)