邮件的发送和接收流程
发送方通过SMTP协议连接到网易的服务器,向网易的服务器发送一封邮件,网易收到后通过SMTP协议转交给qq的smtp服务器,qq的smtp服务器把邮件存储到这个接收者的账号中,接收者通过POP3协议连接qq的POP3服务器,POP3服务器把邮件发送给接收者。
加入依赖包。SUN公司提供的标准开发包。
一封简单邮件的发送:
package com.dongmu;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;
public class MailDemo01 {
public static void main(String[] args) {
//1、连接邮件服务器的参数配置
Properties props = new Properties();
//设置用户的认证方式
props.setProperty("mail.smpt.anth", "true");
//设置传输协议
props.setProperty("mail.transport.protocol", "smtp");
//设置发件人的SMTP服务器地址
props.setProperty("mail.host", "smtp.qq.com");
//QQ还有SSL加密
try {
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smpt.ss1.enable",true);
props.put("mail.smpt.ss1.socketFactory",sf);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
//创建Session环境
Session session = Session.getDefaultInstance(props, new Authenticator() {
//qq才有
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//发件人有户名,授权码
return new PasswordAuthentication("2645990605@qq.com","授权码");
}
});
Transport transport=null;
//开启session的Debug模式
session.setDebug(true);
//获取Transport对象
try {
transport = session.getTransport();
//使用邮箱的用户名和授权码连接服务器
transport.connect("smtp.qq.com","2645990605@qq.com","码");
//创建邮件
MimeMessage message = new MimeMessage(session);
//设置收件人信息
message.setRecipient(Message.RecipientType.TO,new InternetAddress("wydongmu@163.com"));
//设置发件人信息
//这里不能随便写
message.setFrom(new InternetAddress("2645990605@qq.com"));
//设置邮件主题
message.setSubject("简单的邮件");
//设置邮件内容
//下面两个谁在下面会发送哪个信息
message.setContent("只有文本!","text/html;charset=utf-8");
message.setText("你好,新的一年开开心心!","utf-8");
//发送邮件
transport.sendMessage(message,message.getAllRecipients());
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}finally {
if (transport!=null){
try {
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}
}
带有图片的邮件的发送
package com.dongmu;
import com.sun.mail.util.MailSSLSocketFactory;
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.security.GeneralSecurityException;
import java.util.Properties;
public class MailDemo02 {
public static void main(String[] args) {
//1、连接邮件服务器的参数配置
Properties props = new Properties();
//设置用户的认证方式
props.setProperty("mail.smpt.anth", "true");
//设置传输协议
props.setProperty("mail.transport.protocol", "smtp");
//设置发件人的SMTP服务器地址
props.setProperty("mail.host", "smtp.qq.com");
//QQ还有SSL加密
try {
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smpt.ss1.enable",true);
props.put("mail.smpt.ss1.socketFactory",sf);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
//创建Session环境
Session session = Session.getDefaultInstance(props, new Authenticator() {
//qq才有
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//发件人有户名,授权码
return new PasswordAuthentication("2645990605@qq.com","授权码");
}
});
Transport transport=null;
//开启session的Debug模式
session.setDebug(true);
//获取Transport对象
try {
transport = session.getTransport();
//使用邮箱的用户名和授权码连接服务器
transport.connect("smtp.qq.com","2645990605@qq.com","授权码");
//创建邮件
MimeMessage message = new MimeMessage(session);
//设置收件人信息
message.setRecipient(Message.RecipientType.TO,new InternetAddress("wydongmu@163.com"));
//设置发件人信息
//这里不能随便写
message.setFrom(new InternetAddress("2645990605@qq.com"));
//设置邮件主题
message.setSubject("带有图片的复杂邮件");
//准备图片数据
MimeBodyPart image = new MimeBodyPart();
DataHandler handler = new DataHandler(new FileDataSource("D:\\word文档\\PS素材\\nsn.jpg"));
image.setDataHandler(handler);
image.setContentID("dongmu.jpg");
//设置正文数据
//下面两个谁在下面会发送哪个信息
MimeBodyPart text = new MimeBodyPart();
text.setContent("不只有文本!<img src:'cid=dongmu.jpg'>的邮件","text/html;charset=utf-8");
//描述数据关系,把图片和正文数据先放在一个MimeMultipart里面
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related");
//把MimeMultipart添加到消息中,准备发送
message.setContent(mm);
message.saveChanges();
//发送邮件
transport.sendMessage(message,message.getAllRecipients());
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}finally {
if (transport!=null){
try {
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}
}
带有图片和附件的邮件的发送
package com.dongmu;
import com.sun.mail.util.MailSSLSocketFactory;
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.security.GeneralSecurityException;
import java.util.Properties;
public class MailDemo03 {
public static void main(String[] args) {
//1、连接邮件服务器的参数配置
Properties props = new Properties();
//设置用户的认证方式
props.setProperty("mail.smpt.anth", "true");
//设置传输协议
props.setProperty("mail.transport.protocol", "smtp");
//设置发件人的SMTP服务器地址
props.setProperty("mail.host", "smtp.qq.com");
//QQ还有SSL加密
try {
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smpt.ss1.enable",true);
props.put("mail.smpt.ss1.socketFactory",sf);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
//创建Session环境
Session session = Session.getDefaultInstance(props, new Authenticator() {
//qq才有
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//发件人有户名,授权码
return new PasswordAuthentication("2645990605@qq.com","授权码");
}
});
Transport transport=null;
//开启session的Debug模式
session.setDebug(true);
//获取Transport对象
try {
transport = session.getTransport();
//使用邮箱的用户名和授权码连接服务器
transport.connect("smtp.qq.com","2645990605@qq.com","授权码");
//创建邮件
MimeMessage message = new MimeMessage(session);
//设置收件人信息
message.setRecipient(Message.RecipientType.TO,new InternetAddress("wydongmu@163.com"));
//设置发件人信息
//这里不能随便写
message.setFrom(new InternetAddress("2645990605@qq.com"));
//设置邮件主题
message.setSubject("带有图片的复杂邮件");
//准备图片数据
MimeBodyPart image = new MimeBodyPart();
DataHandler handler = new DataHandler(new FileDataSource("D:\\word文档\\PS素材\\nsn.jpg"));
image.setDataHandler(handler);
image.setContentID("dongmu.jpg");
//下面这个可以设置邮件名
image.setFileName("dongmu.jpg");
//准备附件数据
MimeBodyPart attach = new MimeBodyPart();
DataHandler handler1 = new DataHandler(new FileDataSource("D:\\word文档\\文娱\\music\\那就好好告个别吧.m4a"));
attach.setDataHandler(handler1);
attach.setFileName("jl.m4a");
//设置正文数据
//下面两个谁在下面会发送哪个信息
MimeBodyPart text = new MimeBodyPart();
text.setContent("不只有文本,还有图片和附件!<img src:'cid=dongmu.jpg'>的邮件","text/html;charset=utf-8");
//描述数据关系,把图片和正文数据先放在一个MimeMultipart里面
MimeMultipart mm1 = new MimeMultipart();
mm1.addBodyPart(text);
mm1.addBodyPart(image);
mm1.setSubType("related");
//把拼装好的邮件设置为主体
MimeBodyPart mm11 = new MimeBodyPart();
mm11.setContent(mm1);
//继续进行拼接
MimeMultipart mm2 = new MimeMultipart();
mm2.addBodyPart(mm11);
mm2.addBodyPart(attach);
mm2.setSubType("mixed");
//把MimeMultipart添加到消息中,准备发送
message.setContent(mm2);
message.saveChanges();
//发送邮件
transport.sendMessage(message,message.getAllRecipients());
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}finally {
if (transport!=null){
try {
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}
}