1*cd519653SAndrew Rist /************************************************************** 2*cd519653SAndrew Rist * 3*cd519653SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*cd519653SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*cd519653SAndrew Rist * distributed with this work for additional information 6*cd519653SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*cd519653SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*cd519653SAndrew Rist * "License"); you may not use this file except in compliance 9*cd519653SAndrew Rist * with the License. You may obtain a copy of the License at 10*cd519653SAndrew Rist * 11*cd519653SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*cd519653SAndrew Rist * 13*cd519653SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*cd519653SAndrew Rist * software distributed under the License is distributed on an 15*cd519653SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*cd519653SAndrew Rist * KIND, either express or implied. See the License for the 17*cd519653SAndrew Rist * specific language governing permissions and limitations 18*cd519653SAndrew Rist * under the License. 19*cd519653SAndrew Rist * 20*cd519653SAndrew Rist *************************************************************/ 21*cd519653SAndrew Rist 22cdf0e10cSrcweir import javax.mail.*; 23cdf0e10cSrcweir import javax.mail.internet.*; 24cdf0e10cSrcweir import com.msoft.mail.provider.nntp.NNTPTransport; 25cdf0e10cSrcweir import java.util.Properties; 26cdf0e10cSrcweir import java.io.*; 27cdf0e10cSrcweir import javax.activation.*; 28cdf0e10cSrcweir 29cdf0e10cSrcweir 30cdf0e10cSrcweir public class Sender 31cdf0e10cSrcweir { 32cdf0e10cSrcweir // Constructor params: 33cdf0e10cSrcweir private StatusWindow status = null; 34cdf0e10cSrcweir private OfficeAttachment attachments = null; 35cdf0e10cSrcweir private String replyto = ""; 36cdf0e10cSrcweir private String subject = ""; 37cdf0e10cSrcweir private String comment = ""; 38cdf0e10cSrcweir private String hostname = ""; 39cdf0e10cSrcweir private String newsgroup = ""; 40cdf0e10cSrcweir private String statusLine = ""; 41cdf0e10cSrcweir 42cdf0e10cSrcweir 43cdf0e10cSrcweir Sender( StatusWindow sw, OfficeAttachment attach, String reply, String sub, String com, String host, String group )44cdf0e10cSrcweir public Sender( StatusWindow sw, OfficeAttachment attach, String reply, 45cdf0e10cSrcweir String sub, String com, String host, String group ) 46cdf0e10cSrcweir { 47cdf0e10cSrcweir status = sw; 48cdf0e10cSrcweir attachments = attach; 49cdf0e10cSrcweir replyto = reply; 50cdf0e10cSrcweir subject = sub; 51cdf0e10cSrcweir comment = com; 52cdf0e10cSrcweir hostname = host; 53cdf0e10cSrcweir newsgroup = group; 54cdf0e10cSrcweir } 55cdf0e10cSrcweir 56cdf0e10cSrcweir 57cdf0e10cSrcweir sendMail()58cdf0e10cSrcweir public boolean sendMail() 59cdf0e10cSrcweir { 60cdf0e10cSrcweir int statusPos = 5; 61cdf0e10cSrcweir try 62cdf0e10cSrcweir { 63cdf0e10cSrcweir attachments.createTempDocs(); 64cdf0e10cSrcweir // Property for any information 65cdf0e10cSrcweir Properties props = new Properties(); 66cdf0e10cSrcweir 67cdf0e10cSrcweir // Create unique session (null is unused authenticator info) 68cdf0e10cSrcweir statusLine = "Creating unique session"; 69cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); // 5 70cdf0e10cSrcweir Session session = Session.getInstance( props, null ); 71cdf0e10cSrcweir 72cdf0e10cSrcweir // Create message 73cdf0e10cSrcweir statusPos++; // 6 74cdf0e10cSrcweir statusLine = "Creating message"; 75cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); 76cdf0e10cSrcweir MimeMessage message = new MimeMessage( session ); 77cdf0e10cSrcweir message.setFrom( new InternetAddress( replyto ) ); 78cdf0e10cSrcweir message.setSubject( subject ); 79cdf0e10cSrcweir message.setText( comment ); 80cdf0e10cSrcweir message.addHeader( "Newsgroups", newsgroup ); 81cdf0e10cSrcweir 82cdf0e10cSrcweir // Buildup bodypart with text and attachments 83cdf0e10cSrcweir Multipart multipart = new MimeMultipart(); 84cdf0e10cSrcweir 85cdf0e10cSrcweir BodyPart messageBodyPart = new MimeBodyPart(); 86cdf0e10cSrcweir messageBodyPart.setText( comment ); 87cdf0e10cSrcweir multipart.addBodyPart( messageBodyPart ); 88cdf0e10cSrcweir 89cdf0e10cSrcweir statusPos++; // 7 90cdf0e10cSrcweir statusLine = "Adding attachment(s)"; 91cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); 92cdf0e10cSrcweir File attachs[] = attachments.getAttachments(); 93cdf0e10cSrcweir for(int i=0; i < attachs.length; i++ ) 94cdf0e10cSrcweir { 95cdf0e10cSrcweir //System.out.println( "Adding file: " + attachs[i].getName() ); 96cdf0e10cSrcweir messageBodyPart = new MimeBodyPart(); 97cdf0e10cSrcweir DataSource filesource = new FileDataSource( attachs[i] ); 98cdf0e10cSrcweir messageBodyPart.setDataHandler( new DataHandler( filesource )); 99cdf0e10cSrcweir messageBodyPart.setFileName( attachs[i].getName() ); 100cdf0e10cSrcweir multipart.addBodyPart( messageBodyPart ); 101cdf0e10cSrcweir } 102cdf0e10cSrcweir 103cdf0e10cSrcweir // Add multipart to mail 104cdf0e10cSrcweir message.setContent( multipart ); 105cdf0e10cSrcweir 106cdf0e10cSrcweir // Create and send NNTP transport 107cdf0e10cSrcweir statusPos += 2; // 9 108cdf0e10cSrcweir statusLine = "Creating NNTP transport"; 109cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); 110cdf0e10cSrcweir Transport transport = new NNTPTransport( session, new URLName( "news:" + newsgroup )); 111cdf0e10cSrcweir 112cdf0e10cSrcweir // Null parameters are for user name and password 113cdf0e10cSrcweir statusPos++; // 10 114cdf0e10cSrcweir statusLine = "Connecting to mail server"; 115cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); 116cdf0e10cSrcweir transport.connect( hostname, null, null ); 117cdf0e10cSrcweir 118cdf0e10cSrcweir statusPos++; // 11 119cdf0e10cSrcweir statusLine = "Sending message"; 120cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); 121cdf0e10cSrcweir transport.sendMessage( message, message.getAllRecipients() ); 122cdf0e10cSrcweir 123cdf0e10cSrcweir statusPos++; // 12 124cdf0e10cSrcweir statusLine = "Closing transport"; 125cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); 126cdf0e10cSrcweir transport.close(); 127cdf0e10cSrcweir 128cdf0e10cSrcweir // Clean up when finished 129cdf0e10cSrcweir attachments.removeTempDocs(); 130cdf0e10cSrcweir 131cdf0e10cSrcweir return true; 132cdf0e10cSrcweir } 133cdf0e10cSrcweir catch( MessagingException me ) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir if( statusPos == 10 ) 136cdf0e10cSrcweir { 137cdf0e10cSrcweir statusLine = "Error connecting (User authentication?)"; 138cdf0e10cSrcweir } 139cdf0e10cSrcweir status.setStatus( statusPos, statusLine ); 140cdf0e10cSrcweir System.out.println( "Error sending message: "); 141cdf0e10cSrcweir me.printStackTrace(); 142cdf0e10cSrcweir return false; 143cdf0e10cSrcweir } 144cdf0e10cSrcweir 145cdf0e10cSrcweir } 146cdf0e10cSrcweir 147cdf0e10cSrcweir } 148