XU XUv0.16.0

#github.com/xulang-org/packages/smtp

导入方式: smtp #github.com/xulang-org/packages/smtp

公开 API: 7 函数, 14 方法, 5 类型, 0 常量, 0 方法集

扩展包 `smtp #github.com/xulang-org/packages/smtp`,由模块 github.com/xulang-org/packages 提供。

github.com/xulang-org/packages · 1.17.2 · 安装与使用

示例代码 · API 声明

示例代码

以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。

48_standalone_smtp/main.xu

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@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]")
}

API 声明

client

XuAPI
pub fn client(endpoint string) SmtpClient

用途

客户端。

参数

endpoint string
endpoint(string)

返回值

SmtpClient — 该类型的值

pool

XuAPI
pub fn pool(endpoint string, max_conns int) SmtpPool

用途

执行 pool 处理。

参数

endpoint string
endpoint(string)
max_conns int
max_conns(int)

返回值

SmtpPool — 该类型的值

struct Message

XuAPI
pub struct Message {
    pub from string
    pub to string
    pub subject string
    pub body string
}

用途

承载 from、to、subject、body 等属性的结构体。

公开字段

from string
字段(string)
to string
字段(string)
subject string
字段(string)
body string
正文

查看用法示例

struct SmtpClient

XuAPI
pub struct SmtpClient {
    pub host string
    pub port int
    pub username string
    pub password string
}

用途

Smtp 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

host string
主机
port int
端口
username string
字段(string)
password string
字段(string)

struct SmtpResponse

XuAPI
pub struct SmtpResponse {
    pub code int
    pub text string
}

用途

Smtp 服务端响应结果,包含状态码、响应头与正文数据。

公开字段

code int
码
text string
文本

read_response

XuAPI
pub fn read_response(fd int) SmtpResponse

用途

读取响应。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)

返回值

SmtpResponse — 该类型的值

new_client

XuAPI
pub fn new_client(endpoint string) SmtpClient

用途

构造 client。

参数

endpoint string
endpoint(string)

返回值

SmtpClient — 该类型的值

(SmtpClient) connect

XuAPI
pub fn (c SmtpClient) connect() int

用途

建立与目标服务端点的网络通信连接。失败时抛出错误,调用方需处理或继续传播。

参数

c SmtpClient
本次调用使用的 SmtpClient 实例;按值接收。

返回值

int — 该类型的值

(SmtpClient) send_mail_on_fd

XuAPI
pub fn (c SmtpClient) send_mail_on_fd(fd int, msg Message) bool

用途

SmtpClient 上的操作:执行 send mail on fd 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c SmtpClient
本次调用使用的 SmtpClient 实例;按值接收。
fd int
文件描述符(int)
msg Message
消息(Message)

返回值

bool — 是否成立

(SmtpClient) close_conn

XuAPI
pub fn (c SmtpClient) close_conn(fd int)

用途

SmtpClient 上的操作:关闭连接。

参数

c SmtpClient
本次调用使用的 SmtpClient 实例;按值接收。
fd int
文件描述符(int)

返回值

不返回值。

struct SmtpPool

XuAPI
pub struct SmtpPool {
    pub client SmtpClient
    pub idle chan int
    pub max_idle int
}

用途

Smtp 连接池管理器,并发安全地维护和调度复用套接字连接。

公开字段

client SmtpClient
字段(SmtpClient)
idle chan int
字段(通道;类型 chan int)
max_idle int
字段(int)

open_pool

XuAPI
pub fn open_pool(endpoint string, max_idle int) SmtpPool

用途

执行 open pool 操作。

参数

endpoint string
endpoint(string)
max_idle int
max_idle(int)

返回值

SmtpPool — 该类型的值

查看用法示例

(&SmtpPool) get_conn

XuAPI
pub fn (p &SmtpPool) get_conn() int

用途

&SmtpPool 上的操作:读取 conn。

参数

p &SmtpPool
本次调用使用的 SmtpPool 实例;以只读借用传入,调用方仍持有实例。

返回值

int — 该类型的值

(&SmtpPool) put_conn

XuAPI
pub fn (p &SmtpPool) put_conn(fd int)

用途

&SmtpPool 上的操作:执行 put conn 操作。

参数

p &SmtpPool
本次调用使用的 SmtpPool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&SmtpPool) send_mail

XuAPI
pub fn (p &SmtpPool) send_mail(msg Message) bool

用途

&SmtpPool 上的操作:执行 send mail 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &SmtpPool
本次调用使用的 SmtpPool 实例;以只读借用传入,调用方仍持有实例。
msg Message
消息(Message)

返回值

bool — 是否成立

(&SmtpPool) close

XuAPI
pub fn (p &SmtpPool) close()

用途

&SmtpPool 上的操作:关闭资源。

参数

p &SmtpPool
本次调用使用的 SmtpPool 实例;以只读借用传入,调用方仍持有实例。

返回值

不返回值。

struct TlsClient

XuAPI
pub struct TlsClient {
    pub conn transport.Conn
    pub host string
    pub port int
}

用途

Smtp 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

conn transport.Conn
字段(transport.Conn)
host string
主机
port int
端口

tls_connect

XuAPI
pub fn tls_connect(endpoint string, default_port int) TlsClient

用途

执行 tls connect 操作。失败时抛出错误,调用方需处理或继续传播。

参数

endpoint string
endpoint(string)
default_port int
default_port(int)

返回值

TlsClient — 该类型的值

tls_connect_config

XuAPI
pub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClient

用途

执行 tls connect config 操作。失败时抛出错误,调用方需处理或继续传播。

参数

endpoint string
endpoint(string)
default_port int
default_port(int)
cfg tls.Config
cfg(tls.Config)

返回值

TlsClient — 该类型的值

($&TlsClient) write_all

XuAPI
pub fn (c $&TlsClient) write_all(data string) int

用途

$&TlsClient 上的操作:执行 write all 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
data string
数据(string)

返回值

int — 该类型的值

($&TlsClient) read

XuAPI
pub fn (c $&TlsClient) read(max int) string

用途

$&TlsClient 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
max int
上限(int)

返回值

string — 结果字符串

($&TlsClient) read_exact

XuAPI
pub fn (c $&TlsClient) read_exact(need int) string

用途

$&TlsClient 上的操作:执行 read exact 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
need int
need(int)

返回值

string — 结果字符串

($&TlsClient) close

XuAPI
pub fn (c $&TlsClient) close() int

用途

$&TlsClient 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

int — 该类型的值

($&TlsClient) set_deadline

XuAPI
pub fn (c $&TlsClient) set_deadline(t_ns int) int

用途

$&TlsClient 上的操作:设置 deadline。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int
t_ns(int)

返回值

int — 该类型的值

($&TlsClient) set_read_deadline

XuAPI
pub fn (c $&TlsClient) set_read_deadline(t_ns int) int

用途

$&TlsClient 上的操作:设置 read deadline。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int
t_ns(int)

返回值

int — 该类型的值

($&TlsClient) set_write_deadline

XuAPI
pub fn (c $&TlsClient) set_write_deadline(t_ns int) int

用途

$&TlsClient 上的操作:设置 write deadline。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int
t_ns(int)

返回值

int — 该类型的值