Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. package br.com.murah.mail;
  2.  
  3. import java.io.File;
  4. import java.io.FilenameFilter;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.List;
  8.  
  9. import org.apache.commons.mail.DefaultAuthenticator;
  10. import org.apache.commons.mail.EmailAttachment;
  11. import org.apache.commons.mail.MultiPartEmail;
  12.  
  13. /**
  14. * Sends an email.
  15. * <p>
  16. * Default properties for gmail are used when none are provided.
  17. *
  18. * @since Sep 6, 2016
  19. * @author Ulisses Lima
  20. */
  21. public class Main {
  22. /**
  23. * Extensions forbidden by gmail.
  24. */
  25. public static String forbidden = ".*\\.sh|.*\\.exe|.*\\.bat|.*\\.jar|.*\\~";
  26. public static final FilenameFilter filter = new FilenameFilter() {
  27. public boolean accept(File dir, String name) {
  28. return !name.matches(forbidden);
  29. }
  30. };
  31.  
  32. public static void main(String[] args) throws Exception {
  33. boolean html = getBool("html", false);
  34. String server = get("server", "smtp.gmail.com");
  35. int port = getInt("port", 465);
  36. String user = get("user");
  37. String password = get("password");
  38. boolean ssl = getBool("ssl", true);
  39. boolean tls = getBool("tls", false);
  40. String from = get("from", "me");
  41. String[] to = getArray("to");
  42. String[] cc = getArray("cc");
  43. String[] bcc = getArray("bcc");
  44. String subject = get("subject");
  45. String message = get("message");
  46. File[] attachments = getAttachments();
  47.  
  48. MultiPartEmail email = new MultiPartEmail();
  49. email.setHostName(server);
  50. email.setSmtpPort(port);
  51. email.setAuthenticator(new DefaultAuthenticator(user, password));
  52. email.setSSLOnConnect(ssl);
  53. if (tls) {
  54. email.setStartTLSEnabled(tls);
  55. email.setStartTLSRequired(tls);
  56. }
  57. email.setFrom(from);
  58. email.setSubject(subject);
  59. if (html)
  60. email.setContent(message, "text/html; charset=utf-8");
  61. else
  62. email.setMsg(message);
  63. email.addTo(to);
  64. email.addReplyTo(to[0]);
  65. if (cc != null)
  66. email.addCc(cc);
  67. if (bcc != null)
  68. email.addBcc(bcc);
  69.  
  70. for (File file : attachments) {
  71. EmailAttachment attachment = new EmailAttachment();
  72. attachment.setDisposition(EmailAttachment.ATTACHMENT);
  73. attachment.setPath(file.getAbsolutePath());
  74. attachment.setDescription(file.getPath());
  75. attachment.setName(file.getName());
  76. email.attach(attachment);
  77. }
  78.  
  79. email.send();
  80. }
  81.  
  82. public static String get(String prop, String defaultValue) {
  83. return System.getProperty(prop, defaultValue);
  84. }
  85.  
  86. public static String get(String prop) {
  87. return get(prop, "unspecified");
  88. }
  89.  
  90. public static String[] getArray(String prop) {
  91. String string = get(prop, null);
  92. if (string == null)
  93. return null;
  94.  
  95. if (string.contains(";")) {
  96. return string.split(";");
  97. }
  98. return string.split(",");
  99. }
  100.  
  101. public static int getInt(String prop, int defaultValue) {
  102. return Integer.parseInt(get(prop, String.valueOf(defaultValue)));
  103. }
  104.  
  105. public static boolean getBool(String prop, boolean defaultValue) {
  106. return Boolean.valueOf(get(prop, String.valueOf(defaultValue)))
  107. .booleanValue();
  108. }
  109.  
  110. /**
  111. * @return found files.
  112. * @since Sep 6, 2016
  113. * @author Ulisses Lima
  114. */
  115. private static File[] getAttachments() {
  116. String attachParam = get("attach");
  117. if (attachParam == null || attachParam.length() < 1)
  118. return new File[0];
  119.  
  120. String[] filenames = attachParam.split(";");
  121. List<File> files = new ArrayList<File>(filenames.length);
  122.  
  123. for (int i = 0; i < filenames.length; i++) {
  124. File file = new File(filenames[i]);
  125. if (!file.exists())
  126. continue;
  127.  
  128. if (file.getName().matches(forbidden))
  129. continue;
  130.  
  131. if (file.isDirectory()) {
  132. files.addAll(Arrays.asList(file.listFiles(filter)));
  133. } else {
  134. files.add(file);
  135. }
  136. }
  137. return files.toArray(new File[] {});
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement