java mail说白了就是套模板,大框架。框架搭起来后邮件发送的内容就稍微变通一下,就和搭积木一样。
依赖的jar包
使用项目管理工具创建项目的话对应去搜就行了。
纯文本邮件
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;
public class Mail {
public static void main(String[] args) throws GeneralSecurityException, MessagingException {
Properties properties = new Properties();
properties.setProperty("mail.host","smtp.qq.com"); //设置qq邮件服务器
properties.setProperty("mail.transport.protocol","smtp"); //邮件发送协议
properties.setProperty("mail.smtp.auth","true"); //需要验证用户名和密码
//发送邮件的五个步骤
//1. 获取Session实例(定义整个程序所需环境信息,比如主机名、端口号、采用的邮件发送和接收协议)
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("2979746577@qq.com","jxitmoyolhrjdcdh");
}
});
session.setDebug(true); //开启debug模式
//2. 通过session获取transport(用来发送邮件)
Transport transport = session.getTransport();
//3. 使用邮箱的用户名和授权码连上邮件服务器
transport.connect("smtp.qq.com","2979746577@qq.com","jxitmoyolhrjdcdh");
//4. 创建邮件
MimeMessage msg = new MimeMessage(session);
//设置邮件的发送人
msg.setFrom(new InternetAddress("2979746577@qq.com"));
//设置邮件的接收人
msg.setRecipient(Message.RecipientType.TO,new InternetAddress("2979746577@qq.com"));
//设置邮件的主题
msg.setSubject("你好啊!java mail");
//设置邮件的内容
msg.setContent("<h1 style=’color=red‘>你好啊!希望我们能成为很好的朋友</h1>","text/html;charset=utf-8");
//5. 发送邮件
transport.sendMessage(msg, msg.getAllRecipients());
//6. 关闭连接
transport.close();
}
}
带附件的邮件
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;
public class MailPlus {
public static void main(String[] args) throws Exception{
Properties properties = new Properties();
properties.setProperty("mail.host","smtp.qq.com"); //设置qq邮件服务器
properties.setProperty("mail.transport.protocol","smtp"); //邮件发送协议
properties.setProperty("mail.smtp.auth","true"); //需要验证用户名和密码
//发送邮件的五个步骤
//1. 获取Session实例(定义整个程序所需环境信息,比如主机名、端口号、采用的邮件发送和接收协议)
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("2979746577@qq.com","jxitmoyolhrjdcdh");
}
});
session.setDebug(true); //开启debug模式
//2. 通过session获取transport(用来发送邮件)
Transport transport = session.getTransport();
//3. 使用邮箱的用户名和授权码连上邮件服务器
transport.connect("smtp.qq.com","2979746577@qq.com","jxitmoyolhrjdcdh");
//4. 创建邮件
MimeMessage msg = new MimeMessage(session);
//设置邮件的发送人
msg.setFrom(new InternetAddress("2979746577@qq.com"));
//设置邮件的接收人
msg.setRecipient(Message.RecipientType.TO,new InternetAddress("2979746577@qq.com"));
//设置邮件的主题
msg.setSubject("你好啊!java mail");
//=================================================================
//准备图片数据
MimeBodyPart image = new MimeBodyPart();
//图片需要经过数据处理
DataHandler handler = new DataHandler(new FileDataSource("3.png"));
image.setDataHandler(handler);
image.setContentID("bz.jpg"); //给图片设置一个id,附件是设置FileName
//准备正文数据
MimeBodyPart text = new MimeBodyPart();
text.setContent("这是邮件的正文,附带了一张图片<img src='cid:bz.jpg'","text/html;charset=utf-8");
//描述数据关系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(image);
mm.addBodyPart(text);
mm.setSubType("related");
//把编辑好的内容放到消息中,保存修改
msg.setContent(mm);
msg.saveChanges();
//=================================================================
//5. 发送邮件
transport.sendMessage(msg, msg.getAllRecipients());
//6. 关闭连接
transport.close();
}
}