Skip to content

Instantly share code, notes, and snippets.

@ulisseslima
Created March 24, 2017 22:38
Show Gist options
  • Select an option

  • Save ulisseslima/5272a5d9a09104f13ebe570c91aa7e96 to your computer and use it in GitHub Desktop.

Select an option

Save ulisseslima/5272a5d9a09104f13ebe570c91aa7e96 to your computer and use it in GitHub Desktop.
Java class for sending email using Apache Commons.
package br.com.murah.mail;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
/**
* Sends an email.
* <p>
* Default properties for gmail are used when none are provided.
*
* @since Sep 6, 2016
* @author Ulisses Lima
*/
public class Main {
/**
* Extensions forbidden by gmail.
*/
public static String forbidden = ".*\\.sh|.*\\.exe|.*\\.bat|.*\\.jar|.*\\~";
public static final FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return !name.matches(forbidden);
}
};
public static void main(String[] args) throws Exception {
boolean html = getBool("html", false);
String server = get("server", "smtp.gmail.com");
int port = getInt("port", 465);
String user = get("user");
String password = get("password");
boolean ssl = getBool("ssl", true);
boolean tls = getBool("tls", false);
String from = get("from", "me");
String[] to = getArray("to");
String[] cc = getArray("cc");
String[] bcc = getArray("bcc");
String subject = get("subject");
String message = get("message");
File[] attachments = getAttachments();
MultiPartEmail email = new MultiPartEmail();
email.setHostName(server);
email.setSmtpPort(port);
email.setAuthenticator(new DefaultAuthenticator(user, password));
email.setSSLOnConnect(ssl);
if (tls) {
email.setStartTLSEnabled(tls);
email.setStartTLSRequired(tls);
}
email.setFrom(from);
email.setSubject(subject);
if (html)
email.setContent(message, "text/html; charset=utf-8");
else
email.setMsg(message);
email.addTo(to);
email.addReplyTo(to[0]);
if (cc != null)
email.addCc(cc);
if (bcc != null)
email.addBcc(bcc);
for (File file : attachments) {
EmailAttachment attachment = new EmailAttachment();
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setPath(file.getAbsolutePath());
attachment.setDescription(file.getPath());
attachment.setName(file.getName());
email.attach(attachment);
}
email.send();
}
public static String get(String prop, String defaultValue) {
return System.getProperty(prop, defaultValue);
}
public static String get(String prop) {
return get(prop, "unspecified");
}
public static String[] getArray(String prop) {
String string = get(prop, null);
if (string == null)
return null;
if (string.contains(";")) {
return string.split(";");
}
return string.split(",");
}
public static int getInt(String prop, int defaultValue) {
return Integer.parseInt(get(prop, String.valueOf(defaultValue)));
}
public static boolean getBool(String prop, boolean defaultValue) {
return Boolean.valueOf(get(prop, String.valueOf(defaultValue)))
.booleanValue();
}
/**
* @return found files.
* @since Sep 6, 2016
* @author Ulisses Lima
*/
private static File[] getAttachments() {
String attachParam = get("attach");
if (attachParam == null || attachParam.length() < 1)
return new File[0];
String[] filenames = attachParam.split(";");
List<File> files = new ArrayList<File>(filenames.length);
for (int i = 0; i < filenames.length; i++) {
File file = new File(filenames[i]);
if (!file.exists())
continue;
if (file.getName().matches(forbidden))
continue;
if (file.isDirectory()) {
files.addAll(Arrays.asList(file.listFiles(filter)));
} else {
files.add(file);
}
}
return files.toArray(new File[] {});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment