spring boot 发送邮件 & 文件名过长出现乱码

package com.crossborderpayment.amazoncore.provider.utils;

import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.core.io.Resource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class EmailUtil {

    public static final String DEFALUT_ENCODING = "UTF-8";

    public static void main(String[] args) {

        JavaMailSenderImpl sender = initJavaMailSender();
        String[] ss = {"**qq.com"};

        try {
//            EmailUtil.sendTextWithHtml(sender, ss, "测试简单文本邮件发送! ", " <a href='http://work.dev.gomeplus.com/'>test</a>测试我的简单邮件发送机制!! ");
            EmailUtil.sendWithAttament(sender, ss, "测试邮件中上传附件!!", "<html><head></head><body><h1>你好:附件中有学习资料!</h1></body></html>", "xx.xls", "D:\\aa.xls");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 全部发送
     *
     * @param sender     配置
     * @param tos        目标邮箱数组
     * @param from
     * @param subject    标题
     * @param text       内容
     * @param imgId      图片ID
     * @param imgPath    图片地址
     * @param AttachName 附件名称
     * @param filePath   附件地址
     * @throws MessagingException
     */
    public static void sendWithAll(JavaMailSenderImpl sender, String[] tos, String from, String subject, String text,
                                   String imgId, String imgPath, String AttachName, String filePath) throws MessagingException, UnsupportedEncodingException {
        //联合 MimeUtility.encodeText(AttachName); 解决文件名称过长出现乱码问题
        System.setProperty("mail.mime.splitlongparameters", "false");
        MimeMessage mailMessage = sender.createMimeMessage();
        MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage, tos, sender.getUsername(), subject, text,
                true, true, DEFALUT_ENCODING);

        // 插入图片
        if (StringUtils.isNotEmpty(imgId) && StringUtils.isNotEmpty(imgPath)) {
            FileSystemResource img = new FileSystemResource(new File(imgPath));
            messageHelper.addInline(imgId, img);
        }

        // 插入附件
        if (StringUtils.isNotEmpty(AttachName) && StringUtils.isNotEmpty(filePath)) {
            FileSystemResource file = new FileSystemResource(new File(filePath));
            MimeUtility.encodeText(AttachName);
            messageHelper.addAttachment(AttachName, file);
        }
        // 发送邮件
        sender.send(mailMessage);

        System.out.println("邮件发送成功..");
    }

    private static MimeMessageHelper initMimeMessageHelper(MimeMessage mailMessage, String[] tos, String from,
                                                           String subject, String text) throws MessagingException {
        return initMimeMessageHelper(mailMessage, tos, from, subject, text, true, false, DEFALUT_ENCODING);
    }

    private static MimeMessageHelper initMimeMessageHelper(MimeMessage mailMessage, String[] tos, String from,
                                                           String subject, String text, boolean isHTML, boolean multipart, String encoding) throws MessagingException {
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, multipart, encoding);
        messageHelper.setTo(tos);
        messageHelper.setFrom(from);
        messageHelper.setSubject(subject);
        // true 表示启动HTML格式的邮件
        messageHelper.setText(text, isHTML);

        return messageHelper;
    }

    /**
     * 这个方法在实际应用中,springboot会通过在配置文件application.xml
     * 中加配置自动实例化JavaMailSenderImpl,本方法只是为了测试使用
     */
    public static JavaMailSenderImpl initJavaMailSender() {

        Properties properties = new Properties();
        properties.setProperty("mail.debug", "true");// 是否显示调试信息(可选)
        properties.setProperty("mail.smtp.starttls.enable", "false");
        properties.setProperty("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.smtp.auth", "true");
        properties.put(" mail.smtp.timeout ", " 25000 ");

        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        javaMailSender.setJavaMailProperties(properties);
        javaMailSender.setHost("smtp.exmail.qq.com");
        javaMailSender.setUsername("**@qq.com"); // s根据自己的情况,设置username
        javaMailSender.setPassword("****"); // 根据自己的情况, 设置password
        javaMailSender.setPort(465);
        javaMailSender.setDefaultEncoding("UTF-8");

        return javaMailSender;
    }
}


application.yml

    #发送邮件
    mail:
        host: smtp.exmail.qq.com
        username: **@qq.com
        password: **
        port: 465
        default-encoding: UTF-8
        protocol: smtp
        healthcheck:
                enabled: false
        properties:
          mail:
            debug: false
            smtp:
              socketFactory.class: javax.net.ssl.SSLSocketFactory
              auth: true
              timeout: 25000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值