spring发送邮件附件显示名中文乱码的解决

原效果:

处理代码

在程序启动时设置:

public static void main(String[] args) {
		//java mail发邮件是附件名过长默认会被截断,附件名显示【tcmime.29121.29517.50430.bin】,主动设为false可正常显示附件名
		System.setProperty("mail.mime.splitlongparameters", "false");
        SpringApplication.run(xxxApplication.class, args);
	}
MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");
和helper.addAttachment(MimeUtility.encodeWord(file.getFilename(),"utf-8","B"), file);

api解释

 /**
     * 发送一封带附件的模板邮件
     * @param receiverName      邮件接收者,多个接收者以逗号隔开
     * @param cc                抄送,多个接收者以逗号隔开
     * @param subject           主题
     * @param sendFileList      发送附件列表
     * @param templateName      模板名
     * @param model             传递到模板的值
     */
    public void sendAttachmentsTemplateMailMail(String receiverName,String cc, String subject, List<File> sendFileList,String templateName,Map<String, Object> model) {
       if(StringUtils.isEmpty(receiverName)){
           throw new RuntimeException("没有邮件接受者");
       }
        MimeMessage message ;
        try {
            String sender = environment.getProperty("spring.mail.username");
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");
            helper.setFrom(sender);
            helper.setTo(StringUtils.split(receiverName,","));
            helper.setSubject(subject);
            if(StringUtils.isNotEmpty(cc)){
                helper.setCc(StringUtils.split(cc,","));
            }
            //读取 html 模板
            Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);
            String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
            helper.setText(html,true);
            if(CollectionUtils.isNotEmpty(sendFileList)){
                for(File sendFile : sendFileList){
                    if(sendFile == null || !sendFile.exists()){
                        throw new FileNotFoundException(sendFile+" sendFile不存在");
                    }
                    FileSystemResource file = new FileSystemResource(sendFile);
                    System.out.println(file.getFilename());
                    helper.addAttachment(MimeUtility.encodeWord(file.getFilename(),"utf-8","B"), file);
                }
            }

        } catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        mailSender.send(message);
    }

 

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用 Spring Boot 的 JavaMailSender 来发送带附件的邮件。下面是一个简单的示例代码: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @SpringBootApplication public class EmailApplication { @Autowired private JavaMailSender javaMailSender; public static void main(String[] args) { SpringApplication.run(EmailApplication.class, args); } public void sendEmailWithAttachment() throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo("recipient@example.com"); helper.setSubject("邮件主题"); helper.setText("邮件内容"); FileSystemResource file = new FileSystemResource(new File("/path/to/attachment.txt")); helper.addAttachment("附件称.txt", file); javaMailSender.send(message); } } ``` 请注意替换示例代码中的收件人地址和附件路径。这里使用的是 `FileSystemResource` 类来加载文件作为附件,并通过 `addAttachment` 方法将附件添加到邮件中。 你可以将上述代码放在 Spring Boot 项目中的任何地方,并通过调用 `sendEmailWithAttachment` 方法来发送带附件的邮件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值