[ Web Proxy ]
URL:
Viewing: https://sendgrid.com/docs/Integrate/Code_Examples/java.html [Back]  [Original]

v3 API Java Code Example | SendGrid Docs | Twilio
For Developers
Sending Email
Page tools
Copy as markdown
Useful for sharing or LLM

On this page
Looking for more inspiration?Visit the
Developer Hub
On this page
Copy as markdown

v3 API Java Code Example

Positive FeedbackNegative Feedback

(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
Positive FeedbackNegative Feedback

The following example sends a single email with the Twilio SendGrid Java library. Set your API key in the SENDGRID_API_KEY environment variable, then build and send the message.

Send an email with the Twilio SendGrid Java library

send-an-email-with-the-twilio-sendgrid-java-library page anchor
Copy code block
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?

We all do sometimes; code is hard. Get help now from our support team(link takes you to an external page), or lean on the wisdom of the crowd by browsing the SendGrid tag(link takes you to an external page) on Stack Overflow.

Copyright 2026 Twilio Inc.


Web Proxy Viewer  |  New URL  |  Original Page