SendGrid Status Report Integration

Posted By : Kuldeep Kumar | 30-Oct-2018
Hey, Friends, today am writing this blog to explain how you can integrate "SendGrid" status report with your project, As we know "SendGrid" is a major Email and Marketing Service provider, we use Sendgrid's API to send email in our project. Sometimes it is required to have Information regarding Different settings and email records of the sendgrid account that you are using with your project.for this sendgrid has provides some endpoint that is going to discuss.
 
there are different sections and settings in sendgrid account that are needed at runtime of email processing.such as "Email Clients", "Browser Stats", "WebHook" and "Suppressions" etc.
 
to use sendgrid API you must have an account on SendGrid by which you will be able to access "SEND_GRID_API_KEY", all you need to have this API KEY for sending email from your java project.
 
 
public class SendMail  {
public static void main(String args[]) throws Exception{
   Email from = new Email("[email protected]");
   String subject = "Sending mail from SendGrid";
   Email to = new Email("[email protected]");
   Content content = new Content("data for email");
   Mail mail = new Mail(from, subject, to, content);
   SendGrid sg = new SendGrid("SEND_GRID_API_KEY");
   Request request = new Request();
   try {
      request.setMethod(Method.POST);
      request.setEndpoint("mail/send");
      request.setBody(mail.build());
      Response response = sg.api(request);
      System.out.println(response.getStatusCode());
      System.out.println(response.getBody());
      System.out.println(response.getHeaders());
   } catch (IOException ex) {
     throw ex;
   }
}

}
 
1. To get status Report, this will return the list of maps containing all stats including blocks, bounces,spam_reports, and invalid_emails etc.
 
    request.setMethod(Method.GET);
    request.setEndpoint("stats");
    request.addQueryParam("aggregated_by", "day");
    request.addQueryParam("limit", "1");
    request.addQueryParam("start_date", "2018-08-01"); //YYYY-MM-DD
    request.addQueryParam("end_date", "2018-10-01");
    request.addQueryParam("offset", "1");
    Response response = sg.api(request);
 
2. To get Bounce Email and the Reason for their bounce.
 
    request.setMethod(Method.GET);
    request.setEndpoint("suppression/bounces");
    Response response = sg.api(request);
 
3. To Delete bounced emails specify the emails.
 
    request.setMethod(Method.DELETE);
    request.setEndpoint("suppression/bounces");
    request.setBody("{\"emails\":[\"[email protected]\",\"[email protected]\"],\"delete_all\":true}");
    Response response = sg.api(request);
 
4. To get Invalid email exists on sendgrid account.
 
    request.setMethod(Method.GET);
    request.setEndpoint("suppression/invalid_emails");
    request.addQueryParam("start_time", "1");
    request.addQueryParam("limit", "1");
    request.addQueryParam("end_time", "1");
    request.addQueryParam("offset", "1");
    Response response = sg.api(request);
 
5. To Delete specified invalid emails exists on sendgrid account.
 
    request.setMethod(Method.DELETE);
    request.setEndpoint("suppression/invalid_emails");
    request.setBody("{\"emails\":[\"[email protected]\",\"[email protected]\"],\"delete_all\":false}");
    Response response = sg.api(request);
 
hope it will be beneficial for you, thanks.

About Author

Author Image
Kuldeep Kumar

Kuldeep is interested in developing web applications and he is currently working on Groovy on Grails. He likes to learn new technologies and love playing chess.

Request for Proposal

Name is required

Comment is required

Sending message..