Search in sources :

Example 6 with SendGrid

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

the class SendGridAutoConfigurationTests method expectedSendGridBeanWithProxyCreated.

@Test
void expectedSendGridBeanWithProxyCreated() {
    loadContext("spring.sendgrid.api-key:SG.SECRET-API-KEY", "spring.sendgrid.proxy.host:localhost", "spring.sendgrid.proxy.port:5678");
    SendGrid sendGrid = this.context.getBean(SendGrid.class);
    assertThat(sendGrid).extracting("client.httpClient.routePlanner").isInstanceOf(DefaultProxyRoutePlanner.class);
}
Also used : SendGrid(com.sendgrid.SendGrid) Test(org.junit.jupiter.api.Test)

Example 7 with SendGrid

use of com.sendgrid.SendGrid in project OsmAnd-tools by osmandapp.

the class EmailSenderMain method main.

public static void main(String[] args) throws SQLException {
    System.out.println("Send email utility");
    EmailParams p = new EmailParams();
    boolean updateBlockList = false;
    for (String arg : args) {
        String val = arg.substring(arg.indexOf("=") + 1);
        if (arg.startsWith("--id=")) {
            p.templateId = val;
        } else if (arg.startsWith("--groups=")) {
            p.mailingGroups = val;
        } else if (arg.startsWith("--sender_mail=")) {
            p.mailFrom = val;
        } else if (arg.startsWith("--giveaway-series=")) {
            p.giveawaySeries = val;
        } else if (arg.startsWith("--subject=")) {
            p.subject = val;
        } else if (arg.startsWith("--topic=")) {
            p.topic = val;
        } else if (arg.startsWith("--run_opt=")) {
            p.runMode = val;
        } else if (arg.startsWith("--test_addr=")) {
            p.testAddresses = val;
        } else if (arg.startsWith("--since-days-ago=")) {
            if (val.length() > 0) {
                p.daySince = Integer.parseInt(val);
            }
        } else if (arg.equals("--update_block_list")) {
            updateBlockList = true;
        }
    }
    final String apiKey = System.getenv("SENDGRID_KEY");
    sendGridClient = new SendGrid(apiKey);
    Connection conn = getConnection();
    if (updateBlockList) {
        updateUnsubscribed(conn);
        updateBlockList(conn);
        conn.close();
        return;
    }
    checkValidity(p);
    if (conn == null) {
        LOGGER.info("Can't connect to the database");
        System.exit(1);
    }
    Set<String> unsubscribedAndBlocked = getUnsubscribedAndBlocked(conn, p.topic);
    switch(p.runMode) {
        case "send_to_test_email_group":
            sendTestEmails(p, unsubscribedAndBlocked);
            break;
        case "print_statistics":
            printStats(conn, p, unsubscribedAndBlocked);
            break;
        case "send_to_production":
            sendProductionEmails(conn, p, unsubscribedAndBlocked);
            break;
    }
    conn.close();
}
Also used : SendGrid(com.sendgrid.SendGrid) Connection(java.sql.Connection)

Example 8 with SendGrid

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

use of com.sendgrid.SendGrid in project CollectiveOneWebapp by CollectiveOne.

the class AppConfig method sendGrid.

@Bean
public SendGrid sendGrid() {
    String key = System.getenv("SENDGRID_API_KEY");
    SendGrid sg = new SendGrid(key);
    sg.addRequestHeader("X-Mock", "true");
    return sg;
}
Also used : SendGrid(com.sendgrid.SendGrid) Bean(org.springframework.context.annotation.Bean)

Example 10 with SendGrid

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

the class SendGridAutoConfigurationTests method autoConfigurationNotFiredWhenBeanAlreadyCreated.

@Test
void autoConfigurationNotFiredWhenBeanAlreadyCreated() {
    loadContext(ManualSendGridConfiguration.class, "spring.sendgrid.api-key:SG.SECRET-API-KEY");
    SendGrid sendGrid = this.context.getBean(SendGrid.class);
    assertThat(sendGrid.getRequestHeaders().get("Authorization")).isEqualTo("Bearer SG.CUSTOM_API_KEY");
}
Also used : SendGrid(com.sendgrid.SendGrid) Test(org.junit.jupiter.api.Test)

Aggregations

SendGrid (com.sendgrid.SendGrid)13 SendGridException (com.sendgrid.SendGridException)3 Test (org.junit.jupiter.api.Test)3 ServletException (javax.servlet.ServletException)2 Bean (org.springframework.context.annotation.Bean)2 Client (com.sendgrid.Client)1 Mail (com.sendgrid.Mail)1 Request (com.sendgrid.Request)1 Response (com.sendgrid.Response)1 Connection (java.sql.Connection)1 AppSettings (net.cryptonomica.entities.AppSettings)1 SenderException (nl.nn.adapterframework.core.SenderException)1 HttpHost (org.apache.http.HttpHost)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 Test (org.junit.Test)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1