Record Class EmailConfig
java.lang.Object
java.lang.Record
cloud.opencode.base.email.EmailConfig
public record EmailConfig(String host, int port, String username, String password, String oauth2Token, boolean ssl, boolean starttls, String defaultFrom, String defaultFromName, Duration timeout, Duration connectionTimeout, int maxRetries, int poolSize, Duration poolIdleTimeout, boolean debug, DkimConfig dkim)
extends Record
Email Configuration Record
邮件配置记录
Immutable configuration for email sending.
邮件发送的不可变配置。
Features | 主要功能:
- SMTP/SMTPS configuration - SMTP/SMTPS配置
- SSL/TLS support - SSL/TLS支持
- OAuth2 authentication (Gmail, Outlook) - OAuth2认证
- Connection pool settings - 连接池设置
- Retry configuration - 重试配置
- DKIM signing support - DKIM签名支持
Usage Examples | 使用示例:
// Basic configuration
EmailConfig config = EmailConfig.builder()
.host("smtp.example.com")
.port(587)
.username("user@example.com")
.password("password")
.starttls(true)
.defaultFrom("noreply@example.com", "System")
.build();
// SSL configuration (port 465)
EmailConfig config = EmailConfig.builder()
.host("smtp.example.com")
.port(465)
.ssl(true)
.username("user")
.password("pass")
.build();
// OAuth2 configuration (Gmail)
EmailConfig config = EmailConfig.builder()
.host("smtp.gmail.com")
.port(587)
.username("user@gmail.com")
.oauth2Token(accessToken)
.starttls(true)
.build();
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
password/oauth2Tokenare stored asString, which the JVM cannot clear (interning + immutability). Heap dumps (post-mortem, hot dumps fromjcmd GC.heap_dump, or container OOM dumps) expose the credential verbatim. After authentication completes, callers retaining a long-lived configuration object should callwithCleared()to obtain a redacted copy and drop the original reference, bounding the retention window. The credential will still be visible on the wire (SMTP/IMAP authentication is not E2E) and in any GC-rooted intermediate buffers inside the Mail/Net stack, so this is mitigation, not elimination. A futurechar[]-throughout redesign is tracked for V1.0.5+.password与oauth2Token以String存储,JVM 无法清零(intern + 不可变)。 堆 dump(事后、jcmd GC.heap_dump热 dump、容器 OOM dump)会原文暴露凭证。 认证完成后,长期持有配置对象的调用方应调withCleared()取脱敏副本并释放原引用, 缩短凭证驻留窗口。凭证在 SMTP/IMAP 认证线缆上、Mail/Net 栈中间缓冲里仍可见,所以这只是 缓解而非消除;char[]全链路重设计列入 V1.0.5+ 跟踪。
- Since:
- JDK 25, opencode-base-email V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classEmail Configuration Builder 邮件配置构建器 -
Constructor Summary
ConstructorsConstructorDescriptionEmailConfig(String host, int port, String username, String password, String oauth2Token, boolean ssl, boolean starttls, String defaultFrom, String defaultFromName, Duration timeout, Duration connectionTimeout, int maxRetries, int poolSize, Duration poolIdleTimeout, boolean debug, DkimConfig dkim) Creates an instance of aEmailConfigrecord class. -
Method Summary
Modifier and TypeMethodDescriptionstatic EmailConfig.Builderbuilder()Create a new builder 创建新的构建器Returns the value of theconnectionTimeoutrecord component.booleandebug()Returns the value of thedebugrecord component.Returns the value of thedefaultFromrecord component.Returns the value of thedefaultFromNamerecord component.dkim()Returns the value of thedkimrecord component.final booleanIndicates whether some other object is "equal to" this one.booleanhasDkim()Check if DKIM signing is configured 检查是否配置了DKIM签名final inthashCode()Returns a hash code value for this object.booleanCheck if OAuth2 authentication is configured 检查是否配置了OAuth2认证host()Returns the value of thehostrecord component.intReturns the value of themaxRetriesrecord component.Returns the value of theoauth2Tokenrecord component.password()Returns the value of thepasswordrecord component.Returns the value of thepoolIdleTimeoutrecord component.intpoolSize()Returns the value of thepoolSizerecord component.intport()Returns the value of theportrecord component.booleanCheck if authentication is required 检查是否需要认证booleanssl()Returns the value of thesslrecord component.booleanstarttls()Returns the value of thestarttlsrecord component.timeout()Returns the value of thetimeoutrecord component.toString()Return string representation with masked sensitive fields 返回屏蔽敏感字段的字符串表示username()Returns the value of theusernamerecord component.Returns a copy of this configuration with credential fields (passwordandoauth2Token) cleared tonull.
-
Constructor Details
-
EmailConfig
public EmailConfig(String host, int port, String username, String password, String oauth2Token, boolean ssl, boolean starttls, String defaultFrom, String defaultFromName, Duration timeout, Duration connectionTimeout, int maxRetries, int poolSize, Duration poolIdleTimeout, boolean debug, DkimConfig dkim) Creates an instance of aEmailConfigrecord class.- Parameters:
host- the value for thehostrecord componentport- the value for theportrecord componentusername- the value for theusernamerecord componentpassword- the value for thepasswordrecord componentoauth2Token- the value for theoauth2Tokenrecord componentssl- the value for thesslrecord componentstarttls- the value for thestarttlsrecord componentdefaultFrom- the value for thedefaultFromrecord componentdefaultFromName- the value for thedefaultFromNamerecord componenttimeout- the value for thetimeoutrecord componentconnectionTimeout- the value for theconnectionTimeoutrecord componentmaxRetries- the value for themaxRetriesrecord componentpoolSize- the value for thepoolSizerecord componentpoolIdleTimeout- the value for thepoolIdleTimeoutrecord componentdebug- the value for thedebugrecord componentdkim- the value for thedkimrecord component
-
-
Method Details
-
builder
-
requiresAuth
public boolean requiresAuth()Check if authentication is required 检查是否需要认证- Returns:
- true if authentication is required | 需要认证返回true
-
hasOAuth2
public boolean hasOAuth2()Check if OAuth2 authentication is configured 检查是否配置了OAuth2认证- Returns:
- true if OAuth2 is configured | 配置了OAuth2返回true
-
hasDkim
public boolean hasDkim()Check if DKIM signing is configured 检查是否配置了DKIM签名- Returns:
- true if DKIM is configured | 配置了DKIM返回true
-
withCleared
Returns a copy of this configuration with credential fields (passwordandoauth2Token) cleared tonull. Use after theSmtpEmailSender(or other consumer) has completed authentication, then drop the original reference, to bound the heap-dump exposure window for the credential. See the class-level Security note for the threat model and limits of this mitigation. 返回一份将凭证字段(password与oauth2Token)置null的配置副本。 在SmtpEmailSender(或其他消费者)完成认证后调用, 并释放原引用,以缩短凭证在堆 dump 中暴露的窗口。威胁模型与缓解边界详见类级安全说明。- Returns:
- a new
EmailConfigwith credentials nulled out | 凭证置空的新EmailConfig - Since:
- opencode-base-email V1.0.4
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
host
Returns the value of thehostrecord component.- Returns:
- the value of the
hostrecord component
-
port
public int port()Returns the value of theportrecord component.- Returns:
- the value of the
portrecord component
-
username
Returns the value of theusernamerecord component.- Returns:
- the value of the
usernamerecord component
-
password
Returns the value of thepasswordrecord component.- Returns:
- the value of the
passwordrecord component
-
oauth2Token
Returns the value of theoauth2Tokenrecord component.- Returns:
- the value of the
oauth2Tokenrecord component
-
ssl
public boolean ssl()Returns the value of thesslrecord component.- Returns:
- the value of the
sslrecord component
-
starttls
public boolean starttls()Returns the value of thestarttlsrecord component.- Returns:
- the value of the
starttlsrecord component
-
defaultFrom
Returns the value of thedefaultFromrecord component.- Returns:
- the value of the
defaultFromrecord component
-
defaultFromName
Returns the value of thedefaultFromNamerecord component.- Returns:
- the value of the
defaultFromNamerecord component
-
timeout
Returns the value of thetimeoutrecord component.- Returns:
- the value of the
timeoutrecord component
-
connectionTimeout
Returns the value of theconnectionTimeoutrecord component.- Returns:
- the value of the
connectionTimeoutrecord component
-
maxRetries
public int maxRetries()Returns the value of themaxRetriesrecord component.- Returns:
- the value of the
maxRetriesrecord component
-
poolSize
public int poolSize()Returns the value of thepoolSizerecord component.- Returns:
- the value of the
poolSizerecord component
-
poolIdleTimeout
Returns the value of thepoolIdleTimeoutrecord component.- Returns:
- the value of the
poolIdleTimeoutrecord component
-
debug
public boolean debug()Returns the value of thedebugrecord component.- Returns:
- the value of the
debugrecord component
-
dkim
Returns the value of thedkimrecord component.- Returns:
- the value of the
dkimrecord component
-