0

How to implement sendgrid parse API in Spring boot.

Code :

@RequestMapping(value = "/api/sendGridParse",method = RequestMethod.POST)
    public ResponseEntity<APIResponseDTO> sendGridParseDoamin(@ModelAttribute EmailParseDTO emailParseDTO )     
    {       
        APIResponseDTO _response = new APIResponseDTO();

        System.out.println("emailParseDTO:"+emailParseDTO.toString());
        try{            
            _response.setMessage("Success");
            _response.setStatus(100);
        }catch (Exception e) {
            e.printStackTrace();
            _response.setMessage("Failed");
            _response.setStatus(101);

        }
        return new ResponseEntity<APIResponseDTO>(_response, HttpStatus.OK);
    }

EmailParseDTO :

public class EmailParseDTO {

    int attachments;
    String charsets; 
    String from; 
    String headers; 
    String html; 
    String subject; 
    String envelope; 
    String text; 
    String to; 
    String cc;
//setter and getter

but it's not send parsed data to above url.

ex: https://d3a26271.ngrok.io/api/sendGridParse

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Raghuveer
  • 1
  • 3
  • What you have posted in question doesn't match with the problem in hand. The given url is nowhere mentioned in code. Please post what you have tried with the relevant code. – Sangam Belose Dec 17 '18 at 09:01
  • Thanks Sangam for your reply.I need code snippet for send grid parser API in Spring boot.like whenever sending any mail to specific domain ex. abc.com. – Raghuveer Dec 17 '18 at 09:10
  • Sendgrid not sending parsed data to above API.This is the URl I configured in send grid( https://d3a26271.ngrok.io/api/sendGridParse). Is there any thing wrong in my API code? – Raghuveer Dec 17 '18 at 09:13
  • You need to add your relevant key and other details provided by sendgrid in your application.properties. Then it should send the request to sendGrid server. Also what response you get when the request is sent.? – Sangam Belose Dec 17 '18 at 10:03
  • Take a look at https://dzone.com/articles/integrate-sendgrid-with-a-spring-boot-and-java-app – Sangam Belose Dec 17 '18 at 10:03
  • I am asking about inbound parse API implementation in Spring boot. http://www.altifysoftware.com/receiving-emails-using-sendgrid-inbound-parse-c-webapi2/ like in Spring boot. – Raghuveer Dec 17 '18 at 10:26
  • Please put these facts in question. So anyone else reading question will have clear idea on what exactly you are trying to achieve. – Sangam Belose Dec 17 '18 at 10:47
  • Post similar request to the your server url from your local machine and check if you are getting the same in your sysout logs...If yes then may be sendgrid is not passing those details in request. One way to check this is to dump entire request in your controller. You will get an idea on what you are getting in request – Sangam Belose Dec 17 '18 at 10:51

1 Answers1

0

According to this article: https://varunsastrydevulapalli.medium.com/the-sendgrid-inbound-webhook-with-spring-dc7b5bae4e0c, we can receive the Inbound Message using this spring controller:

@Controller
@RequestMapping(value = "/messaging")
public class InboundMessageController {
@Bean(name = "multipartResolver")
 public CommonsMultipartResolver commonsMultipartResolver() {
 CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
 commonsMultipartResolver.setDefaultEncoding("UTF-8");
 commonsMultipartResolver.setMaxUploadSize(5000);
 return commonsMultipartResolver;
 }
 
 @RequestMapping(value = "/inbound", method = {RequestMethod.POST, RequestMethod.HEAD}, consumes = MediaType.MULTIPART_FORM_DATA)
 public @ResponseBody
 void processInboundSendGridEmails(HttpServletRequest request,
 HttpServletResponse response,
 @RequestParam(required = false) MultipartFile file,
 SendGridInbound sendGridInbound) {
  System.out.println(sendGridInbound);
 }
}

public class SendGridInbound {
    String headers;
    String dkim;
    String to;
    String html;
    String from;
    String text;
    String sender_ip;
    String spam_report;
    String envelope;
    String attachments;
    String subject;
    String spam_score;
    String attchmentInfo;
    String charsets;
    String spf;

    //getter setters toString
}

Hope it could help.

João Dias
  • 16,277
  • 6
  • 33
  • 45
Dumitru
  • 50
  • 1
  • 7