1 26 27 package mailsb; 28 29 import java.io.BufferedReader; 30 import java.io.InputStreamReader; 31 import java.io.IOException; 32 import javax.naming.InitialContext; 33 import javax.naming.Context; 34 import javax.rmi.PortableRemoteObject; 35 36 41 public class ClientMailer { 42 43 46 private static final int SESSION_CASE = 1; 47 48 51 private static final int MIMEPARTDS_CASE = 2; 52 53 56 private static String content = null; 57 58 61 private ClientMailer() { 62 63 } 64 65 73 private static String getTextFromUser(BufferedReader bufferedReader, String prompt) throws IOException { 74 75 boolean isSet = false; 76 String txt = null; 77 while (!isSet) { 78 System.out.print(prompt + " :"); 79 txt = bufferedReader.readLine(); 80 if (!txt.equals("")) { 81 isSet = true; 82 } else { 83 System.out.println("'" + prompt + "' can't be empty."); 84 } 85 } 86 return txt; 87 } 88 89 93 public static void main(String[] args) { 94 95 if (args.length < 1) { 97 System.err.println("Syntax is : java mailsb.ClientMailer <SessionMailer | MimePartDSMailer>"); 98 System.exit(2); 99 } 100 101 102 int argType = 0; 103 if (args[0].equalsIgnoreCase("SessionMailer")) { 104 argType = SESSION_CASE; 105 } else if (args[0].equalsIgnoreCase("MimePartDSMailer")) { 106 argType = MIMEPARTDS_CASE; 107 } else { 108 System.err.println("Invalid type '" + args[0] + "', valid syntax is : java mailsb.ClientMailer <SessionMailer | MimePartDSMailer>"); 109 System.exit(2); 110 } 111 112 Context initialContext = null; 114 try { 115 initialContext = new InitialContext(); 116 } catch (Exception e) { 117 System.err.println("Cannot get initial context for JNDI: " + e.getMessage()); 118 System.exit(2); 119 } 120 121 switch (argType) { 122 case SESSION_CASE : 123 sessionMailer(initialContext); 124 break; 125 case MIMEPARTDS_CASE : 126 if (args.length > 1) { 127 content = "Content of mail :"; 128 for (int i = 1; i < args.length; i++) { 129 content += args[i]; 130 } 131 } 132 mimePartDSMailer(initialContext); 133 break; 134 default : 135 136 } 137 138 139 } 140 144 private static void mimePartDSMailer(Context initialContext) { 145 146 MimePartDSMailerHome mimePartDSMailerHome = null; 148 try { 149 System.out.print("Looking up the Session mailer home..."); 150 mimePartDSMailerHome = (MimePartDSMailerHome) PortableRemoteObject.narrow(initialContext 151 .lookup("MimePartDSMailerHome"), MimePartDSMailerHome.class); 152 System.out.println("OK !"); 153 } catch (Exception e) { 154 System.out.println("failed !"); 155 System.err.println("Cannot lookup MimePartDSMailerHome: " + e.getMessage()); 156 System.exit(2); 157 } 158 159 MimePartDSMailer mimePartDSMailer = null; 161 try { 162 System.out.print("Creating a MimePartDS mailer bean ..."); 163 mimePartDSMailer = mimePartDSMailerHome.create("MimePartDS mailer"); 164 System.out.println("OK !"); 165 } catch (Exception e) { 166 System.out.println("failed !"); 167 System.err.println("Cannot create MimePartDSMailerBean: " + e.getMessage()); 168 System.exit(2); 169 } 170 171 if (content == null) { 172 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); 174 175 try { 176 System.out.println("Content of the mail (Only one line)"); 177 content = getTextFromUser(bufferedReader, "Content"); 178 } catch (IOException e) { 179 System.err.println("Cannot read line from the bufferedReader : " + e.getMessage()); 180 System.exit(2); 181 } 182 } 183 184 try { 186 System.out.print("Setting the message with given content '" + content + "'..."); 187 mimePartDSMailer.setMessage(content); 188 System.out.println("OK !"); 189 } catch (Exception e) { 190 System.out.println("failed !"); 191 System.err.println("Cannot set args of the message : " + e); 192 System.exit(2); 193 } 194 195 try { 197 System.out.print("Sending the message..."); 198 mimePartDSMailer.send(); 199 System.out.println("OK !"); 200 } catch (Exception e) { 201 System.out.println("Failed !"); 202 System.err.println("Cannot send the message with MailerBean:" + e); 203 System.exit(2); 204 } 205 } 206 207 211 private static void sessionMailer(Context initialContext) { 212 SessionMailerHome sessionMailerHome = null; 214 try { 215 System.out.print("Looking up the Session mailer home..."); 216 sessionMailerHome = (SessionMailerHome) PortableRemoteObject.narrow(initialContext 217 .lookup("SessionMailerHome"), SessionMailerHome.class); 218 System.out.println("OK !"); 219 } catch (Exception e) { 220 System.out.println("failed !"); 221 System.err.println("Cannot lookup MailerHome: " + e.getMessage()); 222 System.exit(2); 223 } 224 225 SessionMailer sessionMailer = null; 227 try { 228 System.out.print("Creating a session mailer bean..."); 229 sessionMailer = sessionMailerHome.create("mailer"); 230 System.out.println("OK !"); 231 } catch (Exception e) { 232 System.out.println("failed !"); 233 System.err.println("Cannot create MailerBean: " + e.getMessage()); 234 System.exit(2); 235 } 236 237 String toRecipient = null; 238 String subject = null; 239 String content = null; 240 241 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); 243 244 try { 245 System.out.println("Recipient is the 'to' field of a mail"); 246 toRecipient = getTextFromUser(bufferedReader, "TO"); 247 248 System.out.println("Subject of the mail"); 249 subject = getTextFromUser(bufferedReader, "Subject"); 250 251 System.out.println("Content of the mail (Only one line)"); 252 content = getTextFromUser(bufferedReader, "Content"); 253 254 } catch (IOException e) { 255 System.err.println("Cannot read line from the bufferedReader : " + e.getMessage()); 256 System.exit(2); 257 } 258 259 try { 261 System.out.print("Setting the message with given args..."); 262 sessionMailer.setMessage(toRecipient, subject, content); 263 System.out.println("OK !"); 264 } catch (Exception e) { 265 System.out.println("failed !"); 266 System.err.println("Cannot set args of the message : " + e); 267 System.exit(2); 268 } 269 270 try { 272 System.out.print("Sending the message..."); 273 sessionMailer.send(); 274 System.out.println("OK !"); 275 } catch (Exception e) { 276 System.out.println("Failed !"); 277 System.err.println("Cannot send the message with MailerBean:" + e); 278 System.exit(2); 279 } 280 } 281 282 } | Popular Tags |