Using Lettre With Gmail

Preface

Lettre is a popular Rust crate for setting up a SMTP client. It works well with Gmail’s SMTP server but preparing your Gmail account for use with Lettre (or a lot of other software…) is not immediately clear. This post will show you how to allow ‘Less Secure Apps’ for your google account and how to use Lettre to send emails through smtp.gmail.com.

Setting up Gmail

Allowing ‘Less Secure Apps’

Normally, Google wants you to perform all authentication to apps using Oauth2. However, to reduce the barrier of entry for the developer, we can tell Google to allow ‘Less Secure Apps’ to access our account. Once allowed, we can use basic smtp authentication to authenticate our app for sending emails.

For Non-Managed Users

  1. Log in to Gmail with the account you want use for smtp.
  2. Access ‘Google Account’ by clicking your avatar at the top-right of the page.
  3. From the menu on the left, access ‘Security’ settings.
  4. Scroll down to the ‘Less secure app access’ section.
  5. Click ‘Turn on access’ and move the slider to the enabled position.

Non-Managed Allow Less Secure Apps

Working with Two-Factor-Auth

If your account has Two-Factor-Auth enabled, then you need to create a new ‘App Password’ to use. An App Password is a random password that can be used by apps to log in without requiring 2FA.

2FA Rejected Less Secure Apps Option

  1. Access your ‘Google Account’ ‘Security’ settings like in the steps above.
  2. Scroll down to the ‘Signing in to Google’ section.
  3. Click on ‘App passwords’ and verify your current credentials.
  4. Generate a new password for app ‘Mail’ and device ‘Other’.
  5. Save the randomly generated app password, and use it instead of your normal password for authenticating with Lettre.

Add App Password

For Managed Users

If your account is managed by a Google domain account (business, enterprise, etc), then the domain administrator must enable access to Less Secure Apps.

  1. Log into the Google Admin Console.
  2. Click the ‘Security’ icon.
  3. Click the ‘Basic settings’ item.
  4. Under the ‘Less secure apps’ section, click ‘Go to settings for less secure apps >>’.
  5. Select either ‘Allow users to manage their access to less secure apps’ or ‘Enforce access to less secure apps for all users (Not Recommended).’
  6. Save.
  7. Now perform the “For Non-Managed Users” steps given above.

Enable Less Secure Apps for Managed Users

Lettre Example

https://github.com/WebeWizard/lettre-gmail-example

Cargo.toml

1
2
3
4
5
6
7
8
9
10
11
[package]
name = "lettre-gmail-example"
version = "0.1.0"
authors = ["webewizard"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
lettre = "0.9.2"
lettre_email = "0.9.2"

main.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
extern crate lettre;
extern crate lettre_email;
use lettre::smtp::authentication::IntoCredentials;
use lettre::{SmtpClient, Transport};
use lettre_email::EmailBuilder;
fn main() {
let smtp_address = "smtp.gmail.com";
let username = "j.halper@dunmiff.com";
let password = "Sup3rDup3rP@ssw0rd";
let email = EmailBuilder::new()
.to("d.schrute@dunmiff.com")
.from(username)
.subject("Which bear is best?")
.text("Bears eat beets. Bears. Beets. Battlestar Galactica.")
.build()
.unwrap()
.into();
let credentials = (username, password).into_credentials();
let mut client = SmtpClient::new_simple(smtp_address)
.unwrap()
.credentials(credentials)
.transport();
let _result = client.send(email);
}

Share Comments