Sample code for 30+ languages & platforms
Rust

POP3 GMail XOAUTH2 Authentication

See more GMail SMTP/IMAP/POP Examples

Demonstrates using OAuth2 authentication with pop.gmail.com.

Note: This example requires Chilkat v9.5.0.83 or greater.

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_mail_host("pop.gmail.com");
mailman.set_mail_port(995);
mailman.set_pop_ssl(true);

mailman.set_pop_username("my_account@gmail.com");

// If using OAuth2 authentication, leave the password empty.
mailman.set_pop_password("");

// Load our previously obtained OAuth2 access token.
// See Getting a GMail POP3 OAuth2 Access Token
let json_token = chilkat::JsonObject::new();
if json_token.load_file("qa_data/tokens/gmail_pop3.json").is_err() {
    println!("{}", json_token.last_error_text());
    return;
}

mailman.set_o_auth2_access_token(&json_token.string_of("access_token").unwrap_or_default());

// Make the TLS connection to the POP3 server.
if mailman.pop3_connect().is_err() {
    println!("{}", mailman.last_error_text());
    return;
}

// Authenticate..
if mailman.pop3_authenticate().is_err() {
    println!("{}", mailman.last_error_text());
    return;
}

// Find out how many emails are on the server..
let num_emails = mailman.check_mail();
if num_emails < 0 {
    println!("{}", mailman.last_error_text());
    return;
}

// Examine the POP3 session log:
println!("{}", mailman.pop3_session_log());

// The POP3 session log will look something like this:

// **** Connected to pop.gmail.com:995
// < +OK Gpop ready for requests from 87.9.200.42 l16mb86351205iok
// > AUTH XOAUTH2 dXNlcj1....VaMDJFAQE=
// < +OK Welcome.
// > STAT
// < +OK 301 1627357
// 
// -- Finished.

// End the POP3 session and close the connection to the GMail server.
if mailman.pop3_end_session().is_err() {
    println!("{}", mailman.last_error_text());
    return;
}

println!("-- Finished.");