添加自定义属性,发送邮件可以配置其他属性,如mail.smtp.ssl.socketFactory

This commit is contained in:
braycep
2021-04-22 16:42:39 +08:00
parent 4762dae538
commit 764ba51715
2 changed files with 72 additions and 4 deletions

View File

@@ -1,21 +1,55 @@
package cn.hutool.extra.mail;
import com.sun.mail.util.MailSSLSocketFactory;
import org.junit.Assert;
import org.junit.Test;
import java.security.GeneralSecurityException;
/**
* 默认邮件帐户设置测试
* @author looly
*
* @author looly
*/
public class MailAccountTest {
@Test
public void parseSettingTest() {
MailAccount account = GlobalMailAccount.INSTANCE.getAccount();
account.getSmtpProps();
Assert.assertNotNull(account.getCharset());
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();
}
}