Send Encrypted Email Using Java Spring Boot – Simple Step By Step Guide With Source Code

Emails are used as communication modes most of the time. Not always people use encrypted email when sending business-sensitive information. Outlook and other email clients have inbuilt support for this and you can do this with a simple click.

Send Encrypted Email Using Java Spring Boot:

But what if you need to send encrypted email via your custom application. In this article, we will see how we can send an encrypted email using Java. We will use the Spring Boot project for this.

Software used

In this tutorial, we will use PKI to encrypt and sign our emails. For testing purposes, we will prepare a couple of self-signed certificates using Java Keytool and use them in our example.

Encrypted Email Prerequisite

Create Certificate for Signer : We will use our domain name certificate as a signer when we only sign and send an email. The below command gives us a JKS file with a private key and public certificate for our site. We will use this to sign our email.

Create Certificate for Mail Receipient : When an encrypted email is sent to any email id or recipient we need to have his public key. So the same can be used to encrypt an email for that user. In our tutorial we will use email id “[email protected]” and we will use a self-signed certificate for this email id. The command to prepare the JKS for this is

As this is a self-signed certificate we need to trust this in our machine. We will install this certificate along with its private key. So when an encrypted email is sent the mail client will be able to access the correct certificate and key. To do that we will convert our JKS to PKCS #12 using below command.

Once done, just double click the “solapure.p12” file, you will be prompted to install this certificate. Follow the instructions and install this certificate as a Trusted Authority.

The Project

Now we will go through an actual encrypted email project. We will be extending our existing project from github repository. The most important is that we add bouncy castle dependencies to our pom.xml along with other default spring boot items.

Now let’s look into our

Email Signing

The first thing this utility method does is to add Bouncy Castle as one of the Security Provider. Then we read the certificates from JKS we prepared earlier. You can refer more on this utility in our previous article

Then we add some default SMIME  capabilities and create a signer object. The signer also needs details about the certificate we read earlier. The signer then can generate the MimeMultipart object which we can send.

To send mail in our example we will do like

Post this, once you receive mail in your mailbox you can verify the signature as below.Email Encryption


Encrypted Email

For encryption of email, the utility class has separate method as below

The first step is similar to signing and we need to let JVM know about our security provider. The method getRecipientPublicCertificate is externalized. In this example, we are reading a fixed certificate for an email but in reality, this has to be dynamic. We need to figure out a way to fetch public certificates based on emails from the public certificate directory.

Once encrypted mail is sent, based upon your recipient’s email client he will be asked to allow access to a private key. You will see the screen as below Encrypted email with Spring Boot

If you allow it then only you will be able to open your encrypted mail. The opened email will look like Encrypted email using java

So now we are sure that our email is encrypted and only the actual recipient will be able to open and read it.  You can download the code from our Github

Download Code


The tutorial gives you a working example of email encryption and signing. I hope you like it and find it useful. Please do not hesitate to comment or ask a question.

  1. Shital Mahajan
    November 16, 2021 | Reply
  2. Arpitha
    December 11, 2019 | Reply
    • Pavan
      December 30, 2019 | Reply
  3. srini
    July 2, 2019 | Reply
    • Pavan
      July 3, 2019 | Reply
      • srini
        July 8, 2019 | Reply
  4. Daniele
    January 21, 2019 | Reply
    • Shilpa
      January 22, 2019 | Reply
  5. Chris
    June 20, 2018 | Reply
    • Pavan
      June 21, 2018 | Reply
  6. Bhupendra
    May 17, 2018 | Reply
    • Pavan
      May 17, 2018 | Reply
      • Bhupendra
        May 18, 2018 | Reply
        • Pavan
          May 18, 2018 | Reply
          • Bhupendra
            May 18, 2018 |
          • Pavan
            May 18, 2018 |
  7. Rama
    February 22, 2018 | Reply
    • Pavan
      February 22, 2018 | Reply
  8. January 14, 2018 | Reply
    • Pavan
      January 15, 2018 | Reply

Add a Comment

Your email address will not be published. Required fields are marked *