Class OpenEmail
java.lang.Object
cloud.opencode.base.email.OpenEmail
Email Utility Facade Class
邮件工具门面类
Unified entry point for email sending and receiving operations.
邮件发送和接收操作的统一入口。
Features | 主要功能:
- Send plain text emails - 发送纯文本邮件
- Send HTML emails - 发送HTML邮件
- Send template-based emails - 发送模板邮件
- Async sending with CompletableFuture - 异步发送
- Batch sending support - 批量发送支持
- Receive emails via IMAP/POP3 - 通过IMAP/POP3接收邮件
- Email query and filtering - 邮件查询和过滤
- Email management (mark read, delete, move) - 邮件管理(标记已读、删除、移动)
- Real-time monitoring (IMAP IDLE) - 实时监控(IMAP IDLE)
- Global configuration - 全局配置
Usage Examples | 使用示例:
// Configure
OpenEmail.configure(EmailConfig.builder()
.host("smtp.example.com")
.port(587)
.username("user@example.com")
.password("password")
.starttls(true)
.defaultFrom("noreply@example.com", "System")
.build());
// Send simple text email
OpenEmail.sendText("user@example.com", "Hello", "Hello World!");
// Send HTML email
OpenEmail.sendHtml("user@example.com", "Welcome",
"<h1>Welcome</h1><p>Thanks for signing up!</p>");
// Send template email
OpenEmail.sendTemplate("user@example.com", "Order Confirmation",
"order-confirm.html",
Map.of("orderNo", "12345", "amount", "99.00"));
// Send with builder
OpenEmail.send(OpenEmail.email()
.to("user@example.com")
.subject("Report")
.html("<p>See attached report</p>")
.attach(Path.of("report.pdf"))
.build());
// Async send
OpenEmail.sendAsync(email)
.thenRun(() -> log.info("Sent successfully"))
.exceptionally(e -> { log.error("Failed", e); return null; });
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Since:
- JDK 25, opencode-base-email V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic EmailConfig.Builderconfig()Create configuration builder 创建配置构建器static voidconfigure(EmailConfig config) Configure default email sender 配置默认邮件发送器static voidconfigure(EmailConfig config, cloud.opencode.base.email.internal.EmailSender sender) Configure with custom sender 使用自定义发送器配置static voidQuick configuration 快速配置static voidconfigureReceiver(EmailReceiveConfig config) Configure email receiver 配置邮件接收器static voidconfigureReceiver(EmailReceiveConfig config, cloud.opencode.base.email.internal.EmailReceiver receiver) Configure receiver with custom implementation 使用自定义实现配置接收器static voidconfigureReceiver(String host, String username, String password, boolean useImap) Quick receiver configuration 快速接收器配置static EmailIdleMonitorcreateMonitor(EmailListener listener) Create email monitor for real-time notifications 创建邮件监控器用于实时通知static EmailIdleMonitorcreateMonitor(String folder, EmailListener listener) Create email monitor for specific folder 为特定文件夹创建邮件监控器static voidDelete email 删除邮件static CompletableFuture<Void> deleteAsync(String messageId) Delete email asynchronously 异步删除邮件static voidDisable rate limiting 禁用频率限制static Email.Builderemail()Create email builder with default from address 创建带默认发件人的邮件构建器static voidConfigure rate limiter with default limits 使用默认限制配置频率限制器static voidenableRateLimiting(int maxPerMinute, int maxPerHour, int maxPerDay) Configure rate limiter with custom limits 使用自定义限制配置频率限制器static EmailConfigGet current configuration 获取当前配置static intgetMessageCount(String folder) Get message count in folder 获取文件夹中的消息数量Get remaining rate limit quota (global) 获取剩余频率限制配额(全局)getRateLimitQuota(String recipient) Get remaining rate limit quota for recipient 获取收件人的剩余频率限制配额static EmailReceiveConfigGet current receive configuration 获取当前接收配置static intgetUnreadCount(String folder) Get unread message count in folder 获取文件夹中的未读消息数量static booleanCheck if configured 检查是否已配置static booleanCheck if rate limiting is enabled 检查是否启用了频率限制static booleanCheck if receiver is configured 检查接收器是否已配置List available folders 列出可用文件夹static voidmarkAsRead(String messageId) Mark email as read 标记邮件为已读static CompletableFuture<Void> markAsReadAsync(String messageId) Mark email as read asynchronously 异步标记邮件为已读static voidmarkAsUnread(String messageId) Mark email as unread 标记邮件为未读static voidmoveToFolder(String messageId, String targetFolder) Move email to folder (IMAP only) 移动邮件到文件夹(仅IMAP)static EmailQuery.Builderquery()Create email query builder 创建邮件查询构建器static List<ReceivedEmail> receive(EmailQuery query) Receive emails with query 使用查询接收邮件static List<ReceivedEmail> Receive all emails from default folder 从默认文件夹接收所有邮件static CompletableFuture<List<ReceivedEmail>> receiveAsync(EmailQuery query) Receive emails asynchronously with query 使用查询异步接收邮件static CompletableFuture<List<ReceivedEmail>> receiveAsync(EmailQuery query, BiConsumer<List<ReceivedEmail>, Throwable> callback) Receive emails asynchronously with callback 使用回调异步接收邮件static ReceivedEmailreceiveById(String messageId) Receive email by message ID 通过消息ID接收邮件static EmailReceiveConfig.BuilderCreate receive configuration builder 创建接收配置构建器static List<ReceivedEmail> Receive unread emails from default folder 从默认文件夹接收未读邮件static CompletableFuture<List<ReceivedEmail>> Receive unread emails asynchronously 异步接收未读邮件static voidSend email 发送邮件static CompletableFuture<Void> sendAllAndWait(List<Email> emails) Send multiple emails and wait for completion 发送多封邮件并等待完成static List<CompletableFuture<Void>> sendAllAsync(List<Email> emails) Send multiple emails asynchronously 异步发送多封邮件static CompletableFuture<Void> Send email asynchronously 异步发送邮件static CompletableFuture<Void> sendAsync(Email email, BiConsumer<Email, Throwable> callback) Send email asynchronously with callback 使用回调异步发送邮件static BatchSendResultSend multiple emails using a single SMTP connection (batch mode) 使用单个SMTP连接批量发送邮件static voidSend HTML email 发送HTML邮件static CompletableFuture<Void> sendHtmlAsync(String to, String subject, String htmlContent) Send HTML email asynchronously 异步发送HTML邮件static voidSend template email 发送模板邮件static voidSend simple text email 发送简单文本邮件static CompletableFuture<Void> sendTextAsync(String to, String subject, String content) Send simple text email asynchronously 异步发送简单文本邮件static voidsendToMultiple(List<String> recipients, String subject, String content) Send to multiple recipients 发送给多个收件人static SendResultsendWithResult(Email email) Send email and return result with message ID 发送邮件并返回包含消息ID的结果static voidsetFlagged(String messageId, boolean flagged) Set email flagged status 设置邮件标记状态static voidsetTemplateEngine(cloud.opencode.base.email.internal.EmailTemplate engine) Set custom template engine 设置自定义模板引擎static voidshutdown()Shutdown all resources (call on application shutdown) 关闭所有资源(应用关闭时调用)static voidShutdown only receiver resources 仅关闭接收器资源static voidShutdown only sender resources 仅关闭发送器资源static ConnectionTestResultTest SMTP connection with current configuration 使用当前配置测试SMTP连接
-
Method Details
-
configure
Configure default email sender 配置默认邮件发送器- Parameters:
config- the email configuration | 邮件配置
-
configure
-
configure
public static void configure(EmailConfig config, cloud.opencode.base.email.internal.EmailSender sender) Configure with custom sender 使用自定义发送器配置- Parameters:
config- the configuration | 配置sender- the custom sender | 自定义发送器
-
setTemplateEngine
public static void setTemplateEngine(cloud.opencode.base.email.internal.EmailTemplate engine) Set custom template engine 设置自定义模板引擎- Parameters:
engine- the template engine | 模板引擎
-
enableRateLimiting
public static void enableRateLimiting()Configure rate limiter with default limits 使用默认限制配置频率限制器Default limits: 10/minute, 100/hour, 1000/day
-
enableRateLimiting
public static void enableRateLimiting(int maxPerMinute, int maxPerHour, int maxPerDay) Configure rate limiter with custom limits 使用自定义限制配置频率限制器- Parameters:
maxPerMinute- max emails per minute | 每分钟最大邮件数maxPerHour- max emails per hour | 每小时最大邮件数maxPerDay- max emails per day | 每天最大邮件数
-
disableRateLimiting
public static void disableRateLimiting()Disable rate limiting 禁用频率限制 -
isRateLimitingEnabled
public static boolean isRateLimitingEnabled()Check if rate limiting is enabled 检查是否启用了频率限制- Returns:
- true if enabled | 启用返回true
-
getRateLimitQuota
Get remaining rate limit quota (global) 获取剩余频率限制配额(全局)- Returns:
- the quota or null if rate limiting disabled | 配额,禁用时返回null
-
getRateLimitQuota
Get remaining rate limit quota for recipient 获取收件人的剩余频率限制配额- Parameters:
recipient- the recipient email | 收件人邮箱- Returns:
- the quota or null if rate limiting disabled | 配额,禁用时返回null
-
isConfigured
public static boolean isConfigured()Check if configured 检查是否已配置- Returns:
- true if configured | 已配置返回true
-
getConfig
Get current configuration 获取当前配置- Returns:
- the configuration | 配置
-
send
Send email 发送邮件- Parameters:
email- the email to send | 要发送的邮件- Throws:
EmailException- if sending fails | 发送失败时抛出
-
sendWithResult
Send email and return result with message ID 发送邮件并返回包含消息ID的结果- Parameters:
email- the email to send | 要发送的邮件- Returns:
- the send result containing message ID | 包含消息ID的发送结果
- Throws:
EmailException- if sending fails | 发送失败时抛出
-
sendText
-
sendHtml
-
sendTemplate
-
sendToMultiple
-
sendBatch
Send multiple emails using a single SMTP connection (batch mode) 使用单个SMTP连接批量发送邮件More efficient than sending individually when sending to multiple recipients.
当发送给多个收件人时,比逐个发送更高效。
- Parameters:
emails- the emails to send | 要发送的邮件列表- Returns:
- the batch result | 批量结果
-
testConnection
Test SMTP connection with current configuration 使用当前配置测试SMTP连接- Returns:
- the test result | 测试结果
-
sendAsync
Send email asynchronously 异步发送邮件- Parameters:
email- the email to send | 要发送的邮件- Returns:
- future that completes when sent | 发送完成时的future
-
sendAsync
Send email asynchronously with callback 使用回调异步发送邮件- Parameters:
email- the email to send | 要发送的邮件callback- callback on completion | 完成时的回调- Returns:
- future that completes when sent | 发送完成时的future
-
sendTextAsync
Send simple text email asynchronously 异步发送简单文本邮件- Parameters:
to- recipient email | 收件人邮箱subject- email subject | 邮件主题content- text content | 文本内容- Returns:
- future that completes when sent | 发送完成时的future
-
sendHtmlAsync
Send HTML email asynchronously 异步发送HTML邮件- Parameters:
to- recipient email | 收件人邮箱subject- email subject | 邮件主题htmlContent- HTML content | HTML内容- Returns:
- future that completes when sent | 发送完成时的future
-
sendAllAsync
Send multiple emails asynchronously 异步发送多封邮件- Parameters:
emails- the emails to send | 要发送的邮件列表- Returns:
- list of futures | future列表
-
sendAllAndWait
Send multiple emails and wait for completion 发送多封邮件并等待完成- Parameters:
emails- the emails to send | 要发送的邮件列表- Returns:
- future that completes when all sent | 全部发送完成时的future
-
configureReceiver
Configure email receiver 配置邮件接收器- Parameters:
config- the receive configuration | 接收配置
-
configureReceiver
-
configureReceiver
public static void configureReceiver(EmailReceiveConfig config, cloud.opencode.base.email.internal.EmailReceiver receiver) Configure receiver with custom implementation 使用自定义实现配置接收器- Parameters:
config- the configuration | 配置receiver- the custom receiver | 自定义接收器
-
isReceiverConfigured
public static boolean isReceiverConfigured()Check if receiver is configured 检查接收器是否已配置- Returns:
- true if configured | 已配置返回true
-
getReceiveConfig
Get current receive configuration 获取当前接收配置- Returns:
- the receive configuration | 接收配置
-
receiveUnread
Receive unread emails from default folder 从默认文件夹接收未读邮件- Returns:
- list of received emails | 接收到的邮件列表
-
receive
Receive emails with query 使用查询接收邮件- Parameters:
query- the email query | 邮件查询- Returns:
- list of received emails | 接收到的邮件列表
-
receiveAll
Receive all emails from default folder 从默认文件夹接收所有邮件- Returns:
- list of received emails | 接收到的邮件列表
-
receiveById
Receive email by message ID 通过消息ID接收邮件- Parameters:
messageId- the message ID | 消息ID- Returns:
- the received email or null | 接收到的邮件或null
-
getMessageCount
Get message count in folder 获取文件夹中的消息数量- Parameters:
folder- the folder name | 文件夹名称- Returns:
- the message count | 消息数量
-
getUnreadCount
Get unread message count in folder 获取文件夹中的未读消息数量- Parameters:
folder- the folder name | 文件夹名称- Returns:
- the unread count | 未读数量
-
markAsRead
Mark email as read 标记邮件为已读- Parameters:
messageId- the message ID | 消息ID
-
markAsUnread
Mark email as unread 标记邮件为未读- Parameters:
messageId- the message ID | 消息ID
-
setFlagged
Set email flagged status 设置邮件标记状态- Parameters:
messageId- the message ID | 消息IDflagged- true to flag, false to unflag | true为标记,false为取消标记
-
delete
Delete email 删除邮件- Parameters:
messageId- the message ID | 消息ID
-
moveToFolder
-
listFolders
-
receiveUnreadAsync
Receive unread emails asynchronously 异步接收未读邮件- Returns:
- future containing received emails | 包含接收邮件的future
-
receiveAsync
Receive emails asynchronously with query 使用查询异步接收邮件- Parameters:
query- the email query | 邮件查询- Returns:
- future containing received emails | 包含接收邮件的future
-
receiveAsync
public static CompletableFuture<List<ReceivedEmail>> receiveAsync(EmailQuery query, BiConsumer<List<ReceivedEmail>, Throwable> callback) Receive emails asynchronously with callback 使用回调异步接收邮件- Parameters:
query- the email query | 邮件查询callback- callback on completion | 完成时的回调- Returns:
- future containing received emails | 包含接收邮件的future
-
markAsReadAsync
Mark email as read asynchronously 异步标记邮件为已读- Parameters:
messageId- the message ID | 消息ID- Returns:
- future that completes when done | 完成时的future
-
deleteAsync
Delete email asynchronously 异步删除邮件- Parameters:
messageId- the message ID | 消息ID- Returns:
- future that completes when done | 完成时的future
-
createMonitor
Create email monitor for real-time notifications 创建邮件监控器用于实时通知- Parameters:
listener- the email listener | 邮件监听器- Returns:
- the monitor | 监控器
-
createMonitor
Create email monitor for specific folder 为特定文件夹创建邮件监控器- Parameters:
folder- the folder to monitor | 要监控的文件夹listener- the email listener | 邮件监听器- Returns:
- the monitor | 监控器
-
query
Create email query builder 创建邮件查询构建器- Returns:
- the query builder | 查询构建器
-
receiveConfig
Create receive configuration builder 创建接收配置构建器- Returns:
- the receive config builder | 接收配置构建器
-
email
Create email builder with default from address 创建带默认发件人的邮件构建器- Returns:
- the email builder | 邮件构建器
-
config
Create configuration builder 创建配置构建器- Returns:
- the config builder | 配置构建器
-
shutdown
public static void shutdown()Shutdown all resources (call on application shutdown) 关闭所有资源(应用关闭时调用) -
shutdownSender
public static void shutdownSender()Shutdown only sender resources 仅关闭发送器资源 -
shutdownReceiver
public static void shutdownReceiver()Shutdown only receiver resources 仅关闭接收器资源
-