Upload
phil-calcado
View
3.296
Download
2
Tags:
Embed Size (px)
DESCRIPTION
http://fragmental.tw/2008/03/24/using-the-right-words/
Citation preview
from: domain-driven-designto: domain-specific language--An Example.
phillip calçadohttp://fragmental.twhttp://www.thoughtworks.com
JavaMail Usage
I want this CSV report delivered
as an e-mailattachment
ClassicExample
public class Mail {
public void sendEmailWithCsvFile(String from, String to, String subject, String body, List<String> csvFileLines) {
Properties props = new Properties(); props.put("mail.smtp.host", "localhost");
Session session = Session.getDefaultInstance(props);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) }; msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject); msg.setSentDate(new Date());
MimeBodyPart part1 = new MimeBodyPart(); part1.setText(body);
MimeBodyPart part2 = new MimeBodyPart(); StringBuffer buffer = new StringBuffer(); for (String line : csvFileLines) buffer.append(line + "\n"); part2.setContent(buffer.toString(), "text/csv"); part2.setFileName("file.csv");
Multipart mp = new MimeMultipart(); mp.addBodyPart(part1); mp.addBodyPart(part2);
msg.setContent(mp);
Transport.send(msg); }}
RemovingsomeNoise
public class Mail {
public void sendEmailWithCsvFile(String from, String to, String subject, String body, List<String> csvFileLines) {
Properties props = new Properties(); props.put("mail.smtp.host", "localhost"); MailService mailService = new MailService(props); MailMessage message = mailService.newMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setBody(body);
StringBuffer buffer = new StringBuffer(); for (String line : csvFileLines) buffer.append(line + "\n"); Attachment csvFile = new Attachment("file.csv", buffer.toString()); message.attach(csvFile);
mailService.send(message); }}
MakingitFlow
public class Mail {
public void sendEmailWithCsvFile(String from, String to, String subject, String body, List<String> csvFileLines) {
StringBuffer buffer = new StringBuffer(); for (String line : csvFileLines) buffer.append(line + "\n"); Properties props = new Properties(); props.put("mail.smtp.host", "localhost"); MailService mailService = new MailService(props); mailService.newMessage() .from(from) .to(to) .subject(subject) .body(body) .attachTextFile("file3.csv", buffer.toString()) .send(); }}
SpeakingtheLanguage
send_message do to '[email protected]' from '[email protected]' subject 'Testing via JRuby'
body %{ Hi, Please do not forget the milk. Bye! }
attach('file4.csv') { ['Sk. Milk', '$1.33'] ['Avocado', '$3.00'] }end
Thanks
Pictures• http://www.flickr.com/photos/bitzi/293673587/
• http://www.flickr.com/photos/sage/2143086954/
• http://www.flickr.com/photos/brianmitchell/2113553867/
• http://www.flickr.com/photos/kamaski/186266747/
• http://www.flickr.com/photos/tosawyer/43691981/
• http://www.flickr.com/photos/helderfontenele/2134684620/
• http://www.flickr.com/photos/sotto1/808195510/