Skip to content

Commit

Permalink
add param for mail
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Oct 10, 2018
1 parent 6049364 commit f222557
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
35 changes: 35 additions & 0 deletions hutool-extra/src/main/java/cn/hutool/extra/mail/MailAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class MailAccount implements Serializable {
private static final String SMTP_HOST = "mail.smtp.host";
private static final String SMTP_PORT = "mail.smtp.port";
private static final String SMTP_AUTH = "mail.smtp.auth";
private static final String SMTP_CONNECTION_TIMEOUT = "mail.smtp.connectiontimeout";
private static final String SMTP_TIMEOUT = "mail.smtp.timeout";

private static final String STARTTTLS_ENABLE = "mail.smtp.starttls.enable";
private static final String SOCKEY_FACTORY = "mail.smtp.socketFactory.class";
Expand Down Expand Up @@ -63,6 +65,11 @@ public class MailAccount implements Serializable {
private boolean socketFactoryFallback;
/** 指定的端口连接到在使用指定的套接字工厂。如果没有设置,将使用默认端口 */
private int socketFactoryPort = 465;

/** SMTP超时时长,单位毫秒,缺省值不超时 */
private long timeout;
/** Socket连接超时值,单位毫秒,缺省值不超时 */
private long connectionTimeout;

// -------------------------------------------------------------- Constructor start
/**
Expand Down Expand Up @@ -374,6 +381,28 @@ public MailAccount setSocketFactoryPort(int socketFactoryPort) {
this.socketFactoryPort = socketFactoryPort;
return this;
}

/**
* 设置SMTP超时时长,单位毫秒,缺省值不超时
* @param timeout SMTP超时时长,单位毫秒,缺省值不超时
* @return this
* @since 4.1.17
*/
public MailAccount setTimeout(long timeout) {
this.timeout = timeout;
return this;
}

/**
* 设置Socket连接超时值,单位毫秒,缺省值不超时
* @param connectionTimeout Socket连接超时值,单位毫秒,缺省值不超时
* @return this
* @since 4.1.17
*/
public MailAccount setConnectionTimeout(long connectionTimeout) {
this.connectionTimeout = connectionTimeout;
return this;
}

/**
* 获得SMTP相关信息
Expand All @@ -389,6 +418,12 @@ public Properties getSmtpProps() {
p.put(SMTP_HOST, this.host);
p.put(SMTP_PORT, String.valueOf(this.port));
p.put(SMTP_AUTH, String.valueOf(this.auth));
if(this.timeout > 0) {
p.put(SMTP_TIMEOUT, String.valueOf(this.timeout));
}
if(this.connectionTimeout > 0) {
p.put(SMTP_CONNECTION_TIMEOUT, String.valueOf(this.connectionTimeout));
}

p.put(MAIL_DEBUG, String.valueOf(this.debug));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void sendTest2() {
}

@Test
@Ignore
// @Ignore
public void sendHtmlTest() {
MailUtil.send("[email protected]", "测试", "<h1>邮件来自Hutool测试</h1>", true);
}
Expand Down
7 changes: 6 additions & 1 deletion hutool-extra/src/test/resources/config/mail.setting
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ socketFactoryFallback = true
socketFactoryPort = 465

# 对于超长参数是否切分为多份,默认为false(国内邮箱附件不支持切分的附件名)
splitlongparameters = false
splitlongparameters = false

# SMTP超时时长,单位毫秒,缺省值不超时
timeout = 0
# Socket连接超时值,单位毫秒,缺省值不超时
connectionTimeout = 0
7 changes: 6 additions & 1 deletion hutool-extra/src/test/resources/example/mail-example.setting
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ socketFactoryFallback = true
socketFactoryPort = 465

# 对于超长参数是否切分为多份,默认为false(国内邮箱附件不支持切分的附件名)
splitlongparameters = false
splitlongparameters = false

# SMTP超时时长,单位毫秒,缺省值不超时
timeout = 0
# Socket连接超时值,单位毫秒,缺省值不超时
connectionTimeout = 0

0 comments on commit f222557

Please sign in to comment.