Struct async_imap::Client
source · pub struct Client<T: Read + Write + Unpin + Debug> { /* private fields */ }
Expand description
An (unauthenticated) handle to talk to an IMAP server. This is what you get when first
connecting. A succesfull call to Client::login
or Client::authenticate
will return a
Session
instance that provides the usual IMAP methods.
Implementations§
source§impl<T: Read + Write + Unpin + Debug + Send> Client<T>
impl<T: Read + Write + Unpin + Debug + Send> Client<T>
sourcepub fn new(stream: T) -> Client<T>
pub fn new(stream: T) -> Client<T>
Creates a new client over the given stream.
For an example of how to use this method to provide a pure-Rust TLS integration, see the rustls.rs in the examples/ directory.
This method primarily exists for writing tests that mock the underlying transport, but can also be used to support IMAP over custom tunnels.
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Convert this Client into the raw underlying stream.
sourcepub async fn login<U: AsRef<str>, P: AsRef<str>>(
self,
username: U,
password: P
) -> Result<Session<T>, (Error, Client<T>)>
pub async fn login<U: AsRef<str>, P: AsRef<str>>( self, username: U, password: P ) -> Result<Session<T>, (Error, Client<T>)>
Log in to the IMAP server. Upon success a Session
instance is
returned; on error the original Client
instance is returned in addition to the error.
This is because login
takes ownership of self
, so in order to try again (e.g. after
prompting the user for credetials), ownership of the original Client
needs to be
transferred back to the caller.
let tls = async_native_tls::TlsConnector::new();
let client = async_imap::connect(
("imap.example.org", 993),
"imap.example.org",
tls
).await?;
match client.login("user", "pass").await {
Ok(s) => {
// you are successfully authenticated!
},
Err((e, orig_client)) => {
eprintln!("error logging in: {}", e);
// prompt user and try again with orig_client here
return Err(e);
}
}
sourcepub async fn authenticate<A: Authenticator, S: AsRef<str>>(
self,
auth_type: S,
authenticator: A
) -> Result<Session<T>, (Error, Client<T>)>
pub async fn authenticate<A: Authenticator, S: AsRef<str>>( self, auth_type: S, authenticator: A ) -> Result<Session<T>, (Error, Client<T>)>
Authenticate with the server using the given custom authenticator
to handle the server’s
challenge.
struct OAuth2 {
user: String,
access_token: String,
}
impl async_imap::Authenticator for &OAuth2 {
type Response = String;
fn process(&mut self, _: &[u8]) -> Self::Response {
format!(
"user={}\x01auth=Bearer {}\x01\x01",
self.user, self.access_token
)
}
}
let auth = OAuth2 {
user: String::from("me@example.com"),
access_token: String::from("<access_token>"),
};
let domain = "imap.example.com";
let tls = async_native_tls::TlsConnector::new();
let client = async_imap::connect((domain, 993), domain, tls).await?;
match client.authenticate("XOAUTH2", &auth).await {
Ok(session) => {
// you are successfully authenticated!
},
Err((err, orig_client)) => {
eprintln!("error authenticating: {}", err);
// prompt user and try again with orig_client here
return Err(err);
}
};