1 64 65 package com.jcorporate.expresso.core.utility; 66 67 import com.jcorporate.expresso.core.job.ServerException; 68 import com.jcorporate.expresso.core.misc.EMailSender; 69 70 import java.io.File ; 71 import java.util.Vector ; 72 73 74 77 public class MailFiles { 78 79 82 public MailFiles() { 83 super(); 84 } 85 86 91 public MailFiles(String [] args) { 92 String myName = (getClass().getName() + 93 ".MailFiles(String[])"); 94 95 try { 96 if ((args.length != 6) && (args.length != 8)) { 97 System.out.println("Usage: MailFiles mailserver mailFrom " + 98 "email-address " + 99 "'subject' 'Message' directory-or-file " + 100 "[SMTP-auth-user SMTP-auth-password]"); 101 System.exit(1); 102 } 103 104 String mailServer = args[0]; 105 String mailFrom = args[1]; 106 String sendToUser = args[2]; 107 String subject = args[3]; 108 String message = args[4]; 109 String dirOrFile = args[5]; 110 String SMTPUser = null; 111 String SMTPPassword = null; 112 113 if (args.length == 8) { 114 SMTPUser = args[6]; 115 SMTPPassword = args[7]; 116 } 117 118 System.out.println("Sending files '" + dirOrFile + "' to user " + 119 sendToUser + " with message '" + message + "'"); 120 121 try { 122 Vector fileNameList = new Vector (4); 123 File myFile = new File (dirOrFile); 124 125 if (myFile.isDirectory()) { 126 String [] OriginalQueue = myFile.list(); 127 128 if (OriginalQueue == null) { 129 throw new ServerException(myName + ":Null array " + 130 "reading directory of " + 131 dirOrFile); 132 } 133 134 int OriginalQueueCount = OriginalQueue.length; 135 136 for (int i = 0; i < OriginalQueueCount; i++) { 137 String ls_OneLine = OriginalQueue[i].trim(); 138 System.out.println("Scanning file " + ls_OneLine); 139 fileNameList.addElement(ls_OneLine); 140 } 141 } else { 142 fileNameList.addElement(dirOrFile); 143 } 144 145 EMailSender ems = new EMailSender(); 146 ems.setToAddress(sendToUser); 147 ems.setFromAddress(mailFrom); 148 ems.setSMTPHost(mailServer); 149 ems.setSubject(subject); 150 ems.setText(message); 151 ems.addFileAttachments(fileNameList); 152 153 if (SMTPUser != null) { 154 ems.setSMTPUser(SMTPUser); 155 ems.setSMTPPassword(SMTPPassword); 156 } 157 158 ems.send(); 159 } catch (Exception e) { 160 e.printStackTrace(); 161 throw new ServerException(myName + ":Uncaught exception " + 162 "sending e-mail:" + e.getMessage()); 163 } 164 } catch (ServerException ae) { 165 System.out.println(ae.getMessage()); 166 } 167 168 169 System.out.println("Message sent"); 170 } 171 172 186 public static void main(String [] args) { 187 new MailFiles(args); 188 } 189 190 } 191 192 | Popular Tags |