Created
March 6, 2012 08:39
-
-
Save iperdomo/1984949 to your computer and use it in GitHub Desktop.
Writing Java in a functional way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mail.core; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.io.StringWriter; | |
import java.net.URISyntaxException; | |
import java.util.Calendar; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import org.apache.commons.mail.EmailException; | |
import org.apache.commons.mail.SimpleEmail; | |
import au.com.bytecode.opencsv.CSVReader; | |
import freemarker.template.Configuration; | |
import freemarker.template.DefaultObjectWrapper; | |
import freemarker.template.Template; | |
import freemarker.template.TemplateException; | |
public class Mail { | |
public static Configuration getConfiguration() throws IOException, URISyntaxException { | |
Configuration conf = new Configuration(); | |
conf.setDirectoryForTemplateLoading(new File("resources/templates")); | |
conf.setObjectWrapper(new DefaultObjectWrapper()); | |
return conf; | |
} | |
public static Template getTemplate(Configuration conf, String templateName) throws IOException { | |
return conf.getTemplate(templateName); | |
} | |
public static String processTemplate(Map<String, String> context, String templateName) | |
throws IOException, URISyntaxException, TemplateException { | |
StringWriter sw = new StringWriter(); | |
Configuration conf = getConfiguration(); | |
Template template = getTemplate(conf, templateName); | |
template.process(context, sw); | |
return sw.toString(); | |
} | |
public static List<String[]> getMembers() throws IOException { | |
CSVReader reader = new CSVReader(new FileReader("resources/members.csv")); | |
return reader.readAll(); | |
} | |
public static void SendEmail(int week, String member, String email) throws EmailException, | |
IOException, URISyntaxException, TemplateException { | |
final SimpleEmail e = new SimpleEmail(); | |
final Map<String, String> context = new HashMap<String, String>(); | |
context.put("week", String.valueOf(week)); | |
context.put("member", member); | |
e.setHostName("smtp.gmail.com"); | |
e.setSslSmtpPort("465"); | |
e.setSSL(true); | |
e.addTo(email); | |
e.setFrom("email@domain.com", "Your name"); | |
e.setSubject("Weekly Report - Week " + (week)); | |
e.setMsg(processTemplate(context, "mail.ftl")); | |
e.setAuthentication("email@domain.com", "your password"); | |
e.send(); | |
} | |
public static void SendEmails(int week, List<String[]> data) throws EmailException, IOException, | |
URISyntaxException, TemplateException { | |
for (String[] line : data) { | |
SendEmail(week, line[0], line[1]); | |
} | |
} | |
public static void main(String[] args) throws IOException, EmailException, URISyntaxException, | |
TemplateException { | |
final List<String[]> data = getMembers(); | |
final int week = Calendar.getInstance().get(Calendar.WEEK_OF_YEAR); | |
SendEmails(week, data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment