1*cdf0e10cSrcweir import javax.mail.*; 2*cdf0e10cSrcweir import javax.mail.internet.*; 3*cdf0e10cSrcweir import com.msoft.mail.provider.nntp.NNTPTransport; 4*cdf0e10cSrcweir import java.util.Properties; 5*cdf0e10cSrcweir import java.io.*; 6*cdf0e10cSrcweir import javax.activation.*; 7*cdf0e10cSrcweir 8*cdf0e10cSrcweir 9*cdf0e10cSrcweir public class Sender 10*cdf0e10cSrcweir { 11*cdf0e10cSrcweir // Constructor params: 12*cdf0e10cSrcweir private StatusWindow status = null; 13*cdf0e10cSrcweir private OfficeAttachment attachments = null; 14*cdf0e10cSrcweir private String replyto = ""; 15*cdf0e10cSrcweir private String subject = ""; 16*cdf0e10cSrcweir private String comment = ""; 17*cdf0e10cSrcweir private String hostname = ""; 18*cdf0e10cSrcweir private String newsgroup = ""; 19*cdf0e10cSrcweir private String statusLine = ""; 20*cdf0e10cSrcweir 21*cdf0e10cSrcweir 22*cdf0e10cSrcweir 23*cdf0e10cSrcweir public Sender( StatusWindow sw, OfficeAttachment attach, String reply, 24*cdf0e10cSrcweir String sub, String com, String host, String group ) 25*cdf0e10cSrcweir { 26*cdf0e10cSrcweir status = sw; 27*cdf0e10cSrcweir attachments = attach; 28*cdf0e10cSrcweir replyto = reply; 29*cdf0e10cSrcweir subject = sub; 30*cdf0e10cSrcweir comment = com; 31*cdf0e10cSrcweir hostname = host; 32*cdf0e10cSrcweir newsgroup = group; 33*cdf0e10cSrcweir } 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir public boolean sendMail() 38*cdf0e10cSrcweir { 39*cdf0e10cSrcweir int statusPos = 5; 40*cdf0e10cSrcweir try 41*cdf0e10cSrcweir { 42*cdf0e10cSrcweir attachments.createTempDocs(); 43*cdf0e10cSrcweir // Property for any information 44*cdf0e10cSrcweir Properties props = new Properties(); 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir // Create unique session (null is unused authenticator info) 47*cdf0e10cSrcweir statusLine = "Creating unique session"; 48*cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); // 5 49*cdf0e10cSrcweir Session session = Session.getInstance( props, null ); 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir // Create message 52*cdf0e10cSrcweir statusPos++; // 6 53*cdf0e10cSrcweir statusLine = "Creating message"; 54*cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); 55*cdf0e10cSrcweir MimeMessage message = new MimeMessage( session ); 56*cdf0e10cSrcweir message.setFrom( new InternetAddress( replyto ) ); 57*cdf0e10cSrcweir message.setSubject( subject ); 58*cdf0e10cSrcweir message.setText( comment ); 59*cdf0e10cSrcweir message.addHeader( "Newsgroups", newsgroup ); 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir // Buildup bodypart with text and attachments 62*cdf0e10cSrcweir Multipart multipart = new MimeMultipart(); 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir BodyPart messageBodyPart = new MimeBodyPart(); 65*cdf0e10cSrcweir messageBodyPart.setText( comment ); 66*cdf0e10cSrcweir multipart.addBodyPart( messageBodyPart ); 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir statusPos++; // 7 69*cdf0e10cSrcweir statusLine = "Adding attachment(s)"; 70*cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); 71*cdf0e10cSrcweir File attachs[] = attachments.getAttachments(); 72*cdf0e10cSrcweir for(int i=0; i < attachs.length; i++ ) 73*cdf0e10cSrcweir { 74*cdf0e10cSrcweir //System.out.println( "Adding file: " + attachs[i].getName() ); 75*cdf0e10cSrcweir messageBodyPart = new MimeBodyPart(); 76*cdf0e10cSrcweir DataSource filesource = new FileDataSource( attachs[i] ); 77*cdf0e10cSrcweir messageBodyPart.setDataHandler( new DataHandler( filesource )); 78*cdf0e10cSrcweir messageBodyPart.setFileName( attachs[i].getName() ); 79*cdf0e10cSrcweir multipart.addBodyPart( messageBodyPart ); 80*cdf0e10cSrcweir } 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir // Add multipart to mail 83*cdf0e10cSrcweir message.setContent( multipart ); 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir // Create and send NNTP transport 86*cdf0e10cSrcweir statusPos += 2; // 9 87*cdf0e10cSrcweir statusLine = "Creating NNTP transport"; 88*cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); 89*cdf0e10cSrcweir Transport transport = new NNTPTransport( session, new URLName( "news:" + newsgroup )); 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir // Null parameters are for user name and password 92*cdf0e10cSrcweir statusPos++; // 10 93*cdf0e10cSrcweir statusLine = "Connecting to mail server"; 94*cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); 95*cdf0e10cSrcweir transport.connect( hostname, null, null ); 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir statusPos++; // 11 98*cdf0e10cSrcweir statusLine = "Sending message"; 99*cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); 100*cdf0e10cSrcweir transport.sendMessage( message, message.getAllRecipients() ); 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir statusPos++; // 12 103*cdf0e10cSrcweir statusLine = "Closing transport"; 104*cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); 105*cdf0e10cSrcweir transport.close(); 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir // Clean up when finished 108*cdf0e10cSrcweir attachments.removeTempDocs(); 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir return true; 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir catch( MessagingException me ) 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir if( statusPos == 10 ) 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir statusLine = "Error connecting (User authentication?)"; 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); 119*cdf0e10cSrcweir System.out.println( "Error sending message: "); 120*cdf0e10cSrcweir me.printStackTrace(); 121*cdf0e10cSrcweir return false; 122*cdf0e10cSrcweir } 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir } 127