Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. package com.tecnologiafox.interacaofox.system.mail;
  2.  
  3. import java.io.File;
  4. import java.util.Date;
  5. import java.util.Properties;
  6.  
  7. import javax.activation.CommandMap;
  8. import javax.activation.DataHandler;
  9. import javax.activation.DataSource;
  10. import javax.activation.FileDataSource;
  11. import javax.activation.MailcapCommandMap;
  12. import javax.mail.BodyPart;
  13. import javax.mail.Multipart;
  14. import javax.mail.PasswordAuthentication;
  15. import javax.mail.Session;
  16. import javax.mail.Transport;
  17. import javax.mail.internet.InternetAddress;
  18. import javax.mail.internet.MimeBodyPart;
  19. import javax.mail.internet.MimeMessage;
  20. import javax.mail.internet.MimeMultipart;
  21.  
  22. /**
  23. * Created by erick on 8/3/15.
  24. */
  25. public class Mail extends javax.mail.Authenticator{
  26. private String user;
  27. private String password;
  28.  
  29. private String[] to;
  30. private String from;
  31.  
  32. private String port;
  33. private String sport;
  34.  
  35. private String host;
  36.  
  37. private String subject;
  38. private String body;
  39.  
  40. private boolean _auth;
  41.  
  42. private boolean _debuggable;
  43.  
  44. private Multipart multipart;
  45.  
  46. public String getPassword() {
  47. return password;
  48. }
  49.  
  50. public void setPassword(String password) {
  51. this.password = password;
  52. }
  53.  
  54. public String[] getTo() {
  55. return to;
  56. }
  57.  
  58. public void setTo(String[] to) {
  59. this.to = to;
  60. }
  61.  
  62. public String getFrom() {
  63. return from;
  64. }
  65.  
  66. public void setFrom(String from) {
  67. this.from = from;
  68. }
  69.  
  70. public String getHost() {
  71. return host;
  72. }
  73.  
  74. public void setHost(String host) {
  75. this.host = host;
  76. }
  77.  
  78. public String getSubject() {
  79. return subject;
  80. }
  81.  
  82. public void setSubject(String subject) {
  83. this.subject = subject;
  84. }
  85.  
  86. public Multipart getMultipart() {
  87. return multipart;
  88. }
  89.  
  90. public void setMultipart(Multipart multipart) {
  91. this.multipart = multipart;
  92. }
  93.  
  94. public Mail() {
  95. host = "HOST"; // default smtp server
  96. port = "587"; // default smtp port
  97. sport = "465"; // default socketfactory port - 465
  98.  
  99. user = "USER"; // username
  100. password = "PASS"; // password
  101. from = "EMAIL_ALIAS_FROM"; // email sent from
  102. subject = "BLA BLA BLA"; // email subject
  103. body = "BLA BLA BLA BODY"; // email body
  104.  
  105. _debuggable = false; // debug mode on or off - default off
  106. _auth = true; // smtp authentication - default on
  107.  
  108. multipart = new MimeMultipart();
  109.  
  110. // There is something wrong with MailCap, javamail can not find a
  111. // handler for the multipart/mixed part, so this bit needs to be added.
  112. MailcapCommandMap mc = (MailcapCommandMap) CommandMap
  113. .getDefaultCommandMap();
  114. mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
  115. mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
  116. mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
  117. mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
  118. mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
  119. CommandMap.setDefaultCommandMap(mc);
  120. }
  121.  
  122. public Mail(String user, String pass) {
  123. this();
  124.  
  125. this.user = user;
  126. password = pass;
  127. }
  128.  
  129. public boolean send() throws Exception {
  130. Properties props = _setProperties();
  131.  
  132. if (!user.equals("") && !password.equals("") && to.length > 0
  133. && !from.equals("") && !subject.equals("") && !body.equals("")) {
  134. Session session = Session.getInstance(props, this);
  135.  
  136. MimeMessage msg = new MimeMessage(session);
  137.  
  138. msg.setFrom(new InternetAddress(from));
  139.  
  140. InternetAddress[] addressTo = new InternetAddress[to.length];
  141. for (int i = 0; i < to.length; i++) {
  142. addressTo[i] = new InternetAddress(to[i]);
  143. }
  144. msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
  145.  
  146. msg.setSubject(subject);
  147. msg.setSentDate(new Date());
  148.  
  149. // setup message body
  150. BodyPart messageBodyPart = new MimeBodyPart();
  151. messageBodyPart.setText(body);
  152. multipart.addBodyPart(messageBodyPart);
  153.  
  154. // Put parts in message
  155. msg.setContent(multipart);
  156.  
  157. // send email
  158. Transport.send(msg);
  159.  
  160. return true;
  161. } else {
  162. return false;
  163. }
  164. }
  165.  
  166. public void addAttachment(String filename) throws Exception {
  167. BodyPart messageBodyPart = new MimeBodyPart();
  168. DataSource source = new FileDataSource(filename);
  169. messageBodyPart.setDataHandler(new DataHandler(source));
  170. messageBodyPart.setFileName(new File(filename).getName());
  171.  
  172. multipart.addBodyPart(messageBodyPart);
  173. }
  174.  
  175. @Override
  176. public PasswordAuthentication getPasswordAuthentication() {
  177. return new PasswordAuthentication(user, password);
  178. }
  179.  
  180. private Properties _setProperties() {
  181. Properties props = new Properties();
  182.  
  183. props.put("mail.smtp.host", host);
  184.  
  185. if (_debuggable) {
  186. props.put("mail.debug", "true");
  187. }
  188.  
  189. if (_auth) {
  190. props.put("mail.smtp.starttls.enable", "true");
  191. props.put("mail.smtp.auth", "true");
  192. }
  193.  
  194. props.put("mail.smtp.port", port);
  195. props.put("mail.smtp.socketFactory.port", sport);
  196. props.put("mail.smtp.socketFactory.class",
  197. "javax.net.ssl.SSLSocketFactory");
  198. props.put("mail.smtp.socketFactory.fallback", "false");
  199.  
  200. return props;
  201. }
  202.  
  203. // the getters and setters
  204. public String getBody() {
  205. return body;
  206. }
  207.  
  208. public void setBody(String _body) {
  209. this.body = _body;
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement