Skip to content

Commit db360f1

Browse files
feat: runtime independent examples
1 parent 9097f0e commit db360f1

File tree

4 files changed

+133
-4
lines changed

4 files changed

+133
-4
lines changed

Diff for: Cargo.toml

+12-2
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ nom = "5.0"
2727
base64 = "0.11"
2828
chrono = "0.4"
2929
async-native-tls = "0.3.0"
30-
async-std = { version = "1.4.0", default-features = false, features = ["std"] }
30+
async-std = { version = "1.4.0", default-features = false, features = ["std"]}
3131
pin-utils = "0.1.0-alpha.4"
3232
futures = "0.3.0"
3333
async-attributes = "1.1.0"
3434
rental = "0.5.5"
35-
stop-token = { version = "0.1.1", features = ["unstable"] }
35+
stop-token = "0.1.1"
3636
byte-pool = "0.2.1"
3737
lazy_static = "1.4.0"
3838
log = "0.4.8"
@@ -42,6 +42,8 @@ thiserror = "1.0.9"
4242
lettre_email = "0.9"
4343
pretty_assertions = "0.6.1"
4444
async-smtp = "0.2.0"
45+
async-std = { version = "1.4.0", features = ["default"] }
46+
tokio = "0.2.6"
4547

4648
[[example]]
4749
name = "basic"
@@ -51,6 +53,14 @@ required-features = ["default"]
5153
name = "gmail_oauth2"
5254
required-features = ["default"]
5355

56+
[[example]]
57+
name = "futures"
58+
required-features = ["default"]
59+
60+
[[example]]
61+
name = "tokio"
62+
required-features = ["default"]
63+
5464
[[test]]
5565
name = "imap_integration"
5666
required-features = ["default"]

Diff for: examples/README.md

+4-2
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.

Diff for: examples/futures.rs

+57
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+
}

Diff for: examples/tokio.rs

+60
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)