The Sendgrid Inbound webhook with spring

2 min readFeb 15, 2019

--

I was trying to set up an inbound webhook for sendgrid at work and it took a lot of time trying to figure out how to set it up, I was finally able to do so with some effort. To prevent this from being an issue for java developers in the future. I figured I should put my solution out there, Hope it helps.

I am assuming you already know how to create a spring project and have already set up your domain for mail sending via sendgrid. If not this is a good place to start off.

I was mainly thrown off by the request posted by sendgrid to my inbound webhook in the multipart/form-data format, which I hadn’t run into before. I was mostly dealing with JSON based inbound, But their documentation said its to be able to transfer attachments over the POST request. So I had to build the following.

To start off I did this

  1. Set up a sendgrid inbound webhook on sengrid using the instructions here
  2. Set up a java project with spring facet in intellij (you can just use spring boot for a quick start).
  3. Set up a controller called InboundMessageController for the /messaging context and a /inbound for dealing with the sendgrid inbound.
  4. Created an Inbound Object so that the posted data could be parsed by spring directly into the object and save me some code.
  5. Sent an email to my inbound domain at (test@<mydomain.com>), to see the email coming in.

My controller was pretty simple.

So a basic controller that is handling inbound for the domain /messaging and /inbound is utilized for POST requests that come in from sendgrid.

The Sendgrid Inbound object is also pretty simple, its a collection of attributes that are posted by sendgrid in the multipart/form-data format,ps. it does not contain all the attributes sent, you can find all of the attributes posted here.

The Inbound object, its also pretty simple, Spring should be smart enough to parse the inbound request for you.

Now, your webhook should be able to just parse the inbound email into the specified object which you can then utilize in your java code.

Hope this makes your life a little simpler.

Varun.

Sign up to discover human stories that deepen your understanding of the world.

--

--

Varun Sastry Devulapalli
Varun Sastry Devulapalli

Written by Varun Sastry Devulapalli

Hi, I am a software developer based in chicago, working since 2012. Hope you find my articles helpful.

Responses (2)

Write a response