Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

MailMessage.java

Go to the documentation of this file.
00001 package net.threebit.utils.sosc;
00002 
00003 /*
00004         Copyright 2003 Shawn Deleurme
00005         Copyright 2003 Kevin O'Donnell
00006 
00007         This program is free software; you can redistribute it and/or modify
00008         it under the terms of the GNU General Public License as published by
00009         the Free Software Foundation; either version 2 of the License, or
00010         (at your option) any later version.
00011 
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU General Public License for more details.
00016 
00017         You should have received a copy of the GNU General Public License
00018         along with this program; if not, write to the Free Software
00019         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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                 // Add a 'text/plain' body part for the message content.
00131                 BodyPart bp = new MimeBodyPart();
00132                 bp.setText(data.toString());
00133                 mmp.addBodyPart(bp);
00134 
00135                 // Go through any "attachments" and add them next
00136                 for (Iterator i = attachements.iterator(); i.hasNext(); ) {
00137                         BodyPart p = (BodyPart) i.next();
00138                         mmp.addBodyPart(p);
00139                 }
00140 
00141                 // Set the message content to the multipart data
00142                 // then call back to MailTool to send the actual message
00143                 message.setContent(mmp);
00144                 MailTool.send(this);
00145         }
00146 
00147 }

Generated on Mon Jul 14 17:19:20 2003 for SOSC by doxygen1.2.15