Java Code Examples for com.sendgrid.SendGrid#Email

The following examples show how to use com.sendgrid.SendGrid#Email . 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: SendGridTranslationTest.java    From ogham with Apache License 2.0 6 votes vote down vote up
@Test
public void forTemplatedTextEmail() throws MessagingException, SendGridException {
	// @formatter:off
	final Email email = new Email()
								.subject(SUBJECT)
								.content(new TemplateContent("string:" + CONTENT_TEXT_TEMPLATE, new SimpleContext("name", NAME)))
								.from(new EmailAddress(FROM_ADDRESS, FROM))
								.to(new EmailAddress(TO_ADDRESS_1, TO), new EmailAddress(TO_ADDRESS_2, TO));
	// @formatter:on

	messagingService.send(email);

	final ArgumentCaptor<SendGrid.Email> argument = ArgumentCaptor.forClass(SendGrid.Email.class);
	verify(sendGridClient).send(argument.capture());
	final SendGrid.Email val = argument.getValue();

	assertEquals(SUBJECT, val.getSubject());
	assertEquals(FROM, val.getFromName());
	assertEquals(FROM_ADDRESS, val.getFrom());
	assertArrayEquals(new String[] { TO, TO }, val.getToNames());
	assertArrayEquals(new String[] { TO_ADDRESS_1, TO_ADDRESS_2 }, val.getTos());
	assertEquals(CONTENT_TEXT_RESULT, val.getText());
}
 
Example 2
Source File: MultiContentHandler.java    From ogham with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the content and adds it into the email. This method is expected to
 * update the content of the {@code email} parameter.
 * 
 * While the method signature accepts any {@link Content} instance as
 * parameter, the method will fail if anything other than a
 * {@link MultiContent} is provided.
 * 
 * @param email
 *            the email to put the content in
 * @param content
 *            the unprocessed content
 * @throws ContentHandlerException
 *             the handler is unable to add the content to the email
 * @throws IllegalArgumentException
 *             the content provided is not of the right type
 */
@Override
public void setContent(final Email original, final SendGrid.Email email, final Content content) throws ContentHandlerException {
	if (email == null) {
		throw new IllegalArgumentException("[email] cannot be null");
	}
	if (content == null) {
		throw new IllegalArgumentException("[content] cannot be null");
	}

	if (content instanceof MultiContent) {
		for (Content subContent : ((MultiContent) content).getContents()) {
			delegate.setContent(original, email, subContent);
		}
	} else {
		throw new IllegalArgumentException("This instance can only work with MultiContent instances, but was passed " + content.getClass().getSimpleName());
	}
}
 
Example 3
Source File: SendGridTranslationTest.java    From ogham with Apache License 2.0 6 votes vote down vote up
@Test
public void forBasicTextEmail() throws MessagingException, SendGridException {
	// @formatter:off
	final Email email = new Email()
								.subject(SUBJECT)
								.content(CONTENT_TEXT)
								.from(new EmailAddress(FROM_ADDRESS, FROM))
								.to(new EmailAddress(TO_ADDRESS_1, TO));
	// @formatter:on

	messagingService.send(email);

	final ArgumentCaptor<SendGrid.Email> argument = ArgumentCaptor.forClass(SendGrid.Email.class);
	verify(sendGridClient).send(argument.capture());
	final SendGrid.Email val = argument.getValue();

	assertEquals(SUBJECT, val.getSubject());
	assertEquals(FROM, val.getFromName());
	assertEquals(FROM_ADDRESS, val.getFrom());
	assertEquals(TO, val.getToNames()[0]);
	assertEquals(TO_ADDRESS_1, val.getTos()[0]);
	assertEquals(CONTENT_TEXT, val.getText());
}
 
Example 4
Source File: SendGridTranslationTest.java    From ogham with Apache License 2.0 6 votes vote down vote up
@Test
public void forTemplatedHtmlEmail() throws MessagingException, SendGridException {
	// @formatter:off
	final Email email = new Email()
								.subject(SUBJECT)
								.content(new TemplateContent("string:" + CONTENT_HTML_TEMPLATE, new SimpleContext("name", NAME)))
								.from(new EmailAddress(FROM_ADDRESS, FROM))
								.to(new EmailAddress(TO_ADDRESS_1, TO), new EmailAddress(TO_ADDRESS_2, TO));
	// @formatter:on

	messagingService.send(email);

	final ArgumentCaptor<SendGrid.Email> argument = ArgumentCaptor.forClass(SendGrid.Email.class);
	verify(sendGridClient).send(argument.capture());
	final SendGrid.Email val = argument.getValue();

	assertEquals(SUBJECT, val.getSubject());
	assertEquals(FROM, val.getFromName());
	assertEquals(FROM_ADDRESS, val.getFrom());
	assertArrayEquals(new String[] { TO, TO }, val.getToNames());
	assertArrayEquals(new String[] { TO_ADDRESS_1, TO_ADDRESS_2 }, val.getTos());
	assertEquals(CONTENT_HTML_RESULT, val.getHtml());
}
 
Example 5
Source File: SendGridTranslationTest.java    From ogham with Apache License 2.0 6 votes vote down vote up
@Test
public void forBasicHtmlEmail() throws MessagingException, SendGridException {
	// @formatter:off
	final Email email = new Email()
								.subject(SUBJECT)
								.content(new StringContent(CONTENT_HTML))
								.from(new EmailAddress(FROM_ADDRESS, FROM))
								.to(new EmailAddress(TO_ADDRESS_1, TO));
	// @formatter:on

	messagingService.send(email);

	final ArgumentCaptor<SendGrid.Email> argument = ArgumentCaptor.forClass(SendGrid.Email.class);
	verify(sendGridClient).send(argument.capture());
	final SendGrid.Email val = argument.getValue();

	assertEquals(SUBJECT, val.getSubject());
	assertEquals(FROM, val.getFromName());
	assertEquals(FROM_ADDRESS, val.getFrom());
	assertEquals(TO, val.getToNames()[0]);
	assertEquals(TO_ADDRESS_1, val.getTos()[0]);
	assertEquals(CONTENT_HTML, val.getHtml());
}
 
Example 6
Source File: MultiContentHandlerTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void setContent_single() throws ContentHandlerException {
	final Content exp = new StringContent("Insignificant");
	final Content content = new MultiContent(exp);
	final SendGrid.Email email = new SendGrid.Email();

	instance.setContent(null, email, content);

	verify(delegate).setContent(any(), any(SendGrid.Email.class), eq(exp));
}
 
Example 7
Source File: SendGridEmailService.java    From dropwizard-experiment with MIT License 5 votes vote down vote up
@Override
public void send(String toAddress, String subject, String htmlContent) {
    SendGrid.Email email = new SendGrid.Email();
    email.setSubject(subject);
    email.setHtml(htmlContent);

    email.setFrom(config.getFromEmail());
    email.setFromName(config.getFromName());

    if (!Strings.isNullOrEmpty(config.getOverrideReceiver())) {
        email.addTo(config.getOverrideReceiver());
    } else {
        email.addTo(toAddress);
    }

    try {
        SendGrid.Response response = sendGrid.send(email);

        if (!response.getStatus()) {
            metrics.meter(MetricRegistry.name("email", "send", "failure", toMetricName(toAddress))).mark();
            log.error("Error sending email via SendGrid:", response.getMessage());
        } else {
            metrics.meter(MetricRegistry.name("email", "send", "success", toMetricName(toAddress))).mark();
        }
    } catch (SendGridException e) {
        metrics.meter(MetricRegistry.name("email", "send", "failure", toMetricName(toAddress))).mark();
        log.error("Error sending email via SendGrid:", e);
    }
}
 
Example 8
Source File: StringContentHandlerTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test(expected = ContentHandlerException.class)
public void setContent_unknown() throws ContentHandlerException, MimeTypeDetectionException, MimeTypeParseException {
	final SendGrid.Email email = new SendGrid.Email();
	final StringContent content = new StringContent(CONTENT_JSON);

	final MimeType mime = new MimeType("application/json");
	when(provider.detect(CONTENT_JSON)).thenReturn(mime);

	instance.setContent(null, email, content);
}
 
Example 9
Source File: StringContentHandlerTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void setContent_html() throws ContentHandlerException, MimeTypeDetectionException, MimeTypeParseException {
	final SendGrid.Email email = new SendGrid.Email();
	final StringContent content = new StringContent(CONTENT_HTML);

	final MimeType mime = new MimeType("text/html");
	when(provider.detect(CONTENT_HTML)).thenReturn(mime);

	instance.setContent(null, email, content);

	assertEquals("The email was not correctly updated", CONTENT_HTML, email.getHtml());
}
 
Example 10
Source File: StringContentHandlerTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void setContent_text() throws ContentHandlerException, MimeTypeDetectionException, MimeTypeParseException {
	final SendGrid.Email email = new SendGrid.Email();

	final MimeType mime = new MimeType("text/plain");
	when(provider.detect(CONTENT_TEXT)).thenReturn(mime);

	instance.setContent(null, email, content);

	assertEquals("The email was not correctly updated", CONTENT_TEXT, email.getText());
}
 
Example 11
Source File: PriorizedContentHandlerTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test(expected = ContentHandlerException.class)
public void setContent_noMatchingHandler() throws ContentHandlerException {
	final SendGrid.Email email = new SendGrid.Email();
	final StringContent content = new StringContent("Insignificant");

	instance.setContent(null, email, content);
}
 
Example 12
Source File: MultiContentHandlerTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test(expected = ContentHandlerException.class)
public void setContent_delegateFailure() throws ContentHandlerException {
	final Content exp = new StringContent("Insignificant");
	final Content content = new MultiContent(exp);
	final SendGrid.Email email = new SendGrid.Email();

	final ContentHandlerException e = new ContentHandlerException("Thrown by mock", exp);
	doThrow(e).when(delegate).setContent(null, email, exp);
	instance.setContent(null, email, content);
}
 
Example 13
Source File: ContentWithAttachmentsHandler.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Override
public void setContent(Email original, SendGrid.Email email, Content content) throws ContentHandlerException {
	ContentWithAttachments cwa = (ContentWithAttachments) content;
	for (Attachment attachment : cwa.getAttachments()) {
		LOG.debug("Attaching {} to email", attachment);
		original.attach(attachment);
	}
	delegate.setContent(original, email, cwa.getContent());
}
 
Example 14
Source File: MultiContentHandlerTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void setContent_notMultiContent() throws ContentHandlerException {
	final Content content = new StringContent("Insignificant");
	final SendGrid.Email email = new SendGrid.Email();

	instance.setContent(null, email, content);
}
 
Example 15
Source File: SendGridClientTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void send() throws SendGridException {
	final SendGrid.Response response = new SendGrid.Response(200, "OK");
	final SendGrid.Email exp = new SendGrid.Email();

	when(delegate.send(exp)).thenReturn(response);

	instance.send(exp);

	verify(delegate).send(exp);
}
 
Example 16
Source File: SendGridV2Sender.java    From ogham with Apache License 2.0 5 votes vote down vote up
private SendGrid.Email toSendGridEmail(final Email message) throws ContentHandlerException, AttachmentReadException {
	final SendGrid.Email ret = new SendGrid.Email();
	ret.setSubject(message.getSubject());

	ret.setFrom(message.getFrom().getAddress());
	ret.setFromName(message.getFrom().getPersonal() == null ? "" : message.getFrom().getPersonal());

	final String[] tos = new String[message.getRecipients().size()];
	final String[] toNames = new String[message.getRecipients().size()];
	int i = 0;
	for (Recipient recipient : message.getRecipients()) {
		final EmailAddress address = recipient.getAddress();
		tos[i] = address.getAddress();
		toNames[i] = address.getPersonal() == null ? "" : address.getPersonal();
		i++;
	}
	ret.setTo(tos);
	ret.setToName(toNames);

	handler.setContent(message, ret, message.getContent());

	for (Attachment attachment : message.getAttachments()) {
		addAttachment(ret, attachment);
	}

	return ret;
}
 
Example 17
Source File: StringContentHandlerTest.java    From ogham with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void setContent_badContentType() throws ContentHandlerException {
	final SendGrid.Email email = new SendGrid.Email();

	instance.setContent(null, email, testContent);
}
 
Example 18
Source File: SendGridV2Sender.java    From ogham with Apache License 2.0 4 votes vote down vote up
private SendGrid.Email intercept(SendGrid.Email sendGridEmail, Email source) {
	if (interceptor == null) {
		return sendGridEmail;
	}
	return interceptor.intercept(sendGridEmail, source);
}
 
Example 19
Source File: SendGridClient.java    From ogham with Apache License 2.0 2 votes vote down vote up
/**
 * Sends the provided email to SendGrid.
 * 
 * @param email
 *            the email to send, cannot be {@code null}
 * @throws SendGridException
 *             an unexpected error occurred when trying to send the email
 */
void send(SendGrid.Email email) throws SendGridException;
 
Example 20
Source File: SendGridContentHandler.java    From ogham with Apache License 2.0 2 votes vote down vote up
/**
 * Reads the content and adds it into the email. This method is expected to
 * update the content of the {@code email} parameter.
 * 
 * @param original
 *            the original Ogham email
 * @param email
 *            the email to put the content in
 * @param content
 *            the unprocessed content
 * @throws ContentHandlerException
 *             the handler is unable to add the content to the email
 */
void setContent(Email original, SendGrid.Email email, Content content) throws ContentHandlerException;