1 package org.crazybob.rss; 2 3 import java.io.*; 4 import java.util.*; 5 import org.apache.commons.net.smtp.*; 6 import java.text.*; 7 8 13 public class Mailer { 14 15 private String smtpServer; 16 17 private SimpleDateFormat dateFormat = 18 new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z"); 19 20 public Mailer(String smtpServer) { 21 this.smtpServer = smtpServer; 22 } 23 24 public void send(String to, String from, String friendlyFrom, 25 String subject, String message) 26 throws IOException { 27 SMTPClient client = new SMTPClient(); 28 client.connect(this.smtpServer); 29 int reply = client.getReplyCode(); 30 if (!SMTPReply.isPositiveCompletion(reply)) 31 throw new IOException("Bad reply from SMTP server: " + reply); 32 client.sendSimpleMessage(from, to, 33 composeMessage(to, friendlyFrom, subject, message)); 34 client.disconnect(); 35 } 36 37 String composeMessage(String to, String from, String subject, 38 String message) { 39 StringBuffer buffer = new StringBuffer (); 40 Date now = new Date(); 41 buffer.append("To: " + to + "\n"); 42 buffer.append("From: " + from + "\n"); 43 buffer.append("Date: " + dateFormat.format(now) + "\n"); 44 buffer.append("Subject: " + subject + "\n"); 45 buffer.append("MIME-Version: 1.0\n"); 46 buffer.append("Content-Type: multipart/alternative;" + 47 " boundary=\"----_=_NextPart_001_01C2BB1C.BEA819A2\"\n\n"); 48 buffer.append("------_=_NextPart_001_01C2BB1C.BEA819A2\n"); 49 buffer.append("Content-Type: text/plain; charset=\"iso-8859-1\"\n\n"); 50 buffer.append(message); 51 buffer.append("\n\n"); 52 buffer.append("------_=_NextPart_001_01C2BB1C.BEA819A2\n"); 53 buffer.append("Content-Type: text/html; charset=\"iso-8859-1\"\n\n"); 54 buffer.append(message); 55 buffer.append("\n\n"); 56 buffer.append("------_=_NextPart_001_01C2BB1C.BEA819A2--"); 57 buffer.append("\n\n"); 58 return buffer.toString(); 59 } 60 } 61
| Popular Tags
|