charset null use default

This commit is contained in:
Looly 2021-10-24 20:10:19 +08:00
parent d571567c52
commit 7abb318e31
4 changed files with 44 additions and 23 deletions

View File

@ -3,12 +3,13 @@
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.7.16 (2021-10-23) # 5.7.16 (2021-10-24)
### 🐣新特性 ### 🐣新特性
* 【core 】 增加DateTime.toLocalDateTime * 【core 】 增加DateTime.toLocalDateTime
* 【core 】 CharSequenceUtil增加normalize方法pr#444@Gitee * 【core 】 CharSequenceUtil增加normalize方法pr#444@Gitee
* 【core 】 MailAccount增加setEncodefilename()方法可选是否编码附件的文件名issue#I4F160@Gitee * 【core 】 MailAccount增加setEncodefilename()方法可选是否编码附件的文件名issue#I4F160@Gitee
* 【core 】 MailAccount中charset增加null时的默认规则
### 🐞Bug修复 ### 🐞Bug修复

View File

@ -43,7 +43,7 @@ public class InternalMailUtil {
* 解析第一个地址 * 解析第一个地址
* *
* @param address 地址字符串 * @param address 地址字符串
* @param charset 编码 * @param charset 编码{@code null}表示使用系统属性定义的编码或系统编码
* @return 地址列表 * @return 地址列表
*/ */
public static InternetAddress parseFirstAddress(String address, Charset charset) { public static InternetAddress parseFirstAddress(String address, Charset charset) {
@ -63,7 +63,7 @@ public class InternalMailUtil {
* 地址间使用" "","";"分隔 * 地址间使用" "","";"分隔
* *
* @param address 地址字符串 * @param address 地址字符串
* @param charset 编码 * @param charset 编码{@code null}表示使用系统属性定义的编码或系统编码
* @return 地址列表 * @return 地址列表
*/ */
public static InternetAddress[] parseAddress(String address, Charset charset) { public static InternetAddress[] parseAddress(String address, Charset charset) {
@ -75,9 +75,10 @@ public class InternalMailUtil {
} }
//编码用户名 //编码用户名
if (ArrayUtil.isNotEmpty(addresses)) { if (ArrayUtil.isNotEmpty(addresses)) {
final String charsetStr = null == charset ? null : charset.name();
for (InternetAddress internetAddress : addresses) { for (InternetAddress internetAddress : addresses) {
try { try {
internetAddress.setPersonal(internetAddress.getPersonal(), charset.name()); internetAddress.setPersonal(internetAddress.getPersonal(), charsetStr);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new MailException(e); throw new MailException(e);
} }

View File

@ -21,6 +21,7 @@ import javax.mail.Transport;
import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.mail.util.ByteArrayDataSource; import javax.mail.util.ByteArrayDataSource;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -264,7 +265,7 @@ public class Mail implements Builder<MimeMessage> {
bodyPart = new MimeBodyPart(); bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(attachment)); bodyPart.setDataHandler(new DataHandler(attachment));
nameEncoded = attachment.getName(); nameEncoded = attachment.getName();
if(this.mailAccount.isEncodefilename()){ if (this.mailAccount.isEncodefilename()) {
nameEncoded = InternalMailUtil.encodeText(nameEncoded, charset); nameEncoded = InternalMailUtil.encodeText(nameEncoded, charset);
} }
// 普通附件文件名 // 普通附件文件名
@ -388,7 +389,7 @@ public class Mail implements Builder<MimeMessage> {
try { try {
return doSend(); return doSend();
} catch (MessagingException e) { } catch (MessagingException e) {
if(e instanceof SendFailedException){ if (e instanceof SendFailedException) {
// 当地址无效时显示更加详细的无效地址信息 // 当地址无效时显示更加详细的无效地址信息
final Address[] invalidAddresses = ((SendFailedException) e).getInvalidAddresses(); final Address[] invalidAddresses = ((SendFailedException) e).getInvalidAddresses();
final String msg = StrUtil.format("Invalid Addresses: {}", ArrayUtil.toString(invalidAddresses)); final String msg = StrUtil.format("Invalid Addresses: {}", ArrayUtil.toString(invalidAddresses));
@ -430,7 +431,7 @@ public class Mail implements Builder<MimeMessage> {
msg.setFrom(InternalMailUtil.parseFirstAddress(from, charset)); msg.setFrom(InternalMailUtil.parseFirstAddress(from, charset));
} }
// 标题 // 标题
msg.setSubject(this.title, charset.name()); msg.setSubject(this.title, (null == charset) ? null : charset.name());
// 发送时间 // 发送时间
msg.setSentDate(new Date()); msg.setSentDate(new Date());
// 内容和附件 // 内容和附件
@ -456,14 +457,15 @@ public class Mail implements Builder<MimeMessage> {
/** /**
* 构建邮件信息主体 * 构建邮件信息主体
* *
* @param charset 编码 * @param charset 编码{@code null}则使用{@link MimeUtility#getDefaultJavaCharset()}
* @return 邮件信息主体 * @return 邮件信息主体
* @throws MessagingException 消息异常 * @throws MessagingException 消息异常
*/ */
private Multipart buildContent(Charset charset) throws MessagingException { private Multipart buildContent(Charset charset) throws MessagingException {
final String charsetStr = null != charset ? charset.name() : MimeUtility.getDefaultJavaCharset();
// 正文 // 正文
final MimeBodyPart body = new MimeBodyPart(); final MimeBodyPart body = new MimeBodyPart();
body.setContent(content, StrUtil.format("text/{}; charset={}", isHtml ? "html" : "plain", charset)); body.setContent(content, StrUtil.format("text/{}; charset={}", isHtml ? "html" : "plain", charsetStr));
this.multipart.addBodyPart(body); this.multipart.addBodyPart(body);
return this.multipart; return this.multipart;
@ -478,7 +480,7 @@ public class Mail implements Builder<MimeMessage> {
private Session getSession() { private Session getSession() {
final Session session = MailUtil.getSession(this.mailAccount, this.useGlobalSession); final Session session = MailUtil.getSession(this.mailAccount, this.useGlobalSession);
if(null != this.debugOutput){ if (null != this.debugOutput) {
session.setDebugOut(debugOutput); session.setDebugOut(debugOutput);
} }

View File

@ -34,10 +34,13 @@ public class MailAccount implements Serializable {
private static final String SOCKET_FACTORY_FALLBACK = "mail.smtp.socketFactory.fallback"; private static final String SOCKET_FACTORY_FALLBACK = "mail.smtp.socketFactory.fallback";
private static final String SOCKET_FACTORY_PORT = "smtp.socketFactory.port"; private static final String SOCKET_FACTORY_PORT = "smtp.socketFactory.port";
// System Properties
private static final String SPLIT_LONG_PARAMS = "mail.mime.splitlongparameters";
//private static final String ENCODE_FILE_NAME = "mail.mime.encodefilename";
//private static final String CHARSET = "mail.mime.charset";
// 其他 // 其他
private static final String MAIL_DEBUG = "mail.debug"; private static final String MAIL_DEBUG = "mail.debug";
private static final String SPLIT_LONG_PARAMS = "mail.mime.splitlongparameters";
// private static final String ENCODE_FILE_NAME = "mail.mime.encodefilename";
public static final String[] MAIL_SETTING_PATHS = new String[]{"config/mail.setting", "config/mailAccount.setting", "mail.setting"}; public static final String[] MAIL_SETTING_PATHS = new String[]{"config/mail.setting", "config/mailAccount.setting", "mail.setting"};
@ -303,16 +306,19 @@ public class MailAccount implements Serializable {
/** /**
* 获取字符集编码 * 获取字符集编码
* *
* @return 编码 * @return 编码可能为{@code null}
*/ */
public Charset getCharset() { public Charset getCharset() {
return charset; return charset;
} }
/** /**
* 设置字符集编码 * 设置字符集编码此选项不会修改全局配置若修改全局配置请设置此项为{@code null}并设置
* <pre>
* System.setProperty("mail.mime.charset", charset);
* </pre>
* *
* @param charset 字符集编码 * @param charset 字符集编码{@code null} 则表示使用全局设置的默认编码全局编码为mail.mime.charset系统属性
* @return this * @return this
*/ */
public MailAccount setCharset(Charset charset) { public MailAccount setCharset(Charset charset) {
@ -330,7 +336,11 @@ public class MailAccount implements Serializable {
} }
/** /**
* 设置对于超长参数是否切分为多份默认为false国内邮箱附件不支持切分的附件名 * 设置对于超长参数是否切分为多份默认为false国内邮箱附件不支持切分的附件名<br>
* 注意此项为全局设置此项会调用
* <pre>
* System.setProperty("mail.mime.splitlongparameters", true)
* </pre>
* *
* @param splitlongparameters 对于超长参数是否切分为多份 * @param splitlongparameters 对于超长参数是否切分为多份
*/ */
@ -345,11 +355,17 @@ public class MailAccount implements Serializable {
* @since 5.7.16 * @since 5.7.16
*/ */
public boolean isEncodefilename() { public boolean isEncodefilename() {
return encodefilename; return encodefilename;
} }
/** /**
* 设置对于文件名是否使用{@link #charset}编码 * 设置对于文件名是否使用{@link #charset}编码此选项不会修改全局配置<br>
* 如果此选项设置为{@code false}则是否编码取决于两个系统属性
* <ul>
* <li>mail.mime.encodefilename 是否编码附件文件名</li>
* <li>mail.mime.charset 编码文件名的编码</li>
* </ul>
* *
* @param encodefilename 对于文件名是否使用{@link #charset}编码 * @param encodefilename 对于文件名是否使用{@link #charset}编码
* @since 5.7.16 * @since 5.7.16
@ -400,6 +416,7 @@ public class MailAccount implements Serializable {
/** /**
* 获取SSL协议多个协议用空格分隔 * 获取SSL协议多个协议用空格分隔
*
* @return SSL协议多个协议用空格分隔 * @return SSL协议多个协议用空格分隔
* @since 5.5.7 * @since 5.5.7
*/ */