Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
更新 DefaultMailSender.buildMessage(MailSenderConfig) 异常 message 信息 fix
Browse files Browse the repository at this point in the history
#51

InternetAddressUtil.buildFromAddress(String, String) 新增 非 blank 校验 fix
#50

InternetAddressUtil.buildFromAddress(String, String) 处理 checked
exception fix #49
  • Loading branch information
venusdrogon committed Apr 25, 2019
1 parent 63dfd02 commit b0a7704
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.feilong.net.mail.setter.HeaderSetter;
import com.feilong.net.mail.setter.RecipientsSetter;
import com.feilong.net.mail.util.InternetAddressUtil;
import com.feilong.tools.slf4j.Slf4jUtil;

/**
* 邮件发送器.
Expand Down Expand Up @@ -137,9 +138,9 @@ static Message buildMessage(MailSenderConfig mailSenderConfig){

try{
setMessageAttribute(message, mailSenderConfig);
}catch (UnsupportedEncodingException | MessagingException e){
LOGGER.error("", e);
throw new MailSenderException(e);
}catch (MessagingException e){
//since 1.13.2 update exception message
throw new MailSenderException(Slf4jUtil.format("mailSenderConfig:[{}]", JsonUtil.format(mailSenderConfig)), e);
}

return message;
Expand Down Expand Up @@ -242,8 +243,8 @@ static BodyPart buildHtmlContentBody(MailSenderConfig mailSenderConfig) throws M
* @see #setRecipients(Message, MailSenderConfig)
* @see #setHeaders(Message, MailSenderConfig)
*/
private static void setMessageAttribute(Message message,MailSenderConfig mailSenderConfig)
throws UnsupportedEncodingException,MessagingException{
private static void setMessageAttribute(Message message,MailSenderConfig mailSenderConfig) throws MessagingException{

message.setFrom(InternetAddressUtil.buildFromAddress(mailSenderConfig.getPersonal(), mailSenderConfig.getFromAddress()));

// 设置邮件接受人群
Expand All @@ -256,5 +257,6 @@ private static void setMessageAttribute(Message message,MailSenderConfig mailSen

//header信息
HeaderSetter.setHeaders(message, mailSenderConfig);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import static com.feilong.core.Validator.isNotNullOrEmpty;

import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;

import com.feilong.core.DefaultRuntimeException;
import com.feilong.net.mail.entity.MailSenderConfig;
import com.feilong.net.mail.util.InternetAddressUtil;
import com.feilong.tools.slf4j.Slf4jUtil;

/**
* The Class RecipientsSetter.
Expand All @@ -42,31 +45,38 @@ public final class RecipientsSetter{
* the message
* @param mailSenderConfig
* the new recipients
* @throws MessagingException
* the messaging exception
*/
public static void setRecipients(Message message,MailSenderConfig mailSenderConfig) throws MessagingException{
public static void setRecipients(Message message,MailSenderConfig mailSenderConfig){
// 创建邮件的接收者地址,并设置到邮件消息中
// Message.RecipientType.TO属性表示接收者的类型为TO
String[] tos = mailSenderConfig.getTos();
if (isNotNullOrEmpty(tos)){
message.setRecipients(Message.RecipientType.TO, InternetAddressUtil.toAddressArray(tos));
}

//---------------------------------------------------------------

set(message, Message.RecipientType.TO, mailSenderConfig.getTos());
//cc 抄送
String[] ccs = mailSenderConfig.getCcs();
if (isNotNullOrEmpty(ccs)){
message.setRecipients(Message.RecipientType.CC, InternetAddressUtil.toAddressArray(ccs));
}
set(message, Message.RecipientType.CC, mailSenderConfig.getCcs());
//bcc 密送
set(message, Message.RecipientType.BCC, mailSenderConfig.getBccs());
}

//---------------------------------------------------------------
//---------------------------------------------------------------

//bcc 密送
String[] bccs = mailSenderConfig.getBccs();
if (isNotNullOrEmpty(bccs)){
message.setRecipients(Message.RecipientType.BCC, InternetAddressUtil.toAddressArray(bccs));
/**
* 设置.
*
* @param message
* the message
* @param recipientType
* the recipient type
* @param addressArray
* the address array
*/
private static void set(Message message,RecipientType recipientType,String[] addressArray){
try{
if (isNotNullOrEmpty(addressArray)){
message.setRecipients(recipientType, InternetAddressUtil.toAddressArray(addressArray));
}
}catch (MessagingException e){
//since 1.13.2
throw new DefaultRuntimeException(Slf4jUtil.format("addressArray:[{}],recipientType:[{}]", addressArray, recipientType), e);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

import org.apache.commons.lang3.Validate;

import com.feilong.core.DefaultRuntimeException;
import com.feilong.tools.slf4j.Slf4jUtil;

/**
* The Class InternetAddressUtil.
*
Expand Down Expand Up @@ -145,25 +148,31 @@ public static final List<String> toUnicodeStringList(InternetAddress[] internetA
* the personal
* @param fromAddress
* the from address
* @return the address
* @throws UnsupportedEncodingException
* the unsupported encoding exception
* @throws AddressException
* the address exception
* @return 如果 <code>fromAddress</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>fromAddress</code> 是blank,抛出 {@link IllegalArgumentException}<br>
* @since 1.13.0
*/
public static Address buildFromAddress(String personal,String fromAddress) throws UnsupportedEncodingException,AddressException{
// 设置邮件消息的发送者
if (isNotNullOrEmpty(personal)){
//the encoding to be used. Currently supported values are "B" and "Q".
//If this parameter is null, then the "Q" encoding is used if most of characters to be encoded are in the ASCII charset,
//otherwise "B" encoding is used.
//B为base64方式
String encoding = "b";
String encodeText = MimeUtility.encodeText(personal, CHARSET_PERSONAL, encoding);
return new InternetAddress(fromAddress, encodeText);
public static Address buildFromAddress(String personal,String fromAddress){
Validate.notBlank(fromAddress, "fromAddress can't be blank!");

//---------------------------------------------------------------

try{
// 设置邮件消息的发送者
if (isNotNullOrEmpty(personal)){
//the encoding to be used. Currently supported values are "B" and "Q".
//If this parameter is null, then the "Q" encoding is used if most of characters to be encoded are in the ASCII charset,
//otherwise "B" encoding is used.
//B为base64方式
String encoding = "b";
String encodeText = MimeUtility.encodeText(personal, CHARSET_PERSONAL, encoding);
return new InternetAddress(fromAddress, encodeText);
}
return new InternetAddress(fromAddress);
}catch (AddressException | UnsupportedEncodingException e){
//since 1.13.2
throw new DefaultRuntimeException(Slf4jUtil.format("personal:[{}],fromAddress:[{}]", personal, fromAddress), e);
}
return new InternetAddress(fromAddress);
}

}

0 comments on commit b0a7704

Please sign in to comment.