早就想系统学习一下邮件的相关知识了,因为各种原因,一直拖到现在才从传智博客上下载了张老师的视频来看看学习。为了方便以后查阅,自己动手写了个例子记录下。
视频中张老师对邮件的相关基础理论讲得比较透彻,通俗易懂了,在这我也不写那些理论了。写这博文主要是方便以后直接看例子,唤醒这方面相关的知识点而已。
项目中使用的是官网上最新的java-mail.jar,JDK7。 java-mail.jar下载的官网:http://www.oracle.com/technetwork/java/index-138643.html。
注意:官网上说了,如果JDK版本低于JDK6的话,使用java-mail.jar时,还需要jaf.jar;如果是JDK6或更新版本就不需要了,因为JDK6以上已经拥有jaf.jar了。
简单文本邮件信息类
SimpleMailInfo.java
package com.fei;
public class SimpleMailInfo {
/**
* 发送方的smtp服务器名称
*/
private String host ;
/**
* 发送方的smtp服务器端口
*/
private String port;
/**
* 发送人的邮箱登录名
*/
private String loginName ;
/**
* 发送人的邮箱登录密码
*/
private String password;
/**
* 邮件的标题
*/
private String title;
/**
* 邮件的内容
*/
private String content;
/**
* 发件人邮箱地址
*/
private String fromAddress;
/**
* 接收人的邮箱地址
*/
private String toAddress;
public SimpleMailInfo() {
}
public SimpleMailInfo(String host, String port, String loginName,
String password, String title, String content, String fromAddress,
String toAddress) {
super();
this.host = host;
this.port = port;
this.loginName = loginName;
this.password = password;
this.title = title;
this.content = content;
this.fromAddress = fromAddress;
this.toAddress = toAddress;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
}
邮件发送帮助类
MailSenderHelper.java
package com.fei;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailSenderHelper {
/**
* 邮件内容以纯文本显示
*/
public static void sendTextMail(SimpleMailInfo simpleInfo)throws Exception{
//构建Session环境
Properties properties = new Properties();
properties.setProperty("mail.smtp.host", simpleInfo.getHost());
properties.setProperty("mail.smtp.port", simpleInfo.getPort());
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(properties);
session.setDebug(true);//为了在控制台看到输出详细的发送过程中的信息
//构建Message邮件
Message message =new MimeMessage(session);
message.setSubject(simpleInfo.getTitle());//标题
message.setText(simpleInfo.getContent());//内容
message.setFrom(new InternetAddress(simpleInfo.getFromAddress()));//发件人地址
message.setRecipient(RecipientType.TO, new InternetAddress(simpleInfo.getToAddress()));
//构建Transport,连接smtp服务器,并发送邮件
Transport transport = session.getTransport();
transport.connect(simpleInfo.getLoginName(), simpleInfo.getPassword());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
/**
* 邮件内容用HTML解析后才显示
*/
public static void sendHtmlMail(final SimpleMailInfo simpleInfo)throws Exception{
//构建Session环境
Properties properties = new Properties();
properties.setProperty("mail.smtp.host", simpleInfo.getHost());
properties.setProperty("mail.smtp.port", simpleInfo.getPort());
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(properties,new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(simpleInfo.getLoginName(),simpleInfo.getPassword());
}
});
session.setDebug(true);//为了在控制台看到输出详细的发送过程中的信息
//构建Message邮件
Message message =new MimeMessage(session);
message.setSubject(simpleInfo.getTitle());//标题
message.setContent(simpleInfo.getContent(), "text/html;charset=GBK");
message.setFrom(new InternetAddress(simpleInfo.getFromAddress()));//发件人地址
message.setRecipient(RecipientType.TO, new InternetAddress(simpleInfo.getToAddress()));
//构建Transport发送
Transport.send(message);
}
}
发送邮件测试类
SendSimpleMailTest.java
package com.fei;
/**
* 学习如何发送一封简单文本邮件
*
*发送方:xxxx@163.com
*接收方:xxxx@qq.com
*
*/
public class SendSimpleMailTest {
public static void main(String[] args) {
sendText();
sendHtml();
}
private static void sendText(){
SimpleMailInfo mailInfo = new SimpleMailInfo();
mailInfo.setHost("smtp.163.com");
mailInfo.setPort("25");
mailInfo.setLoginName("wei.jian.fei@163.com");
mailInfo.setPassword("****");
mailInfo.setTitle("今晚不见不散");
mailInfo.setContent("美女,今晚老地方,老时间,不见不散哦");
//这里不需要和登录人的邮箱地址一样,可以写别人的邮箱地址,冒充别人
mailInfo.setFromAddress("wei.jian.fei@163.com");
mailInfo.setToAddress("269941633@qq.com");
try {
MailSenderHelper.sendTextMail(mailInfo);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void sendHtml(){
SimpleMailInfo mailInfo = new SimpleMailInfo();
mailInfo.setHost("smtp.163.com");
mailInfo.setPort("25");
mailInfo.setLoginName("wei.jian.fei@163.com");
mailInfo.setPassword("***");
mailInfo.setTitle("我们约会吧");
mailInfo.setContent("<span style='color:red'>美女,今晚老地方,老时间,不见不散哦</span>");
//这里不需要和登录人的邮箱地址一样,可以写别人的邮箱地址,冒充别人
mailInfo.setFromAddress("wei.jian.fei@163.com");
mailInfo.setToAddress("269941633@qq.com");
try {
MailSenderHelper.sendHtmlMail(mailInfo);
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行后,控制台的部分信息:
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.163.com", port 25, isSSL false
220 gz-c-163smtp2.163.com SMTP Server for Netease [466] 9e06c2ef-c598-4e3a-b075-e2a2f184cbae
DEBUG SMTP: connected to host "smtp.163.com", port: 25
EHLO Jfwei
250-gz-c-163smtp2.163.com
250-mail
250-PIPELINING
250-8BITMIME
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250 STARTTLS 9e06c2ef-c598-4e3a-b075-e2a2f184cbae
DEBUG SMTP: Found extension "mail", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "STARTTLS", arg "9e06c2ef-c598-4e3a-b075-e2a2f184cbae"
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<wei.jian.fei@163.com>
250 2.0.0 OK 9e06c2ef-c598-4e3a-b075-e2a2f184cbae
RCPT TO:<269941633@qq.com>
250 2.0.0 OK 9e06c2ef-c598-4e3a-b075-e2a2f184cbae
DEBUG SMTP: Verified Addresses
DEBUG SMTP: 269941633@qq.com
DATA
354 Start mail input; end with <CRLF>.<CRLF> 9e06c2ef-c598-4e3a-b075-e2a2f184cbae
From: wei.jian.fei@163.com
To: 269941633@qq.com
Message-ID: <8713574.0.1405157151272.JavaMail.Administrator@Jfwei>
Subject: =?GBK?B?vfHN7bK7vPuyu8mi?=
MIME-Version: 1.0
Content-Type: text/plain; charset=GBK
Content-Transfer-Encoding: base64
w8DFrqOsvfHN7cDPtdi3vaOswM/Ksbzko6yyu7z7srvJosW2
.
250 2.0.0 OK 9e06c2ef-c598-4e3a-b075-e2a2f184cbae
QUIT
221 2.0.0 Bye 9e06c2ef-c598-4e3a-b075-e2a2f184cbae
如果懂如何在DOS下发送邮件的话,那么对上面控制台输出的信息也就能看懂了。呵呵。
我们再来看看,qq邮箱中,是否收到邮件了。
text纯文本显示模式的邮件
html模式发送的