0

I am trying to automate emails for my app and from the SendGrid documentation, I could write the java code to do the same. Here is the java code,

import com.sendgrid.*;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.*;
import java.io.IOException;

public class TwiMail {
    public static void main(String[] args) throws IOException {           
    Email from = new Email("ss@gmail.com");
    Email to = new Email("vv@outlook.com"); 

    String subject = "Sending subject";
    Content content = new Content("text/html", "<em>easy</em> to do with <strong>Java</strong>");

    Mail mail = new Mail(from, subject, to, content);

   SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
   Request request = new Request();

   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.getHeaders());
   System.out.println(response.getBody());
}
}

But I am getting some unexpected errors like

Exception in thread "main" javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Upon further research, I could get the crux of the problem via this StackOverflow page which is about importing the URL certificate to the java cacerts but I don't have any URLs here to import the certificate Or I might say I don't have the need to. Moreover, I am not sure why this error is occurring on executing the java code given by the Twilio SendGrid official documentation

A little help on resolving this issue would be greatly appreciated, Thank You

Dr.Die_OXide
  • 125
  • 11
  • Im not familiar with SendGrid, but according to this: https://docs.sendgrid.com/api-reference/settings-enforced-tls/update-enforced-tls-settings. Maybe you should try to disable TLS and check if it works in this case – Mark Score Jun 03 '22 at 09:49
  • No, this is not helping to resolve the issue – Dr.Die_OXide Jun 03 '22 at 10:24

0 Answers0