发送邮件
准备
-
Java
应用程序发送E-mail
-
应先安装
JavaMail API
和Java Activation Framework (JAF)
-
Java
网站下载最新版本的 JavaMail,打开网页右侧有个 Downloads 链接,点击下载 -
Java
网站下载最新版本的 JAF(版本 1.1.1)
-
-
JavaMail
是 Java 处理电子邮件相关的编程接口-
没有被加在标准的
Java
开发工具包中 -
所以需要另外下载依赖包
-
javax.mail.jar
包实现了 SMTP、IMAP 和 POP3 协议
-
-
下载链接
-
-
下载并解压缩,在新创建的顶层目录中,发现两个应用程序的
jar
文件- 需要把
mail.jar
和activation.jar
文件添加到CLASSPATH
- 需要把
简单的 E-mail
- 发送简单E-mail的例子
- 假设本地主机已经连接到网络
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail{
public static void main(String [] args){
String to = "abcd@gmail.com"; // 收件人电子邮箱
String from = "web@gmail.com"; // 发件人电子邮箱
String host = "localhost"; // 指定发送邮件的主机为 localhost
Properties properties = System.getProperties(); // 获取系统属性
properties.setProperty("mail.smtp.host", host); // 设置邮件服务器
Session session = Session.getDefaultInstance(properties); // 获取默认session对象
try{
MimeMessage message = new MimeMessage(session); // 创建默认的 MimeMessage 对象
message.setFrom(new InternetAddress(from)); // Set From: 头部头字段
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set To: 头部头字段
message.setSubject("This is the Subject Line!"); // Set Subject: 头部头字段
message.setText("This is actual message"); // 设置消息体
Transport.send(message); // 发送消息
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
/* 发送一封e-mail给多个收件人,使用下面的方法指定多个收件人ID */
/**
* 添加多个接收人
* @param type 设置为 TO、CC 或 BCC;CC 代表抄送,BCC 代表秘密抄送
* 例如:Message.RecipientType.TO
* @param addresses email ID 的数组;指定电子邮件 ID 时,需要使用 InternetAddress() 方法
* @throws MessagingException
*/
void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException;
HTML E-mail
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendHTMLEmail{
public static void main(String [] args){
String to = "abcd@gmail.com"; // 收件人电子邮箱
String from = "web@gmail.com"; // 发件人电子邮箱
String host = "localhost"; // 指定发送邮件的主机为 localhost
Properties properties = System.getProperties(); // 获取系统属性
properties.setProperty("mail.smtp.host", host); // 设置邮件服务器
Session session = Session.getDefaultInstance(properties); // 获取默认的 Session 对象。
try{
MimeMessage message = new MimeMessage(session); // 创建默认的 MimeMessage 对象
message.setFrom(new InternetAddress(from)); // Set From: 头部头字段
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set To: 头部头字段
message.setSubject("This is the Subject Line!"); // Set Subject: 头字段
message.setContent("<h1>This is actual message</h1>", "text/html" ); // 发送 HTML 消息, 可以插入html标签
Transport.send(message); // 发送消息
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
带附件的 E-mail
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendFileEmail{
public static void main(String [] args){
String to = "abcd@gmail.com"; // 收件人电子邮箱
String from = "web@gmail.com"; // 发件人电子邮箱
String host = "localhost"; // 指定发送邮件的主机为 localhost
Properties properties = System.getProperties(); // 获取系统属性
properties.setProperty("mail.smtp.host", host); // 设置邮件服务器
Session session = Session.getDefaultInstance(properties); // 获取默认的 Session 对象
try{
MimeMessage message = new MimeMessage(session); // 创建默认的 MimeMessage 对象
message.setFrom(new InternetAddress(from)); // Set From: 头部头字段
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set To: 头部头字段
message.setSubject("This is the Subject Line!"); // Set Subject: 头字段
BodyPart messageBodyPart = new MimeBodyPart(); // 创建消息部分
messageBodyPart.setText("This is message body"); // 文本消息
Multipart multipart = new MimeMultipart(); // 创建多重消息对象
multipart.addBodyPart(messageBodyPart); // 设置文本消息部分
messageBodyPart = new MimeBodyPart(); // 附件部分
String filename = "file.txt"; // 文件名
DataSource source = new FileDataSource(filename); // 创建数据源对象
messageBodyPart.setDataHandler(new DataHandler(source)); // 添加文件
messageBodyPart.setFileName(filename); // 设置文件名
multipart.addBodyPart(messageBodyPart); // 添加文件消息到消息对象
message.setContent(multipart ); // 添加消息对象到邮件对象
Transport.send(message); // 发送消息
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
用户认证
要求
-
需要提供用户名和密码给
e-mail
服务器达到用户认证的目的-
ops.put("mail.smtp.auth", "true"); // 开启认证 props.setProperty("mail.user", "myuser"); // 添加账号 props.setProperty("mail.password", "mypwd"); // 添加密码
-
其他的发送机制和上述保持一致
-
实例
- 以 QQ 邮件服务器为例
- 登录 QQ邮箱 后台在 - 设置 - 账号中开启
POP3/SMTP
服务
- 登录 QQ邮箱 后台在 - 设置 - 账号中开启
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail2{
public static void main(String [] args){
String to = "xxx@qq.com"; // 收件人电子邮
String from = "xxx@qq.com"; // 发件人电子邮箱
String host = "smtp.qq.com"; // 指定发送邮件的主机为 smtp.qq.com,QQ 邮件服务器
Properties properties = System.getProperties(); // 获取系统属性
properties.setProperty("mail.smtp.host", host); // 设置邮件服务器
properties.put("mail.smtp.auth", "true"); // 开启认证
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("xxx@qq.com", "qq邮箱授权码"); //发件人邮件用户名、授权码
}
}); // 获取默认session对象,通过认证
try{
MimeMessage message = new MimeMessage(session); // 创建默认的 MimeMessage 对象
message.setFrom(new InternetAddress(from)); // Set From: 头部头字段
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set To: 头部头字段
message.setSubject("This is the Subject Line!"); // Set Subject: 头部头字段
message.setText("This is actual message"); // 设置消息体
Transport.send(message); // 发送消息
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}