Guest User

Untitled

a guest
Nov 28th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. package com.tabuleapp.SendGrid;
  2.  
  3. import javax.mail.Authenticator;
  4. import javax.mail.BodyPart;
  5. import javax.mail.Message;
  6. import javax.mail.MessagingException;
  7. import javax.mail.Multipart;
  8. import javax.mail.PasswordAuthentication;
  9. import javax.mail.Session;
  10. import javax.mail.Transport;
  11. import javax.mail.internet.AddressException;
  12. import javax.mail.internet.InternetAddress;
  13. import javax.mail.internet.MimeBodyPart;
  14. import javax.mail.internet.MimeMessage;
  15. import javax.mail.internet.MimeMultipart;
  16.  
  17. import java.io.UnsupportedEncodingException;
  18. import java.util.Properties;
  19.  
  20. /**
  21. * Creates an email object for sending via SendGrid.
  22. * Be sure to update Sendgrid credentials.
  23. *
  24. * Usage:
  25. * SGEmail email = new SGEmail();
  26. * email.to("neil@tabuleapp.com, "Neil Gupta");
  27. * email.from("contact@tabuleapp.com", "Tabule");
  28. * email.subject("This is a subject");
  29. * email.text("This is a plain text body.");
  30. * email.html("This is an html body.");
  31. * email.send();
  32. *
  33. * @author Neil Gupta. Copyright 2012 Tabule, Inc.
  34. * @version 1.0
  35. */
  36. public class SGEmail {
  37.  
  38. private static final String SMTP_AUTH_USER = "YOUR SENDGRID USERNAME";
  39. private static final String SMTP_AUTH_PWD = "YOUR SENDGRID PASSWORD";
  40. private static final String SMTP_HOST_NAME = "smtp.sendgrid.net";
  41.  
  42. private InternetAddress to;
  43. private InternetAddress from;
  44. private String subject;
  45. private String html;
  46. private String text;
  47.  
  48. public void to(String email, String name) {
  49. try {
  50. to = new InternetAddress(email, name);
  51. } catch (UnsupportedEncodingException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55.  
  56. public void to(String email) {
  57. try {
  58. to = new InternetAddress(email);
  59. } catch (AddressException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. public void from(String email, String name) {
  65. try {
  66. from = new InternetAddress(email, name);
  67. } catch (UnsupportedEncodingException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71.  
  72. public void from(String email) {
  73. try {
  74. from = new InternetAddress(email);
  75. } catch (AddressException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79.  
  80. public void subject(String subject) {
  81. this.subject = subject;
  82. }
  83.  
  84. public void html(String html) {
  85. this.html = html;
  86. }
  87.  
  88. public void text(String text) {
  89. this.text = text;
  90. }
  91.  
  92. public void send() {
  93. try {
  94. Properties props = new Properties();
  95. props.put("mail.transport.protocol", "smtp");
  96. props.put("mail.smtp.host", SMTP_HOST_NAME);
  97. props.put("mail.smtp.port", 587);
  98. props.put("mail.smtp.auth", "true");
  99.  
  100. Authenticator auth = new SMTPAuthenticator();
  101. Session mailSession = Session.getInstance(props, auth);
  102. Transport transport = mailSession.getTransport();
  103.  
  104. MimeMessage message = new MimeMessage(mailSession);
  105.  
  106. Multipart multipart = new MimeMultipart("alternative");
  107.  
  108. if (text != null) {
  109. BodyPart part1 = new MimeBodyPart();
  110. part1.setText(text);
  111. multipart.addBodyPart(part1);
  112. }
  113.  
  114. if (html != null) {
  115. BodyPart part2 = new MimeBodyPart();
  116. part2.setContent(html, "text/html");
  117. multipart.addBodyPart(part2);
  118. }
  119.  
  120. message.setContent(multipart);
  121. message.setFrom(from);
  122. message.setSubject(subject);
  123. message.addRecipient(Message.RecipientType.TO, to);
  124.  
  125. transport.connect();
  126. transport.sendMessage(message,
  127. message.getRecipients(Message.RecipientType.TO));
  128. transport.close();
  129. } catch(MessagingException e) {
  130. e.printStackTrace();
  131. }
  132. }
  133.  
  134. private class SMTPAuthenticator extends javax.mail.Authenticator {
  135. public PasswordAuthentication getPasswordAuthentication() {
  136. String username = SMTP_AUTH_USER;
  137. String password = SMTP_AUTH_PWD;
  138. return new PasswordAuthentication(username, password);
  139. }
  140. }
  141. }
Add Comment
Please, Sign In to add comment