client
pub fn client(endpoint string) SmtpClient用途
客户端。
参数
endpointstring- endpoint(string)
返回值
SmtpClient — 该类型的值
#github.com/xulang-org/packages/smtp扩展包 `smtp #github.com/xulang-org/packages/smtp`,由模块 github.com/xulang-org/packages 提供。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
48_standalone_smtp/main.xu@main
smtp #github.com/xulang-org/packages/smtp
#errors
#strings
fn verify_smtp_pool_and_mail() {
println("--- 1. Testing SMTP Connection Pool & Transaction ---")
$p := smtp.open_pool("smtp://127.0.0.1:1025", 5)
// 1. Connection Reuse
fd1 := p.get_conn()
if fd1 < 0 {
println(" [FAIL] Failed to connect to SMTP server")
<<
}
println(" Borrow 1 fd: " + str(fd1))
p.put_conn(fd1)
fd2 := p.get_conn()
if fd2 < 0 {
println(" [FAIL] Failed to re-borrow SMTP connection")
<<
}
println(" Borrow 2 (after return) fd: " + str(fd2))
if fd1 == fd2 {
println(" [PASS] Connection reused successfully (fd " + str(fd1) + " == " + str(fd2) + ")")
} else {
println(" [WARN] Different fd: " + str(fd1) + " vs " + str(fd2))
}
// 2. Concurrency
fd3 := p.get_conn()
if fd3 < 0 {
println(" [FAIL] Failed to borrow concurrent connection")
<<
}
println(" Borrow 3 (concurrently) fd: " + str(fd3))
if fd2 != fd3 {
println(" [PASS] Concurrently allocated distinct fds (" + str(fd2) + " != " + str(fd3) + ")")
} else {
println(" [FAIL] Expected distinct fds for concurrent connections!")
}
p.put_conn(fd2)
p.put_conn(fd3)
// 3. Mail Sending Transaction
msg := smtp.Message{
from: "service-alert@xu.lang",
to: "admin@enterprise.internal",
subject: "Antigravity SMTP Driver Verification 2026",
body: "Hello! This is a real test email sent through Xu Language native RFC 5321 SMTP driver."
}
p.send_mail(msg) >>> (err) {
println(" [FAIL] Failed to send email: " + err.msg())
p.close()
<<
}
println(" [PASS] SMTP Mail transaction (MAIL FROM -> RCPT TO -> DATA) succeeded!")
p.close()
println(" [PASS] SMTP Pool closed cleanly.")
}
fn main() {
println("=== Xu SMTP Native Driver Verification ===\n")
verify_smtp_pool_and_mail()
println("\n[SMTP VERIFICATION 100% COMPLETED]")
}clientpub fn client(endpoint string) SmtpClient客户端。
endpoint stringSmtpClient — 该类型的值
poolpub fn pool(endpoint string, max_conns int) SmtpPool执行 pool 处理。
endpoint stringmax_conns intSmtpPool — 该类型的值
struct Messagepub struct Message {
pub from string
pub to string
pub subject string
pub body string
}承载 from、to、subject、body 等属性的结构体。
from stringto stringsubject stringbody stringstruct SmtpClientpub struct SmtpClient {
pub host string
pub port int
pub username string
pub password string
}Smtp 客户端会话实例,维护与服务端的连接并提供操作接口。
host stringport intusername stringpassword stringstruct SmtpResponsepub struct SmtpResponse {
pub code int
pub text string
}Smtp 服务端响应结果,包含状态码、响应头与正文数据。
code inttext stringread_responsepub fn read_response(fd int) SmtpResponse读取响应。失败时抛出错误,调用方需处理或继续传播。
fd intSmtpResponse — 该类型的值
new_clientpub fn new_client(endpoint string) SmtpClient构造 client。
endpoint stringSmtpClient — 该类型的值
(SmtpClient) connectpub fn (c SmtpClient) connect() int建立与目标服务端点的网络通信连接。失败时抛出错误,调用方需处理或继续传播。
c SmtpClientSmtpClient 实例;按值接收。int — 该类型的值
(SmtpClient) send_mail_on_fdpub fn (c SmtpClient) send_mail_on_fd(fd int, msg Message) boolSmtpClient 上的操作:执行 send mail on fd 操作。失败时抛出错误,调用方需处理或继续传播。
c SmtpClientSmtpClient 实例;按值接收。fd intmsg Messagebool — 是否成立
(SmtpClient) close_connpub fn (c SmtpClient) close_conn(fd int)SmtpClient 上的操作:关闭连接。
c SmtpClientSmtpClient 实例;按值接收。fd int不返回值。
struct SmtpPoolpub struct SmtpPool {
pub client SmtpClient
pub idle chan int
pub max_idle int
}Smtp 连接池管理器,并发安全地维护和调度复用套接字连接。
client SmtpClientidle chan intmax_idle intopen_poolpub fn open_pool(endpoint string, max_idle int) SmtpPool执行 open pool 操作。
endpoint stringmax_idle intSmtpPool — 该类型的值
(&SmtpPool) get_connpub fn (p &SmtpPool) get_conn() int&SmtpPool 上的操作:读取 conn。
p &SmtpPoolSmtpPool 实例;以只读借用传入,调用方仍持有实例。int — 该类型的值
(&SmtpPool) put_connpub fn (p &SmtpPool) put_conn(fd int)&SmtpPool 上的操作:执行 put conn 操作。
p &SmtpPoolSmtpPool 实例;以只读借用传入,调用方仍持有实例。fd int不返回值。
(&SmtpPool) send_mailpub fn (p &SmtpPool) send_mail(msg Message) bool&SmtpPool 上的操作:执行 send mail 操作。失败时抛出错误,调用方需处理或继续传播。
p &SmtpPoolSmtpPool 实例;以只读借用传入,调用方仍持有实例。msg Messagebool — 是否成立
(&SmtpPool) closepub fn (p &SmtpPool) close()&SmtpPool 上的操作:关闭资源。
p &SmtpPoolSmtpPool 实例;以只读借用传入,调用方仍持有实例。不返回值。
struct TlsClientpub struct TlsClient {
pub conn transport.Conn
pub host string
pub port int
}Smtp 客户端会话实例,维护与服务端的连接并提供操作接口。
conn transport.Connhost stringport inttls_connectpub fn tls_connect(endpoint string, default_port int) TlsClient执行 tls connect 操作。失败时抛出错误,调用方需处理或继续传播。
endpoint stringdefault_port intTlsClient — 该类型的值
tls_connect_configpub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClient执行 tls connect config 操作。失败时抛出错误,调用方需处理或继续传播。
endpoint stringdefault_port intcfg tls.ConfigTlsClient — 该类型的值
($&TlsClient) write_allpub fn (c $&TlsClient) write_all(data string) int$&TlsClient 上的操作:执行 write all 操作。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。data stringint — 该类型的值
($&TlsClient) readpub fn (c $&TlsClient) read(max int) string$&TlsClient 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。max intstring — 结果字符串
($&TlsClient) read_exactpub fn (c $&TlsClient) read_exact(need int) string$&TlsClient 上的操作:执行 read exact 操作。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。need intstring — 结果字符串
($&TlsClient) closepub fn (c $&TlsClient) close() int$&TlsClient 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值
($&TlsClient) set_deadlinepub fn (c $&TlsClient) set_deadline(t_ns int) int$&TlsClient 上的操作:设置 deadline。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
($&TlsClient) set_read_deadlinepub fn (c $&TlsClient) set_read_deadline(t_ns int) int$&TlsClient 上的操作:设置 read deadline。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
($&TlsClient) set_write_deadlinepub fn (c $&TlsClient) set_write_deadline(t_ns int) int$&TlsClient 上的操作:设置 write deadline。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值