Sample code for 30+ languages & platforms
Rust

Send Email to Distribution List

See more SMTP Examples

Sends the same email to a list of 1000 email addresses in 50 sends where each email has 20 recipients.

Note: Chilkat is not intended nor designed for mass emailing. A solution such as this might be used for a corporate emailing to employees, or an emailing to newsletter subscribers.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let mailman = chilkat::MailMan::new();

mailman.set_smtp_host("smtp.mymailserver.com");
mailman.set_smtp_port(465);
mailman.set_smtp_ssl(true);
mailman.set_smtp_username("myUsername");
mailman.set_smtp_password("myPassword");

// Create a new email object
let email = chilkat::Email::new();
email.set_subject("This is a test");
email.set_body("This is a test");
email.set_from("Senders Name <sender@example.com>");
let _ = email.add_to("Subscribers", "subscribers@example.com");

let bd_mime = chilkat::BinData::new();
let _ = mailman.render_to_mime_bd(&email, &bd_mime);

// Load a file containing one email address per line.
let dist_list = chilkat::StringTable::new();
if dist_list.append_from_file(1000, "utf-8", "qa_data/distList.txt").is_err() {
    println!("{}", dist_list.last_error_text());
    return;
}

let sb_recipients = chilkat::StringBuilder::new();

let mut i = 0;
let sz_dist_list = dist_list.count();
let mut j = 0;
while i < sz_dist_list {

    // Build a list of comma-separated recipients.
    if j > 0 {
        let _ = sb_recipients.append(",");
    }

    let _ = sb_recipients.append(&dist_list.string_at(i).unwrap_or_default());

    i = i + 1;
    j = j + 1;

    // If we have 20 recipients, or we have the final recipient in the final chunk, then send.
    if (j == 20) || (i == sz_dist_list) {
        if mailman.send_mime_bd("sender@example.com", &sb_recipients.get_as_string().unwrap_or_default(), &bd_mime).is_err() {
            println!("{}", mailman.last_error_text());
            return;
        }

        j = 0;
        sb_recipients.clear();
    }

}

if mailman.close_smtp_connection().is_err() {
    println!("Connection to SMTP server not closed cleanly.");
}

println!("Email sent to distirbution list.");