需要引入的pom文件(maven仓库直接拿的)
<!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
<dependency >
<groupId >javax.mail </groupId >
<artifactId >mail </artifactId >
<version >1.4.7 </version >
</dependency >
工具类代码
package com.xx.email;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Properties;
public class SendEmailFJ {
/**
* 发送带附件的邮件
*
* @param receive 收件人
* @param subject 邮件主题
* @param msg 邮件内容
* @param filename 附件地址
* @return
* @throws
*/
public static boolean sendMail(String receive, String subject, String msg, String filename)
throws GeneralSecurityException {
//设置参数
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 连接协议
properties.put("mail.smtp.host", "");// 主机名 使用qq邮箱当作主机发送可选择
properties.put("mail.smtp.port", 465);// 端口号
properties.put("mail.smtp.auth", "true");//设置认证
properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.socketFactory", sf);
// 得到回话对象
Session session = Session.getInstance(properties);
try {
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 发件人
message.setFrom(new InternetAddress("xxxxxx@"));
// Set To: 收件人
String[] strings = receive.split(",");
//将收件人信息通过‘,’拆分多个邮件地址
InternetAddress[] internetAddresses = new InternetAddress[strings.length];
for (int i = 0; i < strings.length; i++) {
internetAddresses[i] = new InternetAddress(strings[i]);
}
//添加收件人信息
message.setRecipients(,internetAddresses);
// Set Subject: 主题文字
message.setSubject(subject);
// 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 消息
messageBodyPart.setText(msg);
// 创建多重消息
Multipart multipart = new MimeMultipart();
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
// 附件部分
messageBodyPart = new MimeBodyPart();
// 设置要发送附件的文件路径
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
// messageBodyPart.setFileName(filename);
// 处理附件名称中文(附带文件路径)乱码问题
messageBodyPart.setFileName(MimeUtility.encodeText(filename));
multipart.addBodyPart(messageBodyPart);
// 发送完整消息
message.setContent(multipart);
Transport transport = session.getTransport();
transport.connect("发送人的账户信息", "这里写入qq邮箱的授权码");
// 发送消息
transport.sendMessage(message,message.getAllRecipients());
transport.close();
System.out.println("发送成功!");
return true;
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return false;
}
}
测试代码
public static void main(String[] args) {
//发送多个联系人 以','隔开
String receive="xxxxxxxx@";
String subject ="邮件主题";
String msg ="邮件内容";
//附件路径
String filename ="D:\\test.png";
try {
SendEmailFJ.sendMail(receive, subject, msg, filename);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
}