<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> </dependency>
package com.caissa.chador.statement;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import java.util.Properties;
/**
* Created by admin on 2019/2/12.
*/
public class sendMail {
public static void main(String[] args) throws Exception {
send();
}
/**
* 接收邮件
*/
public static void send() throws Exception {
/**
* 因为现在使用的是163邮箱 而163的 pop地址是 pop3.163.com 端口是 110
* 比如使用好未来企业邮箱 就需要换成 好未来邮箱的 pop服务器地址 pop.263.net 和 端口 110
*/
String servicePath = "smtp.126.com"; // 服务器地址
// 准备连接服务器的会话信息
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp"); // 使用pop3协议
props.setProperty("mail.host", servicePath); // pop3服务器
props.setProperty("mail.smtp.auth", "true");
// 创建Session实例对象
Session session = Session.getInstance(props);
session.setDebug(true);
Transport ts=session.getTransport();
ts.connect(servicePath,"username","password");
//创建邮件
Message message = createMixedMail(session);
//发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
/**
* @Method: createMixedMail
* @Description: 生成一封带附件和带图片的邮件*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createMixedMail(Session session) throws Exception {
//创建邮件
MimeMessage message = new MimeMessage(session);
//设置邮件的基本信息
message.setFrom(new InternetAddress("发送人username"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("接收人username"));
message.setSubject("带附件和带图片的的邮件");
//正文
MimeBodyPart text = new MimeBodyPart();
text.setContent("你好啊!!!!!!","text/html;charset=UTF-8");
//图片
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource("src\\3.jpg")));
image.setContentID("aaa.jpg");
//附件1
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src\\4.zip"));
attach.setDataHandler(dh);
attach.setFileName(dh.getName());
//附件2
MimeBodyPart attach2 = new MimeBodyPart();
DataHandler dh2 = new DataHandler(new FileDataSource("src\\波子.zip"));
attach2.setDataHandler(dh2);
attach2.setFileName(MimeUtility.encodeText(dh2.getName()));
//描述关系:正文和图片
MimeMultipart mp1 = new MimeMultipart();
mp1.addBodyPart(text);
mp1.addBodyPart(image);
mp1.setSubType("related");
//描述关系:正文和附件
MimeMultipart mp2 = new MimeMultipart();
mp2.addBodyPart(attach);
mp2.addBodyPart(attach2);
//代表正文的bodypart
MimeBodyPart content = new MimeBodyPart();
content.setContent(mp1);
mp2.addBodyPart(content);
mp2.setSubType("mixed");
message.setContent(mp2);
message.saveChanges();
// message.writeTo(new FileOutputStream("D:\\mail"));
//返回创建好的的邮件
return message;
}
}