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