Advertisement
Guest User

Untitled

a guest
Jun 15th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. package org.bulkmail.test;
  2.  
  3. import java.util.*;
  4. import javax.mail.*;
  5. import javax.mail.internet.*;
  6.  
  7. public class MailSend {
  8.  
  9. private String host;
  10. private String port;
  11. private String username;
  12. private String password;
  13. private String from;
  14.  
  15. public MailSend() {
  16. ResourceBundle rb = ResourceBundle.getBundle("SMTPAuthenticator");
  17. username = rb.getString("username");
  18. password = rb.getString("password");
  19. host = rb.getString("host");
  20. port = rb.getString("port");
  21. from = rb.getString("from");
  22. }
  23.  
  24. public void send(String to, String subject, String text) throws
  25. AuthenticationFailedException, MessagingException {
  26. sendMail(to, subject, text, false);
  27. }
  28.  
  29. public void sendHTML(String to, String subject, String text) throws
  30. AuthenticationFailedException, MessagingException {
  31. sendMail(to, subject, text, true);
  32. }
  33.  
  34. /**
  35. * sendMail - Send the mail message. This method uses Simple Authentication
  36. * schema (LOGIN), through Base64 enconding
  37. *
  38. * @param to String - The mail recepient
  39. * @param subject String - The mail subject
  40. * @param text String - Message to be sent
  41. * @param isHTML boolean - Set the encoding. True for html.
  42. * @throws AuthenticationFailedException
  43. * @throws MessagingException
  44. */
  45. public void sendMail(String to, String subject, String text,
  46. boolean isHTML) throws
  47. AuthenticationFailedException, MessagingException {
  48. // Get system properties
  49. Properties props = System.getProperties();
  50.  
  51. // Setup mail server properties
  52. props.put("mail.transport.protocol", "smtp");
  53. props.put("mail.smtp.submitter","LOGIN");
  54. props.put("mail.smtp.debug", "true");
  55. props.put("mail.smtp.user", username);
  56. props.put("mail.smtp.host", host);
  57. props.put("mail.smtp.port", port);
  58. props.put("mail.smtp.starttls.enable", "true");
  59. props.put("mail.smtp.auth", "true");
  60. props.put("mail.smtp.allow8bitmime", "true");
  61. props.put("mail.smtp.socketFactory.port", port);
  62. props.put("mail.smtp.socketFactory.class", "javax.net.SocketFactory");
  63. props.put("mail.smtp.socketFactory.fallback", "true");
  64.  
  65. // Get session
  66. Session mailSession = javax.mail.Session.getInstance(props,
  67. new SMTPAuthenticator());
  68.  
  69. // Set debug
  70. mailSession.setDebug(true);
  71.  
  72. // Define message
  73. MimeMessage message = new MimeMessage(mailSession);
  74.  
  75. // Set the from address
  76. message.setFrom( (Address)new InternetAddress(from));
  77. Address[] toUser = InternetAddress.parse(to);
  78. message.setRecipients(MimeMessage.RecipientType.TO, toUser);
  79.  
  80. // Set the subject
  81. message.setSubject(subject);
  82.  
  83. // Set the content
  84. if (isHTML) {
  85. message.setContent(text, "text/html");
  86. } else {
  87. message.setText(text);
  88. }
  89.  
  90. // Send message
  91. Transport.send(message);
  92. }
  93.  
  94. // class SMTPAuthenticator extends Authenticator {
  95. // public PasswordAuthentication getPasswordAuthentication() {
  96. // sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
  97. // return new PasswordAuthentication(enc.encode(username.getBytes()),
  98. // enc.encode(password.getBytes()));
  99. // }
  100. // }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement