source: trunk/gli/src/org/greenstone/gatherer/feedback/SendMail.java@ 9335

Last change on this file since 9335 was 9335, checked in by mdewsnip, 19 years ago

Added a comment to these five files indicating that they are not used.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 KB
Line 
1/** This class is not currently used and therefore will not be compiled by makegli. */
2
3package org.greenstone.gatherer.feedback;
4
5import java.util.Properties;
6import javax.mail.*;
7import javax.mail.internet.*;
8import javax.activation.*;
9import java.io.*;
10import java.util.Locale;
11import java.util.ResourceBundle;
12import java.text.MessageFormat;
13import javax.swing.*;
14
15/**
16 * This is class will provides the ability to send email that contains 2 attachment file.
17 * The arguments that user need to provide are email address of the recepient,first file
18 * to be attached, SMTP address of sender email address, email adress of the sender and
19 * second file to be attached and the code. It has to be in this order.
20 * Here we will expect the 2 file to be attached were parser.jar and jar file of all the
21 * 6 xml files made in the Reporting Feedback sequence.
22 * <br>The email send will have subject = Send Feedback followed with the code.
23 * <br>The message will be :
24 * <br>Hereby attached 2 jar files.Please download them at the same place and parser.jar is an executable jar file.
25 * <br>Please keep referencing to this ID Code.
26 * <br>ID Code: code
27 * This email will only can be send to one recepient only.
28 * @author Veronica Liesaputra
29 */
30public class SendMail
31{
32 /**
33 * This is variable will hold the arguments provided by user in order to send
34 * 2 attachment files through email.
35 */
36 private String[] argument;
37 /**
38 * This variable will hold the resource of the words that is stored in Messages.properties file.
39 * The calling using messages.getString(someString) will caused someString to be translated
40 * into some other string that is hold in that file.usually it will caused it to be translated
41 * to the language that the user use or choose to have as stated in Locale.
42 */
43 private static ResourceBundle msg;
44 /**
45 * This variable will hold the jar file unique code.
46 * For example: Jar123File.jar then file_code = 123
47 */
48 private String file_code;
49 /**
50 * This is the flag that will say whether or not the sending is successful.
51 * If failed = false means its succesful otherwise its failed.
52 */
53 private boolean failed = false;
54
55 /**
56 * Crates instance of SendMail
57 */
58 public SendMail () {}
59
60 /**
61 * This method will be send an email with 2 file attachment to a recepient
62 * email address using the to email address and to smpt adress as given in
63 * the arguments. This email will also have the code as specified code.
64 * If the sending is failed then it will give user a message saying whats
65 * the possible error of it, and it will make the jar file changed into
66 * Unsend_Jar123File.jar file.
67 * If the sending is succesful then it will try to send all the other jar files
68 * that were failed to send before.And then it will deletes all the jar files
69 * that are send successfully.
70 * @param args the argument provided to send the email.
71 * @param messages hold the resource of the words that is stored in Messages.properties file.
72 * @param code the code of the email to be send.
73 */
74 public void sendMail(String[] args,ResourceBundle messages,String code)
75 {
76
77 argument = args;
78 msg = messages;
79 file_code = code;
80
81 if(args.length<5)
82 {
83 System.out.println("Args for example:");
84 System.out.println("String[] args = new String(5);");
85 System.out.println("args[0] = " + "[email protected];");
86 System.out.println("args[1] = " + "Jar123.jar;");
87 System.out.println("args[2] = " + "mail.waikato.ac.nz;");
88 System.out.println("args[3] = " + "[email protected];");
89 System.out.println("args[4] = " + "parser.jar;");
90 return;
91 }
92
93 File attachment;
94 attachment =new File(args[1]);
95
96 if(attachment.isDirectory())
97 {
98 System.out.println("Attachment should not be a directory.\n It should be a file.");
99 return;
100 }
101 if(!attachment.exists())
102 {
103 System.out.println("Attachment file not found.");
104 return;
105 }
106
107 Properties p;
108 p = System.getProperties();
109
110 p.put("mail.smtp.host",args[2]);
111
112 Session session;
113 session =Session.getInstance(p);
114
115 Message mail;
116 mail =new MimeMessage(session);
117
118 Transport t;
119
120 try
121 {
122
123 Address [] addresses;
124 addresses = new Address[]{new InternetAddress(args[0])};
125
126 mail.setFrom(new InternetAddress(args[3]));
127
128 mail.setSubject("Send Feedback" + code);
129
130 MimeBodyPart part1;
131 part1 =new MimeBodyPart();
132 String attmnt;
133 attmnt = "Hereby attached 2 jar files.Please download them at the same place and parser.jar " +
134 "is an executable jar file.\n" + "Please keep referencing to this ID Code.\n" +
135 "ID Code:";
136 part1.setText(attmnt+" " + code);
137
138 MimeBodyPart part2;
139 part2 =new MimeBodyPart();
140 FileDataSource source;
141 source =new FileDataSource(args[1]);
142 part2.setDataHandler(new DataHandler(source));
143 part2.setFileName(source.getName());
144
145 MimeBodyPart part3;
146 part3 = new MimeBodyPart();
147 FileDataSource source2;
148 source2 = new FileDataSource(args[4]);
149 part3.setDataHandler(new DataHandler(source2));
150 part3.setFileName(source2.getName());
151
152 Multipart part;
153 part =new MimeMultipart();
154
155 part.addBodyPart(part1);
156 part.addBodyPart(part2);
157 part.addBodyPart(part3);
158
159 mail.setContent(part);
160
161 t = session.getTransport(addresses[0]);
162
163 t.addTransportListener(new javax.mail.event.TransportAdapter()
164 {
165 public void messageNotDelivered(javax.mail.event.TransportEvent te)
166 {
167 Address a[];
168 a =te.getInvalidAddresses();
169 failed = true;
170 System.out.println("Message not delivered for the following addresses:");
171 int i;
172 for(i=0;i<a.length;i++)
173 {
174 System.out.println(a[i]);
175 }
176 }
177 });
178
179 t.connect();
180
181
182 t.sendMessage(mail,addresses);
183
184
185 t.close();
186
187 }
188 catch (NoSuchProviderException e)
189 {
190 failed = true;
191 System.out.println("Provider not found.");
192 String s;
193 s = (String)JOptionPane.showInputDialog(messages.getString("NotFoundProvider"));
194
195 if ((s != null) && (s.length() > 0))
196 {
197 argument[2] = s;
198
199 String s2;
200 s2 = (String)JOptionPane.showInputDialog(messages.getString("NotFoundProvider2"));
201
202 if ((s2 != null) && (s2.length() > 0))
203 argument[3] = s2;
204
205 sendMail(argument,msg,file_code);
206 //Conformation.setFinish(true);
207 return;
208 }
209 else
210 JOptionPane.showMessageDialog(null,messages.getString("Emaillater"),messages.getString("Error"),
211 JOptionPane.INFORMATION_MESSAGE);
212
213 }
214 catch (SendFailedException nspe)
215 {
216 failed = true;
217 int ans;
218 ans = JOptionPane.showConfirmDialog(null,messages.getString("SendFailed"),
219 messages.getString("Error"),JOptionPane.YES_NO_OPTION,
220 JOptionPane.INFORMATION_MESSAGE);
221 if (ans == JOptionPane.YES_OPTION)
222 {
223 sendMail(argument,msg,file_code);
224 //Conformation.setFinish(true);
225 return;
226 }
227 else
228 JOptionPane.showMessageDialog(null,messages.getString("Emaillater"),messages.getString("Error"),
229 JOptionPane.INFORMATION_MESSAGE);
230 }
231 catch (Exception nspe)
232 {
233 failed = true;
234 JOptionPane.showMessageDialog(null,messages.getString("InternalError")+ "\n" +
235 messages.getString("Emaillater"),messages.getString("Error"),
236 JOptionPane.ERROR_MESSAGE);
237 //Conformation.setFinish(true);
238 }
239
240 String dirname;
241 dirname = "xmlfeedback/";
242
243 if (failed == false)
244 {
245 File f;
246 f = new File(dirname + "Jar" + file_code + "File.jar");
247 f.delete();
248
249 File file;
250 file = new File(dirname + ".");
251 File[] fileList;
252 fileList = file.listFiles();
253
254 int y;
255 for (y = 0 ; y < fileList.length ; y++)
256 {
257 String filename;
258 filename = isRightJarFile(fileList[y]);
259 if (filename != null)
260 {
261 renameTo(fileList[y],new File(filename));
262 argument[1] = filename;
263 sendMail(argument,msg,getCode(filename));
264 }
265 }
266 }
267 else
268 {
269 File f2;
270 f2 = new File(dirname + "Jar" + file_code + "File.jar");
271 renameTo(f2,new File(dirname + "Unsend_Jar" + file_code + "File.jar"));
272 }
273
274 //Conformation.setFinish(true);
275 }
276
277 /**
278 * This method will rename file.
279 * @param src is the name of the file to be renamed.
280 * @param dest is the desired file name.
281 */
282 private void renameTo (File src, File dest)
283 {
284 if (src.renameTo(dest) == false)
285 {
286 try
287 {
288 FileInputStream in;
289 in = new FileInputStream(src);
290 FileOutputStream out;
291 out = new FileOutputStream(dest);
292 int next;
293
294 while (true)
295 {
296 next = in.read();
297 if (next == -1)
298 break;
299 else
300 out.write((byte) next);
301 }
302
303 in.close();
304 out.close();
305 }
306 catch (IOException exp) {exp.printStackTrace();}
307 }
308 }
309
310 /**
311 * This method will get the extension of the file.
312 * @param f is the file that we want to get the extension.
313 * @return the file extension
314 */
315 private String getExtension(File f)
316 {
317 String ext;
318 ext = null;
319 String s;
320 s = f.getName();
321 int i;
322 i = s.lastIndexOf('.');
323
324 if (i > 0 && i < s.length() - 1)
325 {
326 ext = s.substring(i+1).toLowerCase();
327 }
328 return ext;
329 }
330
331 /**
332 * This method will get all the name of the file after "_" if the file its a jar file.
333 * For example Unsend_Jar123File.jar then this method will return Jar123File, otherwise
334 * file with any other format will return null.
335 * @param f is the file that we want to get the name.
336 */
337 private String isRightJarFile(File f)
338 {
339 String ext;
340 ext = null;
341
342 if (f.isDirectory() == true)
343 return null;
344
345 if (getExtension(f).compareTo("jar") == 0)
346 {
347 String s;
348 s = f.getName();
349 int i;
350 i = s.lastIndexOf("_");
351
352 if (i > 0 && i < s.length() - 1)
353 {
354 ext = s.substring(i+1,s.length());
355 }
356 else
357 ext = null;
358
359 return ext;
360 }
361 else
362 return null;
363 }
364
365 /**
366 * This method will getting the code that inside the Jar filename.
367 * For example Jar123File then this method will return 123.
368 */
369 private String getCode (String s)
370 {
371 int i;
372 i = s.lastIndexOf("Jar");
373 int j;
374 j = s.indexOf("File");
375 String code;
376 code = s.substring((i+3),j);
377 return code;
378 }
379}
380
381
382
383
384
385
386
387
388
Note: See TracBrowser for help on using the repository browser.