Sample code for 30+ languages & platforms
Rust

Office365 POP3 Login with OAuth2 Authentication

See more Office365 Examples

Demonstrates how to authenticate using OAuth2 using the POP3 protocol with outlook.office365.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("outlook.office365.com");
mailman.set_mail_port(995);
mailman.set_pop_ssl(true);

// Use your O365 email address here.
mailman.set_pop_username("OFFICE365_EMAIL_ADDRESS");

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

// Load our previously obtained OAuth2 access token.
let json_token = chilkat::JsonObject::new();
if json_token.load_file("qa_data/tokens/office365.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 outlook.office365.com POP3 server.
if mailman.pop3_connect().is_err() {
    println!("{}", mailman.last_error_text());
    return;
}

// Authenticate using XOAUTH2
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 outlook.office365.com:995
// < +OK The Microsoft Exchange POP3 service is ready. [QwBIADIAUABSADEAOABD...YwBvAG0A]
// > AUTH XOAUTH2
// < + 
// > <base64 string in XOAUTH2 format>
// < +OK User successfully authenticated.
// > STAT
// < +OK 3 375302

// 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.");