Skip to content

Commit 02f9b65

Browse files
feat: runtime independent examples
1 parent d07ea40 commit 02f9b65

File tree

4 files changed

+130
-2
lines changed

4 files changed

+130
-2
lines changed

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ lettre_email = "0.9"
4242
pretty_assertions = "0.6.1"
4343
async-smtp = { version = "0.3.0" }
4444
async-std = { version = "1.8.0", default-features = false, features = ["std", "attributes"] }
45+
tokio = "0.2.6"
4546

4647
[[example]]
4748
name = "basic"
@@ -51,6 +52,14 @@ required-features = ["default"]
5152
name = "gmail_oauth2"
5253
required-features = ["default"]
5354

55+
[[example]]
56+
name = "futures"
57+
required-features = ["default"]
58+
59+
[[example]]
60+
name = "tokio"
61+
required-features = ["default"]
62+
5463
[[test]]
5564
name = "imap_integration"
5665
required-features = ["default"]

examples/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ Examples:
77
* basic - This is a very basic example of using the client.
88

99
* idle - This is a basic example of how to perform an IMAP IDLE call
10-
and interrupt it based on typing a line into stdin.
10+
and interrupt it based on typing a line into stdin.
1111

1212
* gmail_oauth2 - This is an example using oauth2 for logging into
13-
gmail via the OAUTH2 mechanism.
13+
gmail via the OAUTH2 mechanism.
14+
15+
* futures - The basic example, but using the `futures` executor.

examples/futures.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use async_imap::error::{Error, Result};
2+
use async_std::prelude::*;
3+
use std::env;
4+
5+
fn main() -> Result<()> {
6+
futures::executor::block_on(async {
7+
let args: Vec<String> = env::args().collect();
8+
if args.len() != 4 {
9+
eprintln!("need three arguments: imap-server login password");
10+
Err(Error::Bad("need three arguments".into()))
11+
} else {
12+
let res = fetch_inbox_top(&args[1], &args[2], &args[3]).await?;
13+
println!("**result:\n{}", res.unwrap());
14+
Ok(())
15+
}
16+
})
17+
}
18+
19+
async fn fetch_inbox_top(imap_server: &str, login: &str, password: &str) -> Result<Option<String>> {
20+
let tls = async_native_tls::TlsConnector::new();
21+
22+
// we pass in the imap_server twice to check that the server's TLS
23+
// certificate is valid for the imap_server we're connecting to.
24+
let client = async_imap::connect((imap_server, 993), imap_server, tls).await?;
25+
println!("-- connected to {}:{}", imap_server, 993);
26+
27+
// the client we have here is unauthenticated.
28+
// to do anything useful with the e-mails, we need to log in
29+
let mut imap_session = client.login(login, password).await.map_err(|e| e.0)?;
30+
println!("-- logged in a {}", login);
31+
32+
// we want to fetch the first email in the INBOX mailbox
33+
imap_session.select("INBOX").await?;
34+
println!("-- INBOX selected");
35+
36+
// fetch message number 1 in this mailbox, along with its RFC822 field.
37+
// RFC 822 dictates the format of the body of e-mails
38+
let messages_stream = imap_session.fetch("1", "RFC822").await?;
39+
let messages: Vec<_> = messages_stream.collect::<Result<_>>().await?;
40+
let message = if let Some(m) = messages.first() {
41+
m
42+
} else {
43+
return Ok(None);
44+
};
45+
46+
// extract the message's body
47+
let body = message.body().expect("message did not have a body!");
48+
let body = std::str::from_utf8(body)
49+
.expect("message was not valid utf-8")
50+
.to_string();
51+
println!("-- 1 message received, logging out");
52+
53+
// be nice to the server and log out
54+
imap_session.logout().await?;
55+
56+
Ok(Some(body))
57+
}

examples/tokio.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use async_imap::error::{Error, Result};
2+
use async_std::prelude::*;
3+
use std::env;
4+
use tokio::runtime::Runtime;
5+
6+
fn main() -> Result<()> {
7+
let mut rt = Runtime::new().expect("unable to create runtime");
8+
9+
rt.block_on(async {
10+
let args: Vec<String> = env::args().collect();
11+
if args.len() != 4 {
12+
eprintln!("need three arguments: imap-server login password");
13+
Err(Error::Bad("need three arguments".into()))
14+
} else {
15+
let res = fetch_inbox_top(&args[1], &args[2], &args[3]).await?;
16+
println!("**result:\n{}", res.unwrap());
17+
Ok(())
18+
}
19+
})
20+
}
21+
22+
async fn fetch_inbox_top(imap_server: &str, login: &str, password: &str) -> Result<Option<String>> {
23+
let tls = async_native_tls::TlsConnector::new();
24+
25+
// we pass in the imap_server twice to check that the server's TLS
26+
// certificate is valid for the imap_server we're connecting to.
27+
let client = async_imap::connect((imap_server, 993), imap_server, tls).await?;
28+
println!("-- connected to {}:{}", imap_server, 993);
29+
30+
// the client we have here is unauthenticated.
31+
// to do anything useful with the e-mails, we need to log in
32+
let mut imap_session = client.login(login, password).await.map_err(|e| e.0)?;
33+
println!("-- logged in a {}", login);
34+
35+
// we want to fetch the first email in the INBOX mailbox
36+
imap_session.select("INBOX").await?;
37+
println!("-- INBOX selected");
38+
39+
// fetch message number 1 in this mailbox, along with its RFC822 field.
40+
// RFC 822 dictates the format of the body of e-mails
41+
let messages_stream = imap_session.fetch("1", "RFC822").await?;
42+
let messages: Vec<_> = messages_stream.collect::<Result<_>>().await?;
43+
let message = if let Some(m) = messages.first() {
44+
m
45+
} else {
46+
return Ok(None);
47+
};
48+
49+
// extract the message's body
50+
let body = message.body().expect("message did not have a body!");
51+
let body = std::str::from_utf8(body)
52+
.expect("message was not valid utf-8")
53+
.to_string();
54+
println!("-- 1 message received, logging out");
55+
56+
// be nice to the server and log out
57+
imap_session.logout().await?;
58+
59+
Ok(Some(body))
60+
}

0 commit comments

Comments
 (0)