Java Code Examples for com.sendgrid.SendGrid#api()

The following examples show how to use com.sendgrid.SendGrid#api() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SendEmailServlet.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {


    // Set content for request.
    Email to = new Email(TO_EMAIL);
    Email from = new Email(SENDGRID_SENDER);
    String subject = "This is a test email";
    Content content = new Content("text/plain", "Example text body.");
    Mail mail = new Mail(from, subject, to, content);

    // Instantiates SendGrid client.
    SendGrid sendgrid = new SendGrid(SENDGRID_API_KEY);

    // Instantiate SendGrid request.
    Request request = new Request();

    // Set request configuration.
    request.setMethod(Method.POST);
    request.setEndpoint("mail/send");
    request.setBody(mail.build());

    // Use the client to send the API request.
    Response response = sendgrid.api(request);

    if (response.getStatusCode() != 202) {
      System.out.print(String.format("An error occurred: %s", response.getStatusCode()));
      return;
    }

    System.out.print("Email sent.");
  }
 
Example 2
Source File: SendgridService.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
@Override
public EmailSendingStatus sendEmail(EmailWrapper wrapper) throws IOException {
    Mail email = parseToEmail(wrapper);
    SendGrid sendgrid = new SendGrid(Config.SENDGRID_APIKEY);
    Request request = new Request();
    request.setMethod(Method.POST);
    request.setEndpoint("mail/send");
    request.setBody(email.build());
    Response response = sendgrid.api(request);
    return new EmailSendingStatus(response.getStatusCode(), response.getBody());
}
 
Example 3
Source File: SendEmailServlet.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Override
public void service(HttpServletRequest req, HttpServletResponse resp)
    throws IOException, ServletException {
  // Get parameters from environment variables.
  final String sendgridApiKey = System.getenv("SENDGRID_API_KEY");
  final String sendgridSender = System.getenv("SENDGRID_SENDER");

  // Get email from query string.
  final String toEmail = req.getParameter("to");
  if (toEmail == null) {
    resp.getWriter()
        .print("Please provide an email address in the \"to\" query string parameter.");
    return;
  }

  // Set content for request.
  Email to = new Email(toEmail);
  Email from = new Email(sendgridSender);
  String subject = "This is a test email";
  Content content = new Content("text/plain", "Example text body.");
  Mail mail = new Mail(from, subject, to, content);

  // Instantiates SendGrid client.
  SendGrid sendgrid = new SendGrid(sendgridApiKey);

  // Instantiate SendGrid request.
  Request request = new Request();

  try {
    // Set request configuration.
    request.setMethod(Method.POST);
    request.setEndpoint("mail/send");
    request.setBody(mail.build());

    // Use the client to send the API request.
    Response response = sendgrid.api(request);

    if (response.getStatusCode() != 202) {
      resp.getWriter().print(String.format("An error occurred: %s", response.getStatusCode()));
      return;
    }

    // Print response.
    resp.getWriter().print("Email sent.");
  } catch (IOException e) {
    throw new ServletException("SendGrid error", e);
  }
}
 
Example 4
Source File: SendEmailServlet.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Override
public void service(HttpServletRequest req, HttpServletResponse resp)
    throws IOException, ServletException {
  // Get parameters from environment variables.
  final String sendgridApiKey = System.getenv("SENDGRID_API_KEY");
  final String sendgridSender = System.getenv("SENDGRID_SENDER");

  // Get email from query string.
  final String toEmail = req.getParameter("to");
  if (toEmail == null) {
    resp.getWriter()
        .print("Please provide an email address in the \"to\" query string parameter.");
    return;
  }

  // [START gae_sendgrid]
  // Set content for request.
  Email to = new Email(toEmail);
  Email from = new Email(sendgridSender);
  String subject = "This is a test email";
  Content content = new Content("text/plain", "Example text body.");
  Mail mail = new Mail(from, subject, to, content);

  // Instantiates SendGrid client.
  SendGrid sendgrid = new SendGrid(sendgridApiKey);

  // Instantiate SendGrid request.
  Request request = new Request();

  try {
    // Set request configuration.
    request.setMethod(Method.POST);
    request.setEndpoint("mail/send");
    request.setBody(mail.build());

    // Use the client to send the API request.
    Response response = sendgrid.api(request);

    if (response.getStatusCode() != 202) {
      resp.getWriter().print(String.format("An error occurred: %s", response.getStatusCode()));
      return;
    }

    // Print response.
    resp.getWriter().print("Email sent.");
  } catch (IOException e) {
    throw new ServletException("SendGrid error", e);
  }
  // [END gae_sendgrid]
}