Rust
Rust
SMTP XOAUTH2 Authentication
See more SMTP Examples
Demonstrates how to authenticate using an OAuth2 access token. This example assumes you have already obtained the access token and now wish to use it in an SMTP session.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let oauth2_access_token = "some_previously_obtained_token".to_string();
let mailman = chilkat::MailMan::new();
// Set the properties for the SMTP server, whatever they may be..
mailman.set_smtp_host("smtp.example.com");
mailman.set_smtp_port(587);
mailman.set_start_tls(true);
mailman.set_smtp_username("myLogin");
mailman.set_o_auth2_access_token(&oauth2_access_token);
// 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("Joe Example <joe@example.com>");
let _ = email.add_to("Chilkat Admin", "admin@chilkatsoft.com");
// Connect to the server indicated by the SmtpHost property
if mailman.smtp_connect().is_err() {
println!("{}", mailman.last_error_text());
return;
}
// Authenticate using XOAUTH2 (using the OAuth2AccessToken)
if mailman.smtp_authenticate().is_err() {
println!("{}", mailman.last_error_text());
return;
}
if mailman.send_email(&email).is_err() {
println!("{}", mailman.last_error_text());
return;
}
println!("Email sent.");
let _ = mailman.close_smtp_connection();