client
pub fn client(endpoint string) SmtpClientPurpose
Execute client.
Parameters
endpointstring- endpoint (string)
Returns
SmtpClient — a value of this type
#github.com/xulang-org/packages/smtpExternal package `smtp #github.com/xulang-org/packages/smtp`, provided by github.com/xulang-org/packages.
github.com/xulang-org/packages · 1.17.2 · Install and use
These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.
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) SmtpClientExecute client.
endpoint stringSmtpClient — a value of this type
poolpub fn pool(endpoint string, max_conns int) SmtpPoolExecute pool.
endpoint stringmax_conns intSmtpPool — a value of this type
struct Messagepub struct Message {
pub from string
pub to string
pub subject string
pub body string
}Structure encapsulating 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 Client session maintaining connection and operations.
host stringport intusername stringpassword stringstruct SmtpResponsepub struct SmtpResponse {
pub code int
pub text string
}Smtp Server response payload containing status, headers, and body.
code inttext stringread_responsepub fn read_response(fd int) SmtpResponseReads single-line or multi-line SMTP response 250-first line 250-second line 250 final line Throws on failure; handle the error or propagate it to the caller.
fd intSmtpResponse — a value of this type
new_clientpub fn new_client(endpoint string) SmtpClientConstruct client.
endpoint stringSmtpClient — a value of this type
(SmtpClient) connectpub fn (c SmtpClient) connect() intEstablish network connection to the target server endpoint. Throws on failure; handle the error or propagate it to the caller.
c SmtpClientSmtpClient instance used by this call (value receiver).int — a value of this type
(SmtpClient) send_mail_on_fdpub fn (c SmtpClient) send_mail_on_fd(fd int, msg Message) boolOperation on SmtpClient: send mail on fd. Throws on failure; handle the error or propagate it to the caller.
c SmtpClientSmtpClient instance used by this call (value receiver).fd intmsg Messagebool — true or false
(SmtpClient) close_connpub fn (c SmtpClient) close_conn(fd int)Operation on SmtpClient: close connection.
c SmtpClientSmtpClient instance used by this call (value receiver).fd intThis call does not return a value.
struct SmtpPoolpub struct SmtpPool {
pub client SmtpClient
pub idle chan int
pub max_idle int
}Smtp Connection pool manager scheduling reusable socket connections.
client SmtpClientidle chan intmax_idle intopen_poolpub fn open_pool(endpoint string, max_idle int) SmtpPoolOpen pool.
endpoint stringmax_idle intSmtpPool — a value of this type
(&SmtpPool) get_connpub fn (p &SmtpPool) get_conn() intOperation on &SmtpPool: read conn.
p &SmtpPoolSmtpPool instance used by this call (borrow).int — a value of this type
(&SmtpPool) put_connpub fn (p &SmtpPool) put_conn(fd int)Operation on &SmtpPool: put connection.
p &SmtpPoolSmtpPool instance used by this call (borrow).fd intThis call does not return a value.
(&SmtpPool) send_mailpub fn (p &SmtpPool) send_mail(msg Message) boolConvenient delegate to send mail borrowing from pool Throws on failure; handle the error or propagate it to the caller.
p &SmtpPoolSmtpPool instance used by this call (borrow).msg Messagebool — true or false
(&SmtpPool) closepub fn (p &SmtpPool) close()Operation on &SmtpPool: close the resource.
p &SmtpPoolSmtpPool instance used by this call (borrow).This call does not return a value.
struct TlsClientpub struct TlsClient {
pub conn transport.Conn
pub host string
pub port int
}Smtp Client session maintaining connection and operations.
conn transport.Connhost stringport inttls_connectpub fn tls_connect(endpoint string, default_port int) TlsClientTls connect. Throws on failure; handle the error or propagate it to the caller.
endpoint stringdefault_port intTlsClient — a value of this type
tls_connect_configpub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClientTls connect config. Throws on failure; handle the error or propagate it to the caller.
endpoint stringdefault_port intcfg tls.ConfigTlsClient — a value of this type
($&TlsClient) write_allpub fn (c $&TlsClient) write_all(data string) intOperation on $&TlsClient: write all. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).data stringint — a value of this type
($&TlsClient) readpub fn (c $&TlsClient) read(max int) stringOperation on $&TlsClient: read up to max bytes. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).max intstring — result string
($&TlsClient) read_exactpub fn (c $&TlsClient) read_exact(need int) stringOperation on $&TlsClient: read exact. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).need intstring — result string
($&TlsClient) closepub fn (c $&TlsClient) close() intOperation on $&TlsClient: close the resource. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).int — a value of this type
($&TlsClient) set_deadlinepub fn (c $&TlsClient) set_deadline(t_ns int) intOperation on $&TlsClient: set deadline. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).t_ns intint — a value of this type
($&TlsClient) set_read_deadlinepub fn (c $&TlsClient) set_read_deadline(t_ns int) intOperation on $&TlsClient: set read deadline. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).t_ns intint — a value of this type
($&TlsClient) set_write_deadlinepub fn (c $&TlsClient) set_write_deadline(t_ns int) intOperation on $&TlsClient: set write deadline. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).t_ns intint — a value of this type