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