Latest blog

  • Spring Send Mail Example

    Did you follow Spring Boot Send Email Example and ran Crowsnest? Got it to send the email monitoring notification? If so, bien hecho! But in the end we want it to work for real and not just for local testing purposes. Here, I’ll demonstrate how to send the notification email via Gmail’s SMTP server. This is a Spring send mail example utilising Gmail.

    Application Properties

    Not wasting any time. Below is the properties you’ll need to change in the Crowsnest app. Should be self explanatory. Update your application.properties as appropriate.

    email.recipients = devops@example.com
    spring.mail.host=smtp.gmail.com
    spring.mail.port=587
    spring.mail.username=monitoring@example.com
    spring.mail.password=superSecretPassword
    spring.mail.properties.mail.smtp.auth=true
    spring.mail.properties.mail.smtp.starttls.enable=true

    Google App Password Setup

    Obviously, we’ll need a Gmail account. Once you have that, head over to manage your account (as of this writing, top right, next to the sign out button).

    Spring Send Mail Manage Account
    Spring Send Mail Manage Account

    Your Google account page should look like below. Search for app password.

    Spring Send Mail Google Account
    Spring Send Mail Google Account

    In Google App passwords, create a name for your app (I’ve called mine Crowsnest) and a password. This is the password you’ll put in the application.properties file. This is the spring.mail.password.

    Spring Send Mail Google App Password
    Spring Send Mail Google App Password

    Muy bien. I think that’s all we’ll need to do to have a Google App password for our Spring Boot app to send an email monitoring notification.

    Spring Send Mail Demonstration

    Ok. Run Crowsnest. If you don’t know how, please jump over to Spring Boot Send Email Example and see how it is done. Visit the monitoring page. Depending on your environments.json configuration, you should have something similar like below.

    Crowsnest Up
    Crowsnest Up

    In order to trigger an email notification, we’ll need some site to go down. Fortunately for us, we can edit environments.json and Crowsnest will automatically pick up the changes. Thanks to Java WatchService. Hop over to Java WatchService Example for more details. So to make a site go down, I just change the port number to hit a site that does not exist. The monitoring page should look like something below.

    Crowsnest Down
    Crowsnest Down

    Depending on your polling interval, I’ve set mine to every 10 seconds, an email will be sent. Make sure to stop the app or change environments.json back as soon as the email is sent. You don’t want to spam your inbox unless you want to. The polling interval is set by a cron expression like so in the properties file.

    # At every 10 seconds
    cron.expression = */10 * * * * *

    The moment we’ve been waiting for. Surely the logs the will say it has sent the email. But we will not believe it until it is sent by the SMTP server and it is in the inbox of the recipient. So let’s check the Gmail sent folder and the target recipient inbox (Yahoo). I will let the images speak for themselves below.

    Spring Send Mail Sent
    Spring Send Mail Sent
    Spring Send Mail Inbox
    Spring Send Mail Inbox

    Spring Send Mail Roundup

    Since we are just mere mortals and could not afford an SMTP server, we have utilized Gmail to send our notification email for us. Just a quick Google App password setup and chucking in the correct application properties. We were able to trigger an email notification for real. Thanks for reading my Spring send mail via Gmail example.

    More Spring framework tips here…