diff --git a/examples/README.md b/examples/README.md index b4a5964..b4c9044 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,12 +1,13 @@ -Examples -======== +# Examples This directory contains examples of working with the IMAP client. Examples: - * basic - This is a very basic example of using the client. - * gmail_oauth2 - This is an example using oauth2 for logging into gmail as a secure appplication. - * idle - This is an example showing how to use IDLE to monitor a mailbox. - * rustls - This demonstrates how to use Rustls instead of Openssl for secure connections (helpful for cross compilation). - * starttls - This is an example showing how to use STARTTLS after connecting over plaintext. - * timeout - This demonstrates how to use timeouts while connecting to an IMAP server by using a custom TCP/TLS stream initialization and creating a `Client` directly instead of using the `ClientBuilder`. + +- basic - This is a very basic example of using the client. +- gmail_oauth2 - This is an example using oauth2 for logging into gmail as a secure appplication. +- idle - This is an example showing how to use IDLE to monitor a mailbox. +- rustls - This demonstrates how to use Rustls instead of Openssl for secure connections (helpful for cross compilation). +- starttls - This is an example showing how to use STARTTLS after connecting over plaintext. +- timeout - This demonstrates how to use timeouts while connecting to an IMAP server by using a custom TCP/TLS stream initialization and creating a `Client` directly instead of using the `ClientBuilder`. +- plaintext - This demonstrates how to make an unencrypted IMAP connection (usually over 143) with a `Client` using a naked TCP connection. diff --git a/examples/plaintext.rs b/examples/plaintext.rs new file mode 100644 index 0000000..39472d4 --- /dev/null +++ b/examples/plaintext.rs @@ -0,0 +1,54 @@ +use std::net::TcpStream; + +fn main() { + // REMINDER this is unsafe, the credentials are sent over the connection in CLEARTEXT + // Anyone or anything between this connection and the server can read your login creds! + // Please oh please do not use this where this is even a possibility. + match plaintext() { + Ok(conn) => { + eprintln!("Connection successful!"); + println!("{:?}",conn); + }, + Err(e) => { + eprintln!("Connection error!"); + eprintln!("{:?}",e); + } + } +} + +fn plaintext() -> imap::error::Result> { + // Make a raw TCP connection to an UNSAFE IMAP server + let stream = TcpStream::connect("imap.example.com:143").unwrap(); // This is unsafe. + let mut client = imap::Client::new(stream); + client.read_greeting()?; + eprintln!("\nUNENCRYPTED connection made!!!!\n"); + eprintln!("This is highly not recommended.\n"); + // to do anything useful with the e-mails, we need to log in + let mut imap_session = client.login("user", "pass").unwrap(); + + // we want to fetch the first email in the INBOX mailbox + imap_session.select("INBOX")?; + + // fetch message number 1 in this mailbox, along with its RFC822 field. + // RFC 822 dictates the format of the body of e-mails + let messages = imap_session.fetch("1", "RFC822")?; + let message = if let Some(m) = messages.iter().next() { + m + } else { + return Ok(None); + }; + + // extract the message's body + let mut body; + match message.body() { + Some(msg) => { + body = std::str::from_utf8(msg) + .expect("message was not valid utf-8") + .to_string(); + } + None => body = "".to_string(), + } + // be nice to the server and log out + imap_session.logout()?; + Ok(Some(body)) +}