001/* 002 * @(#)msgsendsample.java 1.15 99/12/06 003 * 004 * Copyright 1996-1999 Sun Microsystems, Inc. All Rights Reserved. 005 * 006 * This software is the proprietary information of Sun Microsystems, Inc. 007 * Use is subject to license terms. 008 * 009 */ 010package ca.bc.webarts.tools; 011 012import java.sql.*; 013import java.util.*; 014import java.io.*; 015import javax.mail.*; 016import javax.mail.internet.*; 017import javax.activation.*; 018 019/* Usage: 020 * java ca.bc.webarts.tools.BlindMailSend ca\bc\webarts\tools\testMailList.txt www_vanc@ieee.ca Aurora1.webarts.bc.ca TestingMail false 021 */ 022 023/** 024 * msgsendsample creates a very simple text/plain message and sends it. 025 * <p> 026 * usage: <code>java msgsendsample <i>filename from smtphost true|false</i></code> 027 * where <i>to</i> and <i>from</i> are the destination and 028 * origin email addresses, respectively, and <i>smtphost</i> 029 * is the hostname of the machine that has the smtp server 030 * running. The last parameter either turns on or turns off 031 * debugging during sending. 032 * 033 * @author Max Spivak 034 */ 035public class BlindMailSend { 036 static String msgText = "This is a message body.\nHere's the second line."; 037 static StreamTokenizer inputTokens; 038 /** 039 * 040 */ 041 static private String db2ServerName = "Aurora1"; 042 /** 043 * 044 */ 045 static private String db2ServerPort = "6790"; 046 /** 047 * 048 */ 049 static private String db2DBName = "sample"; 050 /** 051 * 052 */ 053 static private String db2UserId = "tgutwin"; 054 /** 055 * 056 */ 057 static private String db2UserPassword = "GetYourOwn"; 058 /** 059 * 060 */ 061 static final String adminEmail = "webadmin@webarts.bc.ca"; 062 /** 063 * 064 */ 065 static final String adminEmailURL = "<A HREF=\"" +adminEmail+"\">"+adminEmail+"</A>"; 066 /** 067 * 068 */ 069 static final String jdbcDriverName = "COM.ibm.db2.jdbc.app.DB2Driver"; 070 /** 071 * 072 */ 073 static final String jdbcDriverErrorMsg = "Sorry, a JDBC Driver ERROR occurred. I won't be able to process you request"; 074 /** 075 * 076 */ 077 static final String sqlErrorMsg = "Sorry, a SQL ERROR occurred. I won't be able to process your request"; 078 /** 079 * 080 */ 081 static final String reportErrorMsg = "Please copy the follwing information and email it to "+adminEmail; 082/** 083 * 084 */ 085 static final String fileErrorMsg = "File Error"; 086 087/** 088 * CONSTANT TO HOLD THE DIFFERENT TYPES OF MAIL LISTS 089 */ 090 static final short ECONTACT = 0; 091/** 092 * CONSTANT TO HOLD THE DIFFERENT TYPES OF MAIL LISTS 093 */ 094 static final short TEST = 1; 095/** 096 * CONSTANT TO HOLD THE DIFFERENT TYPES OF MAIL LISTS 097 */ 098 static final short COMPUTER = 2; 099/** 100 * CONSTANT TO HOLD THE DIFFERENT TYPES OF MAIL LISTS 101 */ 102 static final short GENERAL = 3; 103/** 104 * CONSTANT TO HOLD THE DIFFERENT TYPES OF MAIL LISTS 105 */ 106 static final short COMMUNICATIONS = 4; 107/** 108 * CONSTANT TO HOLD THE DIFFERENT TYPES OF MAIL LISTS 109 */ 110 static final short CONTROL = 5; 111/** 112 * CONSTANT TO HOLD THE DIFFERENT TYPES OF MAIL LISTS 113 */ 114 static final short IAS = 6; 115/** 116 * CONSTANT TO HOLD THE DIFFERENT TYPES OF MAIL LISTS 117 */ 118 static final short MANAGEMENT = 7; 119/** 120 * CONSTANT TO HOLD THE DIFFERENT TYPES OF MAIL LISTS 121 */ 122 static final short JOINT_ELECTRONICS = 8; 123/** 124 * CONSTANT TO HOLD THE DIFFERENT TYPES OF MAIL LISTS 125 */ 126 static final short POWER_ELECTRONICS = 9; 127/** 128 * CONSTANT TO HOLD THE DIFFERENT TYPES OF MAIL LISTS 129 */ 130 static final short POWER_ENGINEERING = 10; 131 132/** 133 * reads the passed in filename and parses it into tokens 134 * @param fName is the filename to read 135 */ 136 private static boolean initFileTokens(String inFileName){ 137 boolean retVal = true; 138 139 try{ 140 System.out.println("Tokenizing file "+inFileName); 141 FileReader fileReader = new FileReader(inFileName); 142 143 inputTokens = new StreamTokenizer(fileReader); 144 inputTokens.eolIsSignificant(false); 145 inputTokens.wordChars(33,126); 146 inputTokens.lowerCaseMode(true); 147 inputTokens.slashSlashComments(true); 148 inputTokens.slashStarComments(true); 149 } 150 catch( FileNotFoundException e ) { 151 System.err.println(fileErrorMsg); 152 System.err.println(reportErrorMsg); 153 System.err.println("e.printStackTrace()"); 154 retVal=false; 155 } 156 catch( java.lang.NullPointerException e ) { 157 System.err.println(fileErrorMsg); 158 System.err.println(reportErrorMsg); 159 System.err.println("e.printStackTrace()"); 160 retVal=false; 161 } 162 163 return retVal; 164 } 165 166 167 public static void main(String[] args) { 168 if (args.length != 6) { 169 usage(); 170 System.exit(1); 171 } 172 173 System.out.println(); 174 175 String to = args[0]; 176 String from = args[1]; 177 String host = args[2]; 178 boolean debug = Boolean.valueOf(args[3]).booleanValue(); 179 180 String subjectLine; 181 switch ((new Integer(args[4])).intValue()) { 182 case ECONTACT: 183 subjectLine = "IEEE Vancouver ECONTACT InfoByEmail"; 184 to += "econtactgroup.txt"; 185 break; 186 case TEST : 187 subjectLine = "IEEE Vancouver TEST InfoByEmail"; 188 to += "testgroup.txt"; 189 break; 190 case COMPUTER: 191 subjectLine = "IEEE Vancouver COMPUTER InfoByEmail"; 192 to += "computergroup.txt"; 193 break; 194 case GENERAL: 195 subjectLine = "IEEE Vancouver GENERAL InfoByEmail"; 196 to += "generalgroup.txt"; 197 break; 198 case COMMUNICATIONS: 199 subjectLine = "IEEE Vancouver COMMUNICATIONS InfoByEmail"; 200 to += "communicationsgroup.txt"; 201 break; 202 case CONTROL: 203 subjectLine = "IEEE Vancouver CONTROL InfoByEmail"; 204 to += "controlgroup.txt"; 205 break; 206 case IAS: 207 subjectLine = "IEEE Vancouver IAS InfoByEmail"; 208 to += "iasgroup.txt"; 209 break; 210 case MANAGEMENT: 211 subjectLine = "IEEE Vancouver MANAGEMENT InfoByEmail"; 212 to += "managementgroup.txt"; 213 break; 214 case JOINT_ELECTRONICS: 215 subjectLine = "IEEE Vancouver JOINT_ELECTRONICS InfoByEmail"; 216 to += "joint_electronicsgroup.txt"; 217 break; 218 case POWER_ELECTRONICS: 219 subjectLine = "IEEE Vancouver POWER_ELECTRONICS InfoByEmail"; 220 to += "power_electronicsgroup.txt"; 221 break; 222 case POWER_ENGINEERING: 223 subjectLine = "IEEE Vancouver POWER_ENGINEERING InfoByEmail"; 224 to += "power_engineeringgroup.txt"; 225 break; 226 default: 227 subjectLine = "IEEE Vancouver TEST InfoByEmail"; 228 to += "testgroup2.txt"; 229 } 230 String msgTextFile = args[5]; 231 StringBuffer sBuf = new StringBuffer(); 232 int numChars=0; 233 try{ 234 FileReader fileReader = new FileReader(msgTextFile); 235 byte tempChar = (byte) fileReader.read(); 236 while (tempChar != -1) { 237 sBuf.append( (char) tempChar); 238 tempChar =(byte) fileReader.read(); 239 } 240 } 241 catch( FileNotFoundException e ) { 242 System.err.println(fileErrorMsg); 243 System.err.println(reportErrorMsg); 244 System.err.println("e.printStackTrace()"); 245 246 } 247 catch (IOException ex){ 248 } 249 250 251 msgText = sBuf.toString(); 252 int startSpot = 0,startSpot1 = 0, startSpot2 = 0,startSpot3 =0, startSpot4 = 0,startSpot5 =0; 253 254 /* now strip the header 255 256 startSpot = msgText.lastIndexOf("Subject: "); 257 boolean done = false; 258 259 while(!done){ 260System.out.println("After Subject... start = "+ startSpot); 261 startSpot1 = msgText.indexOf('\n',startSpot+1 ); 262 startSpot2 = msgText.indexOf('\n',startSpot1+1 ); 263msgText+="\n\nAfter nn... start = "+ startSpot + " " +startSpot1 + " " +startSpot2 ; 264 if (startSpot1 == startSpot2 -2){ 265 done=true; 266 } 267msgText+="\n\nAfter nn... start = "+ startSpot + " " +startSpot1 + " " +startSpot2; 268 startSpot=startSpot1; 269 } 270*/ 271 String[] address = new String[500]; 272 int tempResult = 0; 273 274 /* read the list of names to send to */ 275 initFileTokens(to); 276 277 // create some properties and get the default Session 278 Properties props = new Properties(); 279 props.put("mail.smtp.host", host); 280 if (debug) props.put("mail.debug", args[3]); 281 282 Session session = Session.getDefaultInstance(props, null); 283 session.setDebug(debug); 284 285 try { 286 // create a message 287 Message msg = new MimeMessage(session); 288 msg.setFrom(new InternetAddress(from)); 289 290 int i = 0; 291 while( (tempResult = inputTokens.nextToken()) != StreamTokenizer.TT_EOF &&(tempResult == StreamTokenizer.TT_WORD)){ 292 // address[i++]=inputTokens.sval.trim(); 293 msg.addRecipient(Message.RecipientType.BCC,new InternetAddress( inputTokens.sval.trim())); 294 System.out.println("\nAdding BCC " + inputTokens.sval); 295 // InternetAddress[] address = {new InternetAddress(args[0])}; 296 } 297 298 msg.setSubject(subjectLine); 299 msg.setSentDate(new java.util.Date()); 300 // If the desired charset is known, you can use 301 // setText(text, charset) 302 msg.setText(msgText.substring(startSpot+3)); 303 304 Transport.send(msg); 305 } 306 catch (IOException ex){ 307 } 308 catch (StringIndexOutOfBoundsException ex){ 309 } 310 catch (MessagingException mex) { 311 System.out.println("\n--Exception handling in msgsendsample.java"); 312 313 mex.printStackTrace(); 314 System.out.println(); 315 Exception ex = mex; 316 do { 317 if (ex instanceof SendFailedException) { 318 SendFailedException sfex = (SendFailedException)ex; 319 Address[] invalid = sfex.getInvalidAddresses(); 320 if (invalid != null) { 321 System.out.println(" ** Invalid Addresses"); 322 if (invalid != null) { 323 for (int i = 0; i < invalid.length; i++) 324 System.out.println(" " + invalid[i]); 325 } 326 } 327 Address[] validUnsent = sfex.getValidUnsentAddresses(); 328 if (validUnsent != null) { 329 System.out.println(" ** ValidUnsent Addresses"); 330 if (validUnsent != null) { 331 for (int i = 0; i < validUnsent.length; i++) 332 System.out.println(" "+validUnsent[i]); 333 } 334 } 335 Address[] validSent = sfex.getValidSentAddresses(); 336 if (validSent != null) { 337 System.out.println(" ** ValidSent Addresses"); 338 if (validSent != null) { 339 for (int i = 0; i < validSent.length; i++) 340 System.out.println(" "+validSent[i]); 341 } 342 } 343 } 344 System.out.println(); 345 } while ((ex = ((MessagingException)ex).getNextException()) 346 != null); 347 } 348 } 349 350 private static void usage() { 351 System.out.println("usage: java msgsendsample <toListFilename> <from> <smtpServerName> <subjectLine> true|false"); 352 } 353}