mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
添加自定义属性,发送邮件可以配置其他属性,如mail.smtp.ssl.socketFactory
This commit is contained in:
parent
4762dae538
commit
764ba51715
@ -1,11 +1,14 @@
|
|||||||
package cn.hutool.extra.mail;
|
package cn.hutool.extra.mail;
|
||||||
|
|
||||||
import cn.hutool.core.util.CharsetUtil;
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.setting.Setting;
|
import cn.hutool.setting.Setting;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -36,6 +39,9 @@ public class MailAccount implements Serializable {
|
|||||||
|
|
||||||
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"};
|
||||||
|
|
||||||
|
// 允许使用自定义属性
|
||||||
|
private final Map<String, Object> customProperty = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SMTP服务器域名
|
* SMTP服务器域名
|
||||||
*/
|
*/
|
||||||
@ -467,6 +473,29 @@ public class MailAccount implements Serializable {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取自定义属性列表
|
||||||
|
*
|
||||||
|
* @return 自定义参数列表
|
||||||
|
*/
|
||||||
|
public Map<String, Object> getCustomProperty() {
|
||||||
|
return customProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置自定义属性,如mail.smtp.ssl.socketFactory
|
||||||
|
*
|
||||||
|
* @param key 属性名
|
||||||
|
* @param value 属性值
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public MailAccount setCustomProperty(String key, Object value) {
|
||||||
|
if (ObjectUtil.isNotNull(key) && ObjectUtil.isNotNull(value)) {
|
||||||
|
this.customProperty.put(key, value);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得SMTP相关信息
|
* 获得SMTP相关信息
|
||||||
*
|
*
|
||||||
@ -507,11 +536,16 @@ public class MailAccount implements Serializable {
|
|||||||
p.put(SOCKET_FACTORY_FALLBACK, String.valueOf(this.socketFactoryFallback));
|
p.put(SOCKET_FACTORY_FALLBACK, String.valueOf(this.socketFactoryFallback));
|
||||||
p.put(SOCKET_FACTORY_PORT, String.valueOf(this.socketFactoryPort));
|
p.put(SOCKET_FACTORY_PORT, String.valueOf(this.socketFactoryPort));
|
||||||
// issue#IZN95@Gitee,在Linux下需自定义SSL协议版本
|
// issue#IZN95@Gitee,在Linux下需自定义SSL协议版本
|
||||||
if(StrUtil.isNotBlank(this.sslProtocols)){
|
if (StrUtil.isNotBlank(this.sslProtocols)) {
|
||||||
p.put(SSL_PROTOCOLS, this.sslProtocols);
|
p.put(SSL_PROTOCOLS, this.sslProtocols);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 补充自定义属性,允许自定属性覆盖已经设置的值
|
||||||
|
for (String key : customProperty.keySet()) {
|
||||||
|
p.put(key, customProperty.get(key));
|
||||||
|
}
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,21 +1,55 @@
|
|||||||
package cn.hutool.extra.mail;
|
package cn.hutool.extra.mail;
|
||||||
|
|
||||||
|
import com.sun.mail.util.MailSSLSocketFactory;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.security.GeneralSecurityException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认邮件帐户设置测试
|
* 默认邮件帐户设置测试
|
||||||
* @author looly
|
|
||||||
*
|
*
|
||||||
|
* @author looly
|
||||||
*/
|
*/
|
||||||
public class MailAccountTest {
|
public class MailAccountTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void parseSettingTest() {
|
public void parseSettingTest() {
|
||||||
MailAccount account = GlobalMailAccount.INSTANCE.getAccount();
|
MailAccount account = GlobalMailAccount.INSTANCE.getAccount();
|
||||||
account.getSmtpProps();
|
account.getSmtpProps();
|
||||||
|
|
||||||
Assert.assertNotNull(account.getCharset());
|
Assert.assertNotNull(account.getCharset());
|
||||||
Assert.assertTrue(account.isSslEnable());
|
Assert.assertTrue(account.isSslEnable());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
测试案例:使用QQ邮箱、AOL邮箱,如果不改SocketFactory实例,会报错(unable to find valid certification path to requested target),
|
||||||
|
hutool mail中仅提供了'mail.smtp.socketFactory.class'属性,但是没提供'mail.smtp.ssl.socketFactory'属性
|
||||||
|
参见 com.sun.mail.util.SocketFetcher.getSocket(java.lang.String, int, java.util.Properties, java.lang.String, boolean)
|
||||||
|
|
||||||
|
已经测试通过
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void customPropertyTest() throws GeneralSecurityException {
|
||||||
|
MailAccount mailAccount = new MailAccount();
|
||||||
|
mailAccount.setFrom("xxx@xxx.com");
|
||||||
|
mailAccount.setPass("xxxxxx");
|
||||||
|
|
||||||
|
mailAccount.setHost("smtp.aol.com");
|
||||||
|
|
||||||
|
// 使用其他配置属性
|
||||||
|
MailSSLSocketFactory sf = new MailSSLSocketFactory();
|
||||||
|
sf.setTrustAllHosts(true);
|
||||||
|
mailAccount.setCustomProperty("mail.smtp.ssl.socketFactory", sf);
|
||||||
|
|
||||||
|
mailAccount.setAuth(true);
|
||||||
|
mailAccount.setSslEnable(true);
|
||||||
|
|
||||||
|
Mail mail = Mail.create(mailAccount).setTos("xx@xx.com");
|
||||||
|
mail.setTitle("邮箱验证");
|
||||||
|
mail.setContent("您的验证码是:<h3>2333</h3>");
|
||||||
|
mail.setHtml(true);
|
||||||
|
|
||||||
|
mail.send();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user