Skip to contentSkip to navigationSkip to topbar
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
1
// using SendGrid's Java Library
2
// https://github.com/sendgrid/sendgrid-java
3
import com.sendgrid.*;
4
import com.sendgrid.helpers.mail.*;
5
import com.sendgrid.helpers.mail.objects.*;
6
import java.io.IOException;
7
8
public class Example {
9
public static void main(String[] args) throws IOException {
10
Email from = new Email("test@example.com");
11
String subject = "Sending with SendGrid is Fun";
12
Email to = new Email("test@example.com");
13
Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
14
Mail mail = new Mail(from, subject, to, content);
15
16
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
17
Request request = new Request();
18
try {
19
request.setMethod(Method.POST);
20
request.setEndpoint("mail/send");
21
request.setBody(mail.build());
22
Response response = sg.api(request);
23
System.out.println(response.getStatusCode());
24
System.out.println(response.getBody());
25
System.out.println(response.getHeaders());
26
} catch (IOException ex) {
27
throw ex;
28
}
29
}
30
}

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.