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"); } }
@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; }
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(); }
@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); }
@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); }