SendGrid requires a call-back URL on which the event status of the email will be posted. The Url must be publically accessible.
It is not possible to deploy a Webhook Consumer Service Application in a production environment without actually testing it in a local environment.
Some websites on the internet avail the functionality of live webhook testing such as https://webhook.site. It provides a temporary live URL to test the API call back for example https://webhook.site/57d5debd-e9e7-4384-b82b-87e18815bfb5
place this URL at https://app.sendgrid.com/settings/mail_settings in the Event Webhook Settings and place as HTTP Post URL, select Authorization Method None for now. Select Events to be POSTed to your URL:
Or alternatively, SendGrid provides an HTTP PATCH method to update Event Webhook Settings and place, Authorization Method, and Events to be POSTed to your URL in the form of JSON.
{
"group_resubscribe": true,
"delivered": true,
"group_unsubscribe": true,
"spam_report": true,
"url": "https://webhook.site/57d5debd-e9e7-4384-b82b-87e18815bfb5",
"enabled": true,
"bounce": true,
"deferred": true,
"unsubscribe": true,
"dropped": true,
"open": true,
"click": true,
"processed": true
}
The webhook.site will provide a random URL every time.
package org.wesome.sendgrid;
import com.sendgrid.SendGrid;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class SendGridApplication {
@Value("${send.grid.api.key}")
private String sendGridApiKey;
public static void main(String[] args) {
SpringApplication.run(SendGridApplication.class, args);
}
@Bean
public SendGrid sendGrid() {
SendGrid sendGrid = new SendGrid(sendGridApiKey);
return sendGrid;
}
}
package org.wesome.sendgrid.controller;
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.SendGrid;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
public class SendGridController {
@Autowired
private SendGrid sendGrid;
@GetMapping("/send")
public ResponseEntity sendMail() throws IOException {
Email from = new Email("[email protected]");
String subject = "WeSome.Org";
Email to = new Email("[email protected]");
Content content = new Content("text/plain", "I am learning SendGrid from WeSome.Org because its fun");
Mail mail = new Mail(from, subject, to, content);
Request request = new Request();
Response response;
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
response = sendGrid.api(request);
System.out.println("Headers := \n" + response.getHeaders());
System.out.println("Body := \n" + response.getBody());
System.out.println("StatusCode := \n" + response.getStatusCode());
} catch (IOException ex) {
throw ex;
}
return new ResponseEntity(HttpStatus.valueOf(response.getStatusCode()).getReasonPhrase(), HttpStatus.valueOf(response.getStatusCode()));
}
@GetMapping("webhook/settings")
public ResponseEntity sendGridAPI() throws IOException {
Request request = new Request();
Response response;
try {
request.setMethod(Method.PATCH);
request.setEndpoint("user/webhooks/event/settings");
request.setBody("{ \"group_resubscribe\": true, \"delivered\": true, \"group_unsubscribe\": true, \"spam_report\": true, \"url\": \"https://webhook.site/8377c50f-dbdc-494b-bf19-55bfdc1c38df\", \"enabled\": true, \"bounce\": true, \"deferred\": true, \"unsubscribe\": true, \"dropped\": true, \"open\": true, \"click\": true, \"processed\": true }");
response = sendGrid.api(request);
System.out.println("Headers := \n" + response.getHeaders());
System.out.println("Body := \n" + response.getBody());
System.out.println("StatusCode := \n" + response.getStatusCode());
} catch (IOException ex) {
throw ex;
}
return new ResponseEntity(HttpStatus.valueOf(response.getStatusCode()).getReasonPhrase(), HttpStatus.valueOf(response.getStatusCode()));
}
}
plugins {
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'org.springframework.boot' version '2.5.5'
id 'java'
}
group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
}
dependencies {
implementation 'com.sendgrid:sendgrid-java:4.4.5'
implementation('org.springframework.boot:spring-boot-starter-web')
}
test {
useJUnitPlatform()
}
send.grid.api.key=Your SendGrid Api Key
curl --location --request GET 'http://localhost:8080/webhook/settings'
curl --location --request GET 'http://localhost:8080/send'