java sendmail 附件_java 发送邮件带附件生成.bin文件解决方案

import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.core.io.FileSystemResource;

import org.springframework.mail.javamail.JavaMailSenderImpl;

import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeUtility;

import java.io.File;

import java.nio.charset.StandardCharsets;

import java.util.Objects;

public class EmailSendBox {

private Logger logger = LoggerFactory.getLogger(this.getClass());

private JavaMailSenderImpl javaMailSender;

public EmailSendBox(JavaMailSenderImpl javaMailSender) {

this.javaMailSender = javaMailSender;

}

//后加的防止题目过长并且进行全局定义

static {

System.setProperty("mail.mime.splitlongparameters","false");

System.setProperty("mail.mime.charset","UTF-8");

}

/**

* @see #send

*/

public String SendHtml(String subject, String sendText, String receivers, String ccReceivers) {

return send(subject, sendText, true, receivers, ccReceivers, null);

}

/**

* @see #send

*/

public String SendHtml(String subject, String sendText, String receivers, String ccReceivers,

File[] attaches) {

return send(subject, sendText, true, receivers, ccReceivers, attaches);

}

/**

* @param subject 主题

* @param sendText 邮件正文

* @param isHtmlContent 是否html的正文

* @param receivers 收件人,只接收一个

* @param ccReceivers 抄送人,多个以comma(半角逗号,)分割,可以为null

* @param attaches 附件,可以为null

* @return success 代表成功;error 代表失败

*/

public String send(String subject, String sendText, boolean isHtmlContent, String receivers, String ccReceivers,

File[] attaches) {

try {

final MimeMessage message = javaMailSender.createMimeMessage();

MimeMessageHelper helper = new MimeMessageHelper(message, true, StandardCharsets.UTF_8.name());

// 发件人

helper.setFrom(Objects.requireNonNull(javaMailSender.getUsername()));

// 收件人

helper.setTo(receivers.split(","));

// 抄送人

if(StringUtils.isNotBlank(ccReceivers)) {

helper.setCc(ccReceivers.split(","));

}

// 邮件主题

helper.setSubject(subject);

// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件

// 添加邮件正文

helper.setText(sendText, isHtmlContent);

// 附件

if (attaches != null) {

for (File attach : attaches) {

if (attach != null) {

// helper.addAttachment(attach.getName(), new FileSystemResource(attach));//最开始写的这种没加encode的涉及到中文名的就容易出错

helper.addAttachment(MimeUtility.encodeWord(attach.getName()), new FileSystemResource(attach));

}

}

}

javaMailSender.send(message);

return "success";

} catch (Exception e) {

logger.error("mail {} send error", subject, e);

return "error";

}

}

}

网上有很多让加MimeUtility.encodeWord,和 System.setProperty("mail.mime.splitlongparameters","false");的,对于一些人的可能好用,但是我这里就不好用了,可能是未进行全局定义的缘故,所以在该方法中加入static方法块包裹它。可以解决附件由于名字过长且含有中文的造成形成.bin文件。

你可能感兴趣的:(java,sendmail,附件)