Search in sources :

Example 1 with SendGrid

use of com.sendgrid.SendGrid in project spring-boot by spring-projects.

the class SendGridAutoConfigurationTests method expectedSendGridBeanCreatedUsername.

@Test
public void expectedSendGridBeanCreatedUsername() {
    loadContext("spring.sendgrid.username:user", "spring.sendgrid.password:secret");
    SendGrid sendGrid = this.context.getBean(SendGrid.class);
    assertThat(sendGrid).extracting("username").containsExactly("user");
    assertThat(sendGrid).extracting("password").containsExactly("secret");
}
Also used : SendGrid(com.sendgrid.SendGrid) Test(org.junit.Test)

Example 2 with SendGrid

use of com.sendgrid.SendGrid in project spring-boot by spring-projects.

the class SendGridAutoConfiguration method sendGrid.

@Bean
@ConditionalOnMissingBean(SendGrid.class)
public SendGrid sendGrid() {
    SendGrid sendGrid = createSendGrid();
    if (this.properties.isProxyConfigured()) {
        HttpHost proxy = new HttpHost(this.properties.getProxy().getHost(), this.properties.getProxy().getPort());
        sendGrid.setClient(HttpClientBuilder.create().setProxy(proxy).setUserAgent("sendgrid/" + sendGrid.getVersion() + ";java").build());
    }
    return sendGrid;
}
Also used : SendGrid(com.sendgrid.SendGrid) HttpHost(org.apache.http.HttpHost) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 3 with SendGrid

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

the class SendEmailServlet method main.

public static void main(String[] args) throws SendGridException {
    SendGrid sendgrid = new SendGrid(SENDGRID_API_KEY);
    SendGrid.Email email = new SendGrid.Email();
    email.addTo(TO_EMAIL);
    email.setFrom(SENDGRID_SENDER);
    email.setSubject("This is a test email");
    email.setText("Example text body.");
    SendGrid.Response response = sendgrid.send(email);
    if (response.getCode() != 200) {
        System.out.print(String.format("An error occured: %s", response.getMessage()));
        return;
    }
    System.out.print("Email sent.");
}
Also used : SendGrid(com.sendgrid.SendGrid)

Example 4 with SendGrid

use of com.sendgrid.SendGrid 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 5 with SendGrid

use of com.sendgrid.SendGrid 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)

Aggregations

SendGrid (com.sendgrid.SendGrid)11 SendGridException (com.sendgrid.SendGridException)3 Test (org.junit.jupiter.api.Test)3 ServletException (javax.servlet.ServletException)2 Bean (org.springframework.context.annotation.Bean)2 Mail (com.sendgrid.Mail)1 Request (com.sendgrid.Request)1 Response (com.sendgrid.Response)1 AppSettings (net.cryptonomica.entities.AppSettings)1 HttpHost (org.apache.http.HttpHost)1 Test (org.junit.Test)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1