00001 package net.threebit.utils.sosc;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 import java.io.*;
00023 import java.sql.*;
00024 import java.util.*;
00025 import javax.mail.*;
00026 import javax.mail.internet.*;
00027 import javax.activation.*;
00028
00032 public class MailMessage {
00033
00037 public MimeMessage message = null;
00038
00042 private StringBuffer data = new StringBuffer(1000);
00043
00047 private ArrayList attachements = new ArrayList();
00048
00052 public MailMessage( MimeMessage message ) {
00053 this.message = message;
00054 }
00055
00059 public void setFrom (String address) throws Exception {
00060 setFrom(address,null);
00061 }
00062
00066 public void setFrom (String address, String name) throws Exception {
00067 Address[] a = new Address[1];
00068 if (name == null) { a[0] = new InternetAddress(address); }
00069 else { a[0] = new InternetAddress(address,name); }
00070 message.addFrom(a);
00071 }
00072
00076 public void setSubject (String subject) throws Exception {
00077 message.setSubject(subject);
00078 }
00079
00083 public void addRecipient( String address ) throws Exception {
00084 addRecipient(address,null);
00085 }
00086
00090 public void addRecipient( String address, String name ) throws Exception {
00091 Address addr = null;
00092 if (name == null) { addr = new InternetAddress(address); }
00093 else { addr = new InternetAddress(address,name); }
00094 message.addRecipient(Message.RecipientType.TO, addr);
00095 }
00096
00100 public void data (String line) {
00101 data.append(line + "\n");
00102 }
00103
00107 public void data (StringBuffer data) {
00108 data.append(data);
00109 }
00110
00114 public void attachement (File f) throws Exception {
00115 BodyPart bp = new MimeBodyPart();
00116 FileDataSource fs = new FileDataSource(f);
00117 DataHandler dh = new DataHandler(fs);
00118 bp.setDataHandler(dh);
00119 bp.setFileName(f.getName());
00120 attachements.add(bp);
00121 }
00122
00126 public void send() throws Exception {
00127
00128 MimeMultipart mmp = new MimeMultipart();
00129
00130
00131 BodyPart bp = new MimeBodyPart();
00132 bp.setText(data.toString());
00133 mmp.addBodyPart(bp);
00134
00135
00136 for (Iterator i = attachements.iterator(); i.hasNext(); ) {
00137 BodyPart p = (BodyPart) i.next();
00138 mmp.addBodyPart(p);
00139 }
00140
00141
00142
00143 message.setContent(mmp);
00144 MailTool.send(this);
00145 }
00146
00147 }