Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. package cubestack.examples.mail;
  2.  
  3. import java.io.IOException;
  4. import java.util.Properties;
  5.  
  6. import javax.mail.Authenticator;
  7. import javax.mail.Message;
  8. import javax.mail.Message.RecipientType;
  9. import javax.mail.MessagingException;
  10. import javax.mail.PasswordAuthentication;
  11. import javax.mail.Session;
  12. import javax.mail.Transport;
  13. import javax.mail.internet.AddressException;
  14. import javax.mail.internet.InternetAddress;
  15. import javax.mail.internet.MimeBodyPart;
  16. import javax.mail.internet.MimeMultipart;
  17.  
  18. import com.sun.mail.smtp.SMTPMessage;
  19.  
  20. public class Main {
  21.  
  22. private static String[] RECIPIENTS = {"user1@gmail.com","user2@yahoo.co.in"};
  23.  
  24. public static void main(String[] args) throws MessagingException, IOException {
  25. Session session = buildGoogleSession();
  26. Message simpleMessage = buildSimpleMessage(session);
  27. sendMessageToAddress(simpleMessage, RECIPIENTS);
  28. Message messageWithAttachment = buildMessageWithAttachment(session);
  29. sendMessageToAddress(messageWithAttachment, RECIPIENTS);
  30. Message withImage = buildMessageWithEmbeddedImage(session);
  31. addressAndSendMessage(withImage, RECIPIENTS);
  32. System.out.println("All done");
  33. }
  34.  
  35. //
  36. //
  37. // configuration methods
  38. //
  39. //
  40.  
  41. /**
  42. * Build a Session object for an SMTP server that requires neither TSL or
  43. * authentication
  44. *
  45. * @return a Session for sending email
  46. */
  47. public static Session buildSimpleSession() {
  48. Properties mailProps = new Properties();
  49. mailProps.put("mail.transport.protocol", "smtp");
  50. mailProps.put("mail.host", "localhost");
  51. mailProps.put("mail.from", "bro@gmail.com");
  52. return Session.getDefaultInstance(mailProps);
  53. }
  54.  
  55. /**
  56. * Build a Session object for an SMTP server that requires both TSL and
  57. * authentication. This uses Gmail as an example of such a server
  58. *
  59. * @return a Session for sending email
  60. */
  61. public static Session buildGoogleSession() {
  62. Properties mailProps = new Properties();
  63. mailProps.put("mail.transport.protocol", "smtp");
  64. mailProps.put("mail.host", "smtp.gmail.com");
  65. mailProps.put("mail.from", "your_email@gmail.com");
  66. mailProps.put("mail.smtp.starttls.enable", "true");
  67. mailProps.put("mail.smtp.port", "587");
  68. mailProps.put("mail.smtp.auth", "true");
  69. // final, because we're using it in the closure below
  70. final PasswordAuthentication usernamePassword = new PasswordAuthentication(
  71. "your_email@gmail.com", "aVerySecurePwd");
  72. Authenticator auth = new Authenticator() {
  73. protected PasswordAuthentication getPasswordAuthentication() {
  74. return usernamePassword;
  75. }
  76. };
  77. Session session = Session.getInstance(mailProps, auth);
  78. session.setDebug(true);
  79. return session;
  80.  
  81. }
  82.  
  83.  
  84. //
  85. //
  86. // Message building methods
  87. //
  88. //
  89.  
  90. /**
  91. * Build a simple text message - no attachments.
  92. *
  93. * @param session
  94. * @return a multipart MIME message with only one part, a simple text message.
  95. * @throws MessagingException
  96. */
  97. public static Message buildSimpleMessage(Session session)
  98. throws MessagingException {
  99. SMTPMessage m = new SMTPMessage(session);
  100. MimeMultipart content = new MimeMultipart();
  101. MimeBodyPart mainPart = new MimeBodyPart();
  102. mainPart.setText("Hello there! This is simple demo message");
  103. content.addBodyPart(mainPart);
  104. m.setContent(content);
  105. m.setSubject("Demo message");
  106. return m;
  107. }
  108.  
  109.  
  110. /**
  111. * Build a text message with an image as an attachment.
  112. *
  113. * @param session
  114. * @return a multipart MIME message where the first part is a text
  115. * message and the second part is an image
  116. * @throws MessagingException
  117. * @throws IOException
  118. */
  119. public static Message buildMessageWithAttachment(Session session)
  120. throws MessagingException, IOException {
  121. SMTPMessage m = new SMTPMessage(session);
  122. MimeMultipart content = new MimeMultipart();
  123.  
  124. // The main (text) part
  125. MimeBodyPart mainPart = new MimeBodyPart();
  126. mainPart.setText("Hello there! This is simple demo message");
  127. content.addBodyPart(mainPart);
  128.  
  129. // The image
  130. MimeBodyPart imagePart = new MimeBodyPart();
  131. imagePart.attachFile("resources/teapot.jpg");
  132. content.addBodyPart(imagePart);
  133.  
  134. m.setContent(content);
  135. m.setSubject("Demo message with a teapot!");
  136. return m;
  137. }
  138.  
  139. /**
  140. * Build an HTML message with an image embedded in the message.
  141. *
  142. * @param session
  143. * @return a multipart MIME message where the main part is an HTML message and the
  144. * second part is an image that will be displayed within the HTML.
  145. * @throws MessagingException
  146. * @throws IOException
  147. */
  148. public static Message buildMessageWithEmbeddedImage(Session session)
  149. throws MessagingException, IOException {
  150. SMTPMessage m = new SMTPMessage(session);
  151. MimeMultipart content = new MimeMultipart("related");
  152.  
  153. // ContentID is used by both parts
  154. String cid = ContentIdGenerator.getContentId();
  155.  
  156. // HTML part
  157. MimeBodyPart textPart = new MimeBodyPart();
  158. textPart.setText("<html><head>"
  159. + "<title>This is not usually displayed</title>"
  160. + "</head>\n"
  161. + "<body><div><b>Hi there!</b></div>"
  162. + "<div>Sending HTML in email is so <i>cool!</i> </div>\n"
  163. + "<div>And here's an image: <img src=\"cid:"
  164. + cid
  165. + "\" /></div>\n" + "<div>I hope you like it!</div></body></html>",
  166. "US-ASCII", "html");
  167. content.addBodyPart(textPart);
  168.  
  169. // Image part
  170. MimeBodyPart imagePart = new MimeBodyPart();
  171. imagePart.attachFile("resources/teapot.jpg");
  172. imagePart.setContentID("<" + cid + ">");
  173. imagePart.setDisposition(MimeBodyPart.INLINE);
  174. content.addBodyPart(imagePart);
  175.  
  176. m.setContent(content);
  177. m.setSubject("Demo HTML message");
  178. return m;
  179. }
  180.  
  181.  
  182. //
  183. //
  184. // Message sending methods
  185. //
  186. //
  187.  
  188. /**
  189. * Send the message with Transport.send(Message)
  190. *
  191. * @param message
  192. * @param recipient
  193. * @throws MessagingException
  194. */
  195. public static void addressAndSendMessage(Message message, String... recipients)
  196. throws AddressException, MessagingException {
  197. message.setRecipients(RecipientType.TO, getRecipients(recipients));
  198. Transport.send(message);
  199. }
  200.  
  201. /**
  202. * Send the message with Transport.send(Message, Address[])
  203. *
  204. * @param message
  205. * @param recipient
  206. * @throws MessagingException
  207. */
  208. public static void sendMessageToAddress(Message message, String... recipients)
  209. throws MessagingException {
  210. InternetAddress[] iRecipients = getRecipients(recipients);
  211. Transport.send(message, iRecipients);
  212. }
  213.  
  214. private static InternetAddress[] getRecipients(String... recipients) throws AddressException {
  215. InternetAddress[] iRecipients = new InternetAddress[recipients.length];
  216. int i = 0;
  217. for(String recipient: recipients) {
  218. iRecipients[i++] = new InternetAddress(recipient);
  219. }
  220. return iRecipients;
  221. }
  222.  
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement