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."); }
private void setMimeContent(final Email email, final String contentStr, final String mime) throws ContentHandlerException { if ("text/plain".equals(mime)) { email.setText(contentStr); } else if ("text/html".equals(mime)) { email.setHtml(contentStr); } else { throw new ContentHandlerException("MIME type " + mime + " is not supported"); } }
public Email addAttachment(String name, File file) throws IOException, FileNotFoundException { return this.addAttachment(name, new FileInputStream(file)); }
String[] tos = email.getTos(); String[] tonames = email.getToNames(); String[] ccs = email.getCcs(); String[] bccs = email.getBccs(); builder.addTextBody(String.format(PARAM_TO, 0), email.getFrom(), ContentType.create(TEXT_PLAIN, UTF_8)); if (email.getAttachments().size() > 0) { Iterator it = email.getAttachments().entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); if (email.getContentIds().size() > 0) { Iterator it = email.getContentIds().entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); if (email.getHeaders().size() > 0) builder.addTextBody(PARAM_HEADERS, new JSONObject(email.getHeaders()).toString(), ContentType.create(TEXT_PLAIN, UTF_8)); if (email.getFrom() != null && !email.getFrom().isEmpty()) builder.addTextBody(PARAM_FROM, email.getFrom(), ContentType.create(TEXT_PLAIN, UTF_8)); if (email.getFromName() != null && !email.getFromName().isEmpty()) builder.addTextBody(PARAM_FROMNAME, email.getFromName(), ContentType.create(TEXT_PLAIN, UTF_8)); if (email.getReplyTo() != null && !email.getReplyTo().isEmpty()) builder.addTextBody(PARAM_REPLYTO, email.getReplyTo(), ContentType.create(TEXT_PLAIN, UTF_8));
@Override protected Void doInBackground(Void... params) { try { SendGrid sendgrid = new SendGrid(SENDGRID_USERNAME, SENDGRID_PASSWORD); SendGrid.Email email = new SendGrid.Email(); // Get values from edit text to compose email // TODO: Validate edit texts email.addTo(mTo); email.setFrom(mFrom); email.setSubject(mSubject); email.setText(mText); // Attach image if (mUri != null) { email.addAttachment(mAttachmentName, mAppContext.getContentResolver().openInputStream(mUri)); } // Send email, execute http request SendGrid.Response response = sendgrid.send(email); mMsgResponse = response.getMessage(); Log.d("SendAppExample", mMsgResponse); } catch (SendGridException | IOException e) { Log.e("SendAppExample", e.toString()); } return null; }
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); } }
private SendGrid.Email toSendGridEmail(final Email message) throws ContentHandlerException { final SendGrid.Email ret = new SendGrid.Email(); ret.setSubject(message.getSubject()); ret.setFrom(message.getFrom().getAddress()); ret.setFromName(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(); i++; } ret.setTo(tos); ret.setToName(toNames); handler.setContent(ret, message.getContent()); return ret; }
@Override public void send(final Email email) throws SendGridException { if (email == null) { throw new IllegalArgumentException("[email] cannot be null"); } LOG.debug("Sending to SendGrid client: FROM {}<{}>", email.getFromName(), email.getFrom()); LOG.debug("Sending to SendGrid client: TO {} (as {})", email.getTos(), email.getToNames()); LOG.debug("Sending to SendGrid client: SUBJECT {}", email.getSubject()); LOG.debug("Sending to SendGrid client: TEXT CONTENT {}", email.getText()); LOG.debug("Sending to SendGrid client: HTML CONTENT {}", email.getHtml()); initSendGridClient(); final SendGrid.Response response = delegate.send(email); if (response.getStatus()) { LOG.debug("Response from SendGrid client: ({}) {}", response.getCode(), response.getMessage()); } else { throw new SendGridException(new IOException("Sending to SendGrid failed: (" + response.getCode() + ") " + response.getMessage())); } }
public Email addAttachment(String name, String file) throws IOException { return this.addAttachment(name, new ByteArrayInputStream(file.getBytes())); }
@Override public void sendForRemoteValidation(IThreePidSession session) { EmailTemplate template = cfg.getTemplates().getSession().getLocal(); Email email = getEmail(); email.setSubject(populateForRemoteValidation(session, template.getSubject())); email.setText(populateForRemoteValidation(session, getFromFile(template.getBody().getText()))); email.setHtml(populateForRemoteValidation(session, getFromFile(template.getBody().getHtml()))); send(session.getThreePid().getAddress(), email); }
@Override public void sendForValidation(IThreePidSession session) { EmailTemplate template = cfg.getTemplates().getSession().getLocal(); Email email = getEmail(); email.setSubject(populateForValidation(session, template.getSubject())); email.setText(populateForValidation(session, getFromFile(template.getBody().getText()))); email.setHtml(populateForValidation(session, getFromFile(template.getBody().getHtml()))); send(session.getThreePid().getAddress(), email); }
@Override public void sendForInvite(IMatrixIdInvite invite) { EmailTemplate template = cfg.getTemplates().getGeneric().get("matrixId"); Email email = getEmail(); email.setSubject(populateForInvite(invite, template.getSubject())); email.setText(populateForInvite(invite, getFromFile(template.getBody().getText()))); email.setHtml(populateForInvite(invite, getFromFile(template.getBody().getHtml()))); send(invite.getAddress(), email); }
import com.sendgrid.*; public class SendGridExample { public static void main(String[] args) { SendGrid sendgrid = new SendGrid("SENDGRID_APIKEY"); SendGrid.Email email = new SendGrid.Email(); email.addTo("test@sendgrid.com"); email.setFrom("you@youremail.com"); email.setSubject("Sending with SendGrid is Fun"); email.setHtml("and easy to do anywhere, even with Java"); SendGrid.Response response = sendgrid.send(email); } }
SendGrid sendgrid = new SendGrid("YOUR_API_KEY_HERE"); SendGrid.Email welcomeMail = new SendGrid.Email(); welcomeMail.addTo(emailAddress); welcomeMail.addToName("User-san"); welcomeMail.setFrom("welcome@example.com"); welcomeMail.setSubject("Welcome to Example!"); welcomeMail.setText("Thank you for your interest in Example.com! It is still in Beta at the moment but there are a number of exciting features planned. Tell us what you'd like to see."); try { SendGrid.Response response = sendgrid.send(welcomeMail); System.out.println(response.getMessage()); } catch (SendGridException sge) { sge.printStackTrace(); }
SendGrid sendgrid = new SendGrid(Constants.SENDGRID_API_KEY); SendGrid.Email email = new SendGrid.Email(); email.addTo("recipient@gmail.com"); email.setFrom("whatever@whatever.com"); email.setFromName("Whatever"); email.setSubject(...); ....
@Override public void sendForReply(IThreePidInviteReply invite) { EmailTemplate template = cfg.getTemplates().getInvite(); Email email = getEmail(); email.setSubject(populateForReply(invite, template.getSubject())); email.setText(populateForReply(invite, getFromFile(template.getBody().getText()))); email.setHtml(populateForReply(invite, getFromFile(template.getBody().getHtml()))); send(invite.getInvite().getAddress(), email); }
protected Email getEmail() { Email email = new Email(); email.setFrom(cfg.getIdentity().getFrom()); email.setFromName(cfg.getIdentity().getName()); return email; }