Search in sources :

Example 1 with SendGridException

use of com.sendgrid.SendGridException in project mxisd by kamax-io.

the class EmailSendGridNotificationHandler method send.

private void send(String recipient, Email email) {
    if (StringUtils.isBlank(cfg.getIdentity().getFrom())) {
        throw new FeatureNotAvailable("3PID Email identity: sender address is empty - " + "You must set a value for notifications to work");
    }
    try {
        email.addTo(recipient);
        email.setFrom(cfg.getIdentity().getFrom());
        email.setFromName(cfg.getIdentity().getName());
        Response response = sendgrid.send(email);
        if (response.getStatus()) {
            log.info("Successfully sent email to {} using SendGrid", recipient);
        } else {
            throw new RuntimeException("Error sending via SendGrid to " + recipient + ": " + response.getMessage());
        }
    } catch (SendGridException e) {
        throw new RuntimeException("Unable to send e-mail invite via SendGrid to " + recipient, e);
    }
}
Also used : FeatureNotAvailable(io.kamax.mxisd.exception.FeatureNotAvailable) Response(com.sendgrid.SendGrid.Response) SendGridException(com.sendgrid.SendGridException)

Example 2 with SendGridException

use of com.sendgrid.SendGridException in project java-docs-samples by GoogleCloudPlatform.

the class SendEmailServlet method service.

@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    final String sendgridApiKey = System.getenv("SENDGRID_API_KEY");
    final String sendgridSender = System.getenv("SENDGRID_SENDER");
    final String toEmail = req.getParameter("to");
    if (toEmail == null) {
        resp.getWriter().print("Please provide an email address in the \"to\" query string parameter.");
        return;
    }
    SendGrid sendgrid = new SendGrid(sendgridApiKey);
    SendGrid.Email email = new SendGrid.Email();
    email.addTo(toEmail);
    email.setFrom(sendgridSender);
    email.setSubject("This is a test email");
    email.setText("Example text body.");
    try {
        SendGrid.Response response = sendgrid.send(email);
        if (response.getCode() != 200) {
            resp.getWriter().print(String.format("An error occured: %s", response.getMessage()));
            return;
        }
        resp.getWriter().print("Email sent.");
    } catch (SendGridException e) {
        throw new ServletException("SendGrid error", e);
    }
}
Also used : ServletException(javax.servlet.ServletException) SendGrid(com.sendgrid.SendGrid) SendGridException(com.sendgrid.SendGridException)

Example 3 with SendGridException

use of com.sendgrid.SendGridException in project java-docs-samples by GoogleCloudPlatform.

the class SendEmailServlet method service.

@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    final String sendgridApiKey = System.getenv("SENDGRID_API_KEY");
    final String sendgridSender = System.getenv("SENDGRID_SENDER");
    final String toEmail = req.getParameter("to");
    if (toEmail == null) {
        resp.getWriter().print("Please provide an email address in the \"to\" query string parameter.");
        return;
    }
    SendGrid sendgrid = new SendGrid(sendgridApiKey);
    SendGrid.Email email = new SendGrid.Email();
    email.addTo(toEmail);
    email.setFrom(sendgridSender);
    email.setSubject("This is a test email");
    email.setText("Example text body.");
    try {
        SendGrid.Response response = sendgrid.send(email);
        if (response.getCode() != 200) {
            resp.getWriter().print(String.format("An error occured: %s", response.getMessage()));
            return;
        }
        resp.getWriter().print("Email sent.");
    } catch (SendGridException e) {
        throw new ServletException("SendGrid error", e);
    }
}
Also used : ServletException(javax.servlet.ServletException) SendGrid(com.sendgrid.SendGrid) SendGridException(com.sendgrid.SendGridException)

Example 4 with SendGridException

use of com.sendgrid.SendGridException in project cryptonomica by Cryptonomica.

the class SendGridServlet method doPost.

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // (?) ofy() should be used from HttpServletRequest context, not as static method in servlet class
    final String SENDGRID_API_KEY = ofy().load().key(Key.create(AppSettings.class, "SendGridApiKey")).now().getValue();
    /* --- get parameters */
    String emailTO = req.getParameter("email");
    String emailCC = req.getParameter("emailCC");
    String messageSubject = req.getParameter("messageSubject");
    String messageText = req.getParameter("messageText");
    String type = req.getParameter("type");
    /* --- log parameters */
    LOG.warning("emailTO: " + emailTO);
    LOG.warning("emailCC: " + emailCC);
    LOG.warning("messageText: " + messageText);
    LOG.warning("messageSubject: " + messageSubject);
    /* initialize the SendGrid object with your SendGrid credentials */
    SendGrid sendgrid = new SendGrid(SENDGRID_API_KEY);
    /* create mail*/
    SendGrid.Email email = new SendGrid.Email();
    email.addTo(emailTO);
    email.setFrom("admin@cryptonomica.net");
    if (emailCC != null) {
        email.addCc(emailCC);
    }
    email.setSubject(messageSubject);
    email.setText(messageText);
    try {
        SendGrid.Response response = sendgrid.send(email);
        LOG.warning(response.getMessage());
    } catch (SendGridException e) {
        LOG.warning(e.getMessage());
    }
}
Also used : SendGrid(com.sendgrid.SendGrid) AppSettings(net.cryptonomica.entities.AppSettings) SendGridException(com.sendgrid.SendGridException)

Example 5 with SendGridException

use of com.sendgrid.SendGridException in project mxisd by kamax-io.

the class EmailSendGridNotificationHandler method send.

private void send(String recipient, Email email) {
    if (StringUtils.isBlank(cfg.getIdentity().getFrom())) {
        throw new FeatureNotAvailable("3PID Email identity: sender address is empty - " + "You must set a value for notifications to work");
    }
    try {
        email.addTo(recipient);
        email.setFrom(cfg.getIdentity().getFrom());
        email.setFromName(cfg.getIdentity().getName());
        Response response = sendgrid.send(email);
        if (response.getStatus()) {
            log.info("Successfully sent email to {} using SendGrid", recipient);
        } else {
            throw new RuntimeException("Error sending via SendGrid to " + recipient + ": " + response.getMessage());
        }
    } catch (SendGridException e) {
        throw new RuntimeException("Unable to send e-mail invite via SendGrid to " + recipient, e);
    }
}
Also used : FeatureNotAvailable(io.kamax.mxisd.exception.FeatureNotAvailable) Response(com.sendgrid.SendGrid.Response) SendGridException(com.sendgrid.SendGridException)

Aggregations

SendGridException (com.sendgrid.SendGridException)5 SendGrid (com.sendgrid.SendGrid)3 Response (com.sendgrid.SendGrid.Response)2 FeatureNotAvailable (io.kamax.mxisd.exception.FeatureNotAvailable)2 ServletException (javax.servlet.ServletException)2 AppSettings (net.cryptonomica.entities.AppSettings)1