Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

v3 API Java Code Example


(information)

Info

We recommend using SendGrid Java, our client library, available on GitHub(link takes you to an external page), with full documentation.

(information)

Info

Do you have an API Key(link takes you to an external page) yet? If not, go get one. You're going to need it to integrate!


Using SendGrid's Java Library

using-sendgrids-java-library page anchor

_30
// using SendGrid's Java Library
_30
// https://github.com/sendgrid/sendgrid-java
_30
import com.sendgrid.*;
_30
import com.sendgrid.helpers.mail.*;
_30
import com.sendgrid.helpers.mail.objects.*;
_30
import java.io.IOException;
_30
_30
public class Example {
_30
public static void main(String[] args) throws IOException {
_30
Email from = new Email("[email protected]");
_30
String subject = "Sending with SendGrid is Fun";
_30
Email to = new Email("[email protected]");
_30
Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
_30
Mail mail = new Mail(from, subject, to, content);
_30
_30
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
_30
Request request = new Request();
_30
try {
_30
request.setMethod(Method.POST);
_30
request.setEndpoint("mail/send");
_30
request.setBody(mail.build());
_30
Response response = sg.api(request);
_30
System.out.println(response.getStatusCode());
_30
System.out.println(response.getBody());
_30
System.out.println(response.getHeaders());
_30
} catch (IOException ex) {
_30
throw ex;
_30
}
_30
}
_30
}


Rate this page: