Skip to content

Add more examples #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 244 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ thiserror = "1"
tokio = { version = "1", default-features = false, features = ["time", "io-util"], optional = true }

[dev-dependencies]
async-native-tls = { version = "0.5", default-features = false }
env_logger = "^0.9"
glob = "^0.3"
criterion = "^0.3"
@@ -39,7 +40,17 @@ name = "send"
path = "examples/send.rs"
required-features = ["runtime-tokio"]

[[example]]
name = "ssl-xoauth2"
path = "examples/ssl-xoauth2.rs"
required-features = ["runtime-tokio"]

[[example]]
name = "starttls-xoauth2"
path = "examples/starttls-xoauth2.rs"
required-features = ["runtime-tokio"]

[features]
default = ["runtime-tokio"]
runtime-async-std = ["async-std"]
runtime-tokio = ["tokio"]
runtime-async-std = ["async-std", "async-native-tls/runtime-async-std"]
runtime-tokio = ["tokio", "async-native-tls/runtime-tokio"]
43 changes: 43 additions & 0 deletions examples/ssl-xoauth2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// this example can be used as a reference for other smtp providers
// tested with gmail smtp server

use async_native_tls::TlsConnector;
use async_smtp::authentication::{Credentials, Mechanism};
use async_smtp::{Envelope, SendableEmail, SmtpClient, SmtpTransport};
use tokio::io::BufStream;
use tokio::net::TcpStream;

pub type Error = Box<dyn std::error::Error + Send + Sync>;
pub type Result<T> = std::result::Result<T, Error>;

// https://developers.google.com/gmail/imap/imap-smtp
const SMTP_SERVER: &str = "smtp.gmail.com";
const SSL_PORT: u16 = 465;

const USER: &str = "user@gmail.com";
const ACCESS_TOKEN: &str = "accesstoken";

#[tokio::main]
async fn main() -> Result<()> {
let stream = TcpStream::connect((SMTP_SERVER, SSL_PORT)).await?;
let tls_stream = TlsConnector::new().connect(SMTP_SERVER, stream).await?;

let client = SmtpClient::new();
let mut transport = SmtpTransport::new(client, BufStream::new(tls_stream)).await?;

let credentials = Credentials::new(USER.to_owned(), ACCESS_TOKEN.to_owned());
transport
.auth(Mechanism::Xoauth2, &credentials) // use modern auth mechanism
.await?;

let email = SendableEmail::new(
Envelope::new(
Some(USER.parse().unwrap()),
vec!["root@localhost".parse().unwrap()],
)?,
"Hello world",
);
transport.send(email).await?;

Ok(())
}
50 changes: 50 additions & 0 deletions examples/starttls-xoauth2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// this example can be used as a reference for other smtp providers
// tested with gmail, outlook smtp servers

use async_native_tls::TlsConnector;
use async_smtp::authentication::{Credentials, Mechanism};
use async_smtp::{Envelope, SendableEmail, SmtpClient, SmtpTransport};
use tokio::io::BufStream;
use tokio::net::TcpStream;

pub type Error = Box<dyn std::error::Error + Send + Sync>;
pub type Result<T> = std::result::Result<T, Error>;

// https://developers.google.com/gmail/imap/imap-smtp
const SMTP_SERVER: &str = "smtp.gmail.com";
const TLS_PORT: u16 = 587;

const USER: &str = "user@gmail.com";
const ACCESS_TOKEN: &str = "accesstoken";

#[tokio::main]
async fn main() -> Result<()> {
let stream = BufStream::new(TcpStream::connect((SMTP_SERVER, TLS_PORT)).await?);

let client = SmtpClient::new();
let transport = SmtpTransport::new(client, stream).await?;

let pre_tls_stream = transport.starttls().await?.into_inner();
let tls_stream = TlsConnector::new()
.connect(SMTP_SERVER, pre_tls_stream)
.await?;

let client = SmtpClient::new().without_greeting(); // do not wait an greeting message after STARTTLS
let mut transport = SmtpTransport::new(client, BufStream::new(tls_stream)).await?;

let credentials = Credentials::new(USER.to_owned(), ACCESS_TOKEN.to_owned());
transport
.auth(Mechanism::Xoauth2, &credentials) // use modern auth mechanism
.await?;

let email = SendableEmail::new(
Envelope::new(
Some(USER.parse().unwrap()),
vec!["root@localhost".parse().unwrap()],
)?,
"Hello world",
);
transport.send(email).await?;

Ok(())
}