MailUtil-Java发送邮件工具类

时间:2025-03-10 10:38:53
  • import org.;
  • import org.;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import .*;
  • import ;
  • import ;
  • import ;
  • import ;
  • /**
  • * @author
  • * @date 2022/8/11 11:43
  • */
  • public class MailUtil {
  • private static final Logger log = ();
  • private final static String SenderEmail = "tpai18522947960@";//开启授权码的邮箱
  • private final static String senderCode = "OAYMRZLWIYSWUYZD";//授权码
  • public static final String emailSMTPHost = "smtp.";//服务器地址
  • /**
  • * 发送邮件
  • *
  • * @param receiveMailAccount 收件人
  • * @param ccMailAccounts 抄送人
  • * @param bccMailAccount 密送人
  • * @param subject 主题
  • * @param content 内容
  • * @param filePath 附件路径
  • * @param fileName 附件名
  • */
  • public static void sendMail(String receiveMailAccount, List<String> ccMailAccounts, String bccMailAccount,
  • String subject, String content, String filePath, String fileName) {
  • try {
  • Properties props = new Properties();
  • ("", "smtp");// 使用的协议
  • ("", emailSMTPHost);// 发件人的邮箱的SMTP服务器地址
  • ("", "true");// 需要请求认证;
  • ("", "60000");
  • ("", "true");
  • Session session = (props);//得到会话对象实例
  • (true);//是否打印详细日志
  • MimeMessage message = createMimeMessage(session, receiveMailAccount, ccMailAccounts, bccMailAccount, subject, content, filePath, fileName);//获取邮件对象(封装了一个方法)
  • Transport transport = ();
  • (emailSMTPHost, "xxxx", senderCode);//连接发送人的邮箱账户
  • // 6. 发送邮件, 发到所有的收件地址, () 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
  • (message, ());
  • // 7. 关闭连接
  • ();
  • ("邮件发送成功");
  • } catch (Exception e) {
  • ("发送邮件失败");
  • }
  • }
  • /**
  • * 创建邮件
  • *
  • * @param session 会话
  • * @param receiveMailAccount 收件人
  • * @param ccMailAccounts 抄送人
  • * @param bccMailAccount 密送人
  • * @param subject 主题
  • * @param content 内容
  • * @param filePath 附件路径
  • * @param fileName 附件名
  • * @return
  • * @throws Exception
  • */
  • public static MimeMessage createMimeMessage(Session session, String receiveMailAccount, List<String> ccMailAccounts, String bccMailAccount,
  • String subject, String content, String filePath, String fileName) throws Exception {
  • // 1. 创建一封邮件
  • MimeMessage message = new MimeMessage(session);
  • // 2. From: 发件人
  • (new InternetAddress(SenderEmail, "发件人", "UTF-8"));
  • // 3. 设置收件人、抄送人、密送人
  • //:收件类型;:抄送类型;:密送类型
  • (, new InternetAddress(receiveMailAccount, "收件人", "UTF-8"));
  • (, new InternetAddress(getAddress(ccMailAccounts), "抄送人", "UTF-8"));
  • (, new InternetAddress(bccMailAccount, "密送人", "UTF-8"));
  • // 4. Subject: 邮件主题
  • (subject, "UTF-8");
  • // 5. Content: 邮件正文(可以使用html标签)
  • (content, "text/html;charset=UTF-8");
  • MimeMultipart multipart = new MimeMultipart();
  • MimeBodyPart file = new MimeBodyPart();
  • DataHandler handler = new DataHandler(new FileDataSource(filePath));
  • (handler);
  • //对文件名进行编码,防止出现乱码
  • String document = (fileName, "utf-8", "B");
  • (document);
  • (file);
  • (multipart);
  • // 6. 设置发件时间
  • (new Date());
  • // 7. 保存设置
  • ();
  • return message;
  • }
  • //邮箱地址转换
  • private static String getAddress(List<String> mailList) throws AddressException {
  • Address[] address = new InternetAddress[()];
  • for (int i = 0; i < (); i++) {
  • address[i] = new InternetAddress((i));
  • }
  • return (address);
  • }
  • }