Skip to content

Instantly share code, notes, and snippets.

@sendgrid-gists
Last active January 11, 2018 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sendgrid-gists/78d8385f823e0d69580644cc6ca97101 to your computer and use it in GitHub Desktop.
Save sendgrid-gists/78d8385f823e0d69580644cc6ca97101 to your computer and use it in GitHub Desktop.
v3 "Hello World" for email, using SendGrid with Java.
// using SendGrid's Java Library
// https://github.com/sendgrid/sendgrid-java
import com.sendgrid.*;
import java.io.IOException;
public class Example {
public static void main(String[] args) throws IOException {
Email from = new Email("test@example.com");
String subject = "Sending with SendGrid is Fun";
Email to = new Email("test@example.com");
Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
Mail mail = new Mail(from, subject, to, content);
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Request request = new Request();
try {
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.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
}
}
@kemalsami
Copy link

Some part of the code should updated as below:

request.method = Method.POST;
request.endpoint = "mail/send";
request.body = mail.build();

And also
SendGrid sg = new SendGrid("SENDGRID_API_KEY");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment