java发送邮件包括解决附件中文名称过长乱码问题

package com.itsm.core.utils;

import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;

/**

  • Created by jc on 2018/10/11.
    */
    public class MailUtil {
    //private String uploadPath;
    //发件人地址
    //public static String senderAddress = "[email protected]";
    //收件人地址
    // public static String recipientAddress = "[email protected]";
    //发件人账户名
    // public static String senderAccount = "[email protected]";
    //发件人账户密码
    // public static String senderPassword = “xxx”;
    public static void sendMail(String senderAccount,String senderPassword,String senderAddress,String recipientAddress,String smtpHost,String localFileAddress,String mailTile,String mailBody) {
    //1、连接邮件服务器的参数配置附件名称过长乱码解决,关键词false
    System.setProperty(“mail.mime.splitlongparameters”,“false”);
    Properties props = new Properties();
    //设置用户的认证方式
    props.setProperty(“mail.smtp.auth”, “true”);
    //设置传输协议
    props.setProperty(“mail.transport.protocol”, “smtp”);
    //设置发件人的SMTP服务器地址
    props.setProperty(“mail.smtp.host”, smtpHost);
    //2、创建定义整个应用程序所需的环境信息的 Session 对象
    Session session = Session.getInstance(props);
    //设置调试信息在控制台打印出来
    session.setDebug(true);
    //3、创建邮件的实例对象
    Message msg = null;
    try {
    msg = getMimeMessage(session,senderAddress, recipientAddress,localFileAddress,mailTile,mailBody);
    //4、根据session对象获取邮件传输对象Transport
    Transport transport = session.getTransport();
    //设置发件人的账户名和密码
    transport.connect(senderAccount, senderPassword);
    //发送邮件,并发送到所有收件人地址,message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
    transport.sendMessage(msg,msg.getAllRecipients());
    transport.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    //如果只想发送给指定的人,可以如下写法
    //transport.sendMessage(msg, new Address[]{new InternetAddress("[email protected]")});
    //5、关闭邮件连接

    }

    /**

    • 获得创建一封邮件的实例对象

    • @param session

    • @return

    • @throws MessagingException

    • @throws AddressException
      /
      public static MimeMessage getMimeMessage(Session session,String senderAddress,String recipientAddress,String localFileAddress,String mailTile,String mailBody) throws Exception{
      //创建一封邮件的实例对象
      MimeMessage msg = new MimeMessage(session);
      //设置发件人地址
      msg.setFrom(new InternetAddress(senderAddress));
      /
      *

      //设置邮件的发送时间,默认立即发送
      msg.setSentDate(new Date());
      MimeBodyPart text = new MimeBodyPart();
      //设置邮件正文
      text.setContent(mailBody, “text/html;charset=utf-8”);
      MimeBodyPart attachment = new MimeBodyPart();
      // 读取本地文件
      DataHandler dh2 = new DataHandler(new FileDataSource(localFileAddress));
      // 将附件数据添加到"节点"
      attachment.setDataHandler(dh2);
      // 设置附件的文件名(需要编码)
      String filename = dh2.getName();
      filename = MimeUtility.encodeText(filename);
      attachment.setFileName(filename.trim());
      MimeMultipart mm = new MimeMultipart();
      mm.addBodyPart(text);
      mm.addBodyPart(attachment); // 如果有多个附件,可以创建多个多次添加
      mm.setSubType(“mixed”); // 混合关系
      msg.setContent(mm);
      return msg;
      }

}

猜你喜欢

转载自blog.csdn.net/qq_41793064/article/details/88963087