XU XUv0.16.0

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

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

counts: 7 functions, 14 methods, 5 types, 0 constants, 0 method sets

External 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

Examples · API declarations

Examples

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

Required SDK: Xu 0.8.3 · Complete project · Download source

XuLocal SDK example
@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 declarations

client

XuAPI
pub fn client(endpoint string) SmtpClient

Purpose

Execute client.

Parameters

endpoint string
endpoint (string)

Returns

SmtpClient — a value of this type

pool

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

Purpose

Execute pool.

Parameters

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

Returns

SmtpPool — a value of this type

struct Message

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

Purpose

Structure encapsulating from, to, subject, body.

Public fields

from string
field (string)
to string
field (string)
subject string
field (string)
body string
body

See usage example

struct SmtpClient

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

Purpose

Smtp Client session maintaining connection and operations.

Public fields

host string
host
port int
port
username string
field (string)
password string
field (string)

struct SmtpResponse

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

Purpose

Smtp Server response payload containing status, headers, and body.

Public fields

code int
code
text string
text

read_response

XuAPI
pub fn read_response(fd int) SmtpResponse

Purpose

Reads 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.

Parameters

fd int
file descriptor (int)

Returns

SmtpResponse — a value of this type

new_client

XuAPI
pub fn new_client(endpoint string) SmtpClient

Purpose

Construct client.

Parameters

endpoint string
endpoint (string)

Returns

SmtpClient — a value of this type

(SmtpClient) connect

XuAPI
pub fn (c SmtpClient) connect() int

Purpose

Establish network connection to the target server endpoint. Throws on failure; handle the error or propagate it to the caller.

Parameters

c SmtpClient
The SmtpClient instance used by this call (value receiver).

Returns

int — a value of this type

(SmtpClient) send_mail_on_fd

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

Purpose

Operation on SmtpClient: send mail on fd. Throws on failure; handle the error or propagate it to the caller.

Parameters

c SmtpClient
The SmtpClient instance used by this call (value receiver).
fd int
file descriptor (int)
msg Message
message (Message)

Returns

bool — true or false

(SmtpClient) close_conn

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

Purpose

Operation on SmtpClient: close connection.

Parameters

c SmtpClient
The SmtpClient instance used by this call (value receiver).
fd int
file descriptor (int)

Returns

This call does not return a value.

struct SmtpPool

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

Purpose

Smtp Connection pool manager scheduling reusable socket connections.

Public fields

client SmtpClient
field (SmtpClient)
idle chan int
field (channel; type chan int)
max_idle int
field (int)

open_pool

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

Purpose

Open pool.

Parameters

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

Returns

SmtpPool — a value of this type

See usage example

(&SmtpPool) get_conn

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

Purpose

Operation on &SmtpPool: read conn.

Parameters

p &SmtpPool
The SmtpPool instance used by this call (borrow).

Returns

int — a value of this type

(&SmtpPool) put_conn

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

Purpose

Operation on &SmtpPool: put connection.

Parameters

p &SmtpPool
The SmtpPool instance used by this call (borrow).
fd int
file descriptor (int)

Returns

This call does not return a value.

(&SmtpPool) send_mail

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

Purpose

Convenient delegate to send mail borrowing from pool Throws on failure; handle the error or propagate it to the caller.

Parameters

p &SmtpPool
The SmtpPool instance used by this call (borrow).
msg Message
message (Message)

Returns

bool — true or false

(&SmtpPool) close

XuAPI
pub fn (p &SmtpPool) close()

Purpose

Operation on &SmtpPool: close the resource.

Parameters

p &SmtpPool
The SmtpPool instance used by this call (borrow).

Returns

This call does not return a value.

struct TlsClient

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

Purpose

Smtp Client session maintaining connection and operations.

Public fields

conn transport.Conn
field (transport.Conn)
host string
host
port int
port

tls_connect

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

Purpose

Tls connect. Throws on failure; handle the error or propagate it to the caller.

Parameters

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

Returns

TlsClient — a value of this type

tls_connect_config

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

Purpose

Tls connect config. Throws on failure; handle the error or propagate it to the caller.

Parameters

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

Returns

TlsClient — a value of this type

($&TlsClient) write_all

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

Purpose

Operation on $&TlsClient: write all. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
data string
data (string)

Returns

int — a value of this type

($&TlsClient) read

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

Purpose

Operation on $&TlsClient: read up to max bytes. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
max int
maximum (int)

Returns

string — result string

($&TlsClient) read_exact

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

Purpose

Operation on $&TlsClient: read exact. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
need int
need (int)

Returns

string — result string

($&TlsClient) close

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

Purpose

Operation on $&TlsClient: close the resource. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).

Returns

int — a value of this type

($&TlsClient) set_deadline

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

Purpose

Operation on $&TlsClient: set deadline. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
t_ns int
t_ns (int)

Returns

int — a value of this type

($&TlsClient) set_read_deadline

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

Purpose

Operation on $&TlsClient: set read deadline. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
t_ns int
t_ns (int)

Returns

int — a value of this type

($&TlsClient) set_write_deadline

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

Purpose

Operation on $&TlsClient: set write deadline. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
t_ns int
t_ns (int)

Returns

int — a value of this type