Browse Source

add method for #1335

Looly 5 years ago
parent
commit
648ba17107
2 changed files with 26 additions and 1 deletions
  1. 1 0
      CHANGELOG.md
  2. 25 1
      hutool-extra/src/main/java/cn/hutool/extra/mail/Mail.java

+ 1 - 0
CHANGELOG.md

@@ -7,6 +7,7 @@
 
 ### 新特性
 * 【core   】     手机号工具类 座机正则表达式统一管理(pr#243@Gitee)
+* 【extra  】     Mail增加setDebugOutput方法(issue#1335@Gitee)
 
 ### Bug修复
 * 【core   】     修复ZipUtil.unzip从流解压关闭问题(issue#I2B0S1@Gitee)

+ 25 - 1
hutool-extra/src/main/java/cn/hutool/extra/mail/Mail.java

@@ -25,6 +25,7 @@ import javax.mail.util.ByteArrayDataSource;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.PrintStream;
 import java.nio.charset.Charset;
 import java.util.Date;
 
@@ -78,6 +79,11 @@ public class Mail {
 	private boolean useGlobalSession = false;
 
 	/**
+	 * debug输出位置,可以自定义debug日志
+	 */
+	private PrintStream debugOutput;
+
+	/**
 	 * 创建邮件客户端
 	 *
 	 * @param mailAccount 邮件帐号
@@ -345,6 +351,18 @@ public class Mail {
 		this.useGlobalSession = isUseGlobalSession;
 		return this;
 	}
+
+	/**
+	 * 设置debug输出位置,可以自定义debug日志
+	 *
+	 * @param debugOutput debug输出位置
+	 * @return this
+	 * @since 5.5.6
+	 */
+	public Mail setDebugOutput(PrintStream debugOutput) {
+		this.debugOutput = debugOutput;
+		return this;
+	}
 	// --------------------------------------------------------------- Getters and Setters end
 
 	/**
@@ -453,8 +471,14 @@ public class Mail {
 			authenticator = new UserPassAuthenticator(mailAccount.getUser(), mailAccount.getPass());
 		}
 
-		return isSingleton ? Session.getDefaultInstance(mailAccount.getSmtpProps(), authenticator) //
+		final Session session = isSingleton ? Session.getDefaultInstance(mailAccount.getSmtpProps(), authenticator) //
 				: Session.getInstance(mailAccount.getSmtpProps(), authenticator);
+
+		if(null != this.debugOutput){
+			session.setDebugOut(debugOutput);
+		}
+
+		return session;
 	}
 	// --------------------------------------------------------------- Private method end
 }