Guest User

Untitled

a guest
May 30th, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. import java.io.File;
  2. import java.util.Date;
  3. import java.util.Properties;
  4.  
  5. import javax.activation.CommandMap;
  6. import javax.activation.DataHandler;
  7. import javax.activation.DataSource;
  8. import javax.activation.FileDataSource;
  9. import javax.activation.MailcapCommandMap;
  10. import javax.mail.BodyPart;
  11. import javax.mail.MessagingException;
  12. import javax.mail.Multipart;
  13. import javax.mail.PasswordAuthentication;
  14. import javax.mail.Session;
  15. import javax.mail.Transport;
  16. import javax.mail.internet.InternetAddress;
  17. import javax.mail.internet.MimeBodyPart;
  18. import javax.mail.internet.MimeMessage;
  19. import javax.mail.internet.MimeMultipart;
  20.  
  21. /**
  22. * Created by Jesus on 24/11/2017.
  23. */
  24. public class Mail extends javax.mail.Authenticator {
  25.  
  26. // Variables
  27. private String user = ""; // username
  28. private String pass = ""; // password
  29.  
  30. private String[] to;
  31. private String from = user; // correo electrónico enviado desde
  32.  
  33. private String port = "465"; // default puerto smtp
  34. private String sport = "465"; // default puerto socketfactory
  35.  
  36. private String host = "alpha.validns.com"; //"smtp.gmail.com"; // default servidor smtp
  37.  
  38. private String subject = ""; // email asunto
  39.  
  40. private boolean auth = true; // smtp authentication - default on
  41.  
  42. private boolean debuggable = false; // debug mode on or off - default off
  43.  
  44. private Multipart multipart;
  45.  
  46. // Constructor
  47. public Mail() {
  48. multipart = new MimeMultipart();
  49.  
  50. // Hay algo mal con MailCap, javamail no puede encontrar un
  51. // controlador para multipart/mixed part, por lo que este bit debe agregarse.
  52. MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
  53. mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
  54. mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
  55. mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
  56. mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
  57. mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
  58. CommandMap.setDefaultCommandMap(mc);
  59. }
  60.  
  61. public Mail(String user, String pass) {
  62. this();
  63. this.setUser(user);
  64. this.setPass(pass);
  65. }
  66.  
  67. /**
  68. * Envia el correo.
  69. *
  70. * @throws Exception
  71. */
  72. public void send() throws Exception {
  73. throwIfEmpty(user, "Especifica el remitente");
  74. throwIfEmpty(pass, "Se requiere de password");
  75. throwIfEmpty(from, "Especifica el remitente");
  76. //throwIfEmpty(subject, "user == null");
  77. throwIfEmpty(to, "Especifica al menos un destinatario.");
  78. //throwIfEmpty(body, "body == null");
  79.  
  80. Properties props = getProperties();
  81. Session session = Session.getInstance(props, this);
  82.  
  83. MimeMessage msg = new MimeMessage(session);
  84. msg.setFrom(new InternetAddress(from));
  85.  
  86. InternetAddress[] addressTo = new InternetAddress[to.length];
  87. for (int i = 0; i < to.length; i++) {
  88. addressTo[i] = new InternetAddress(to[i]);
  89. }
  90. msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
  91.  
  92. msg.setSubject(subject);
  93. msg.setSentDate(new Date());
  94.  
  95.  
  96. msg.setHeader("X-Priority", "1");
  97. // Poner partes en mensaje
  98. msg.setContent(multipart);
  99.  
  100. // Envia el email
  101. Transport.send(msg);
  102. }
  103.  
  104. /**
  105. * Email cuerpo de correo
  106. *
  107. * @param body Cuerpo del mensaje
  108. */
  109. public void addBody(Object body) {
  110. addBody(body, "text/html");
  111. }
  112.  
  113. /**
  114. * Email cuerpo de correo
  115. *
  116. * @param body Cuerpo del mensaje
  117. * @param contetType typo de contenido
  118. */
  119. public void addBody(Object body, String contetType) {
  120. try {
  121. BodyPart messageBodyPart = new MimeBodyPart();
  122. messageBodyPart.setContent(body, contetType);
  123. multipart.addBodyPart(messageBodyPart);
  124. } catch (MessagingException ignore) {
  125. }
  126. }
  127.  
  128. /**
  129. * Metodo para añadir un adjunto.
  130. * @param filename ruta del archivo a subir.
  131. */
  132. public void addAttachment(String filename) {
  133. addAttachment(new File(filename));
  134. }
  135.  
  136. /**
  137. * Metodo para añadir un archivo.
  138. * @param file archivo a subir.
  139. */
  140. public void addAttachment(java.io.File file) {
  141. try {
  142. BodyPart messageBodyPart = new MimeBodyPart();
  143. DataSource source = new FileDataSource(file);
  144. messageBodyPart.setDataHandler(new DataHandler(source));
  145. messageBodyPart.setFileName(file.getName());
  146.  
  147. multipart.addBodyPart(messageBodyPart);
  148. } catch (MessagingException ignore) {
  149. }
  150. }
  151.  
  152. @Override
  153. public PasswordAuthentication getPasswordAuthentication() {
  154. return new PasswordAuthentication(user, pass);
  155. }
  156.  
  157. private Properties getProperties() {
  158. Properties props = new Properties();
  159.  
  160. props.put("mail.smtp.host", host);
  161.  
  162. if (debuggable) {
  163. props.put("mail.debug", "true");
  164. }
  165.  
  166. if (auth) {
  167. props.put("mail.smtp.auth", "true");
  168. }
  169.  
  170. props.put("mail.smtp.port", port);
  171. props.put("mail.smtp.socketFactory.port", sport);
  172. props.put("mail.smtp.socketFactory.class",
  173. "javax.net.ssl.SSLSocketFactory");
  174. props.put("mail.smtp.socketFactory.fallback", "false");
  175.  
  176. return props;
  177. }
  178.  
  179. private static void throwIfEmpty(String value, String msg) {
  180. if (value == null || value.isEmpty()) throw new RuntimeException(msg);
  181. }
  182.  
  183. private static void throwIfEmpty(String[] to, String msg) {
  184. if (to == null || to.length == 0) throw new RuntimeException(msg);
  185. }
  186.  
  187. // getters and setters
  188.  
  189. public String getUser() {
  190. return user;
  191. }
  192.  
  193. public void setUser(String _user) {
  194. this.user = _user;
  195. setFrom(_user);
  196. }
  197.  
  198. public String getPass() {
  199. return pass;
  200. }
  201.  
  202. public void setPass(String _pass) {
  203. this.pass = _pass;
  204. }
  205.  
  206. public String[] getTo() {
  207. return to;
  208. }
  209.  
  210. public void setTo(String... _to) {
  211. this.to = _to;
  212. }
  213.  
  214. public String getFrom() {
  215. return from;
  216. }
  217.  
  218. public void setFrom(String _from) {
  219. this.from = _from;
  220. }
  221.  
  222. public String getPort() {
  223. return port;
  224. }
  225.  
  226. public void setPort(String _port) {
  227. this.port = _port;
  228. }
  229.  
  230. public String getSport() {
  231. return sport;
  232. }
  233.  
  234. public void setSport(String _sport) {
  235. this.sport = _sport;
  236. }
  237.  
  238. public String getHost() {
  239. return host;
  240. }
  241.  
  242. public void setHost(String _host) {
  243. this.host = _host;
  244. }
  245.  
  246. public String getSubject() {
  247. return subject;
  248. }
  249.  
  250. public void setSubject(String _subject) {
  251. this.subject = _subject;
  252. }
  253.  
  254. public boolean isAuth() {
  255. return auth;
  256. }
  257.  
  258. public void setAuth(boolean _auth) {
  259. this.auth = _auth;
  260. }
  261.  
  262. public boolean isDebuggable() {
  263. return debuggable;
  264. }
  265.  
  266. public void setDebuggable(boolean _debuggable) {
  267. this.debuggable = _debuggable;
  268. }
  269.  
  270. public Multipart getMultipart() {
  271. return multipart;
  272. }
  273.  
  274. public void setMultipart(Multipart _multipart) {
  275. this.multipart = _multipart;
  276. }
  277. }
Add Comment
Please, Sign In to add comment