引入pom文件
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
代码
public static void main(String[] args) throws EmailException {
//附件,可以定义多个附件对象
EmailAttachment attachment1 = new EmailAttachment();
attachment1.setPath("D:\\附件1.txt");
attachment1.setDisposition(EmailAttachment.ATTACHMENT);
attachment1.setDescription("附件1详情");
EmailAttachment attachment2 = new EmailAttachment();
attachment2.setPath("D:\\附件2.txt");
attachment2.setDisposition(EmailAttachment.ATTACHMENT);
attachment2.setDescription("附件2详情");
MultiPartEmail email = new MultiPartEmail();
email.setHostName("smtp.163.com"); //设置发送主机的服务器地址
email.addTo("收件人邮箱"); //设置收件人邮箱
email.setFrom("发件人邮箱"); //发件人邮箱
email.setAuthentication("用户名", "授权码");//设置发件人在邮件服务器上注册的用户名和密码
email.setCharset("utf-8"); //设置主题的字符集
email.setSubject("邮件主题"); //设置邮件的主题
email.setMsg("邮件正问呢"); //邮件正文内容
email.attach(attachment1); //添加附件1
email.attach(attachment2); //添加附件2
String send = email.send();
System.out.println(send);
}