/** This class is not currently used and therefore will not be compiled by makegli. */
package org.greenstone.gatherer.feedback;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
import java.util.Locale;
import java.util.ResourceBundle;
import java.text.MessageFormat;
import javax.swing.*;
/**
* This is class will provides the ability to send email that contains 2 attachment file.
* The arguments that user need to provide are email address of the recepient,first file
* to be attached, SMTP address of sender email address, email adress of the sender and
* second file to be attached and the code. It has to be in this order.
* Here we will expect the 2 file to be attached were parser.jar and jar file of all the
* 6 xml files made in the Reporting Feedback sequence.
*
The email send will have subject = Send Feedback followed with the code.
*
The message will be :
*
Hereby attached 2 jar files.Please download them at the same place and parser.jar is an executable jar file.
*
Please keep referencing to this ID Code.
*
ID Code: code
* This email will only can be send to one recepient only.
* @author Veronica Liesaputra
*/
public class SendMail
{
/**
* This is variable will hold the arguments provided by user in order to send
* 2 attachment files through email.
*/
private String[] argument;
/**
* This variable will hold the resource of the words that is stored in Messages.properties file.
* The calling using messages.getString(someString) will caused someString to be translated
* into some other string that is hold in that file.usually it will caused it to be translated
* to the language that the user use or choose to have as stated in Locale.
*/
private static ResourceBundle msg;
/**
* This variable will hold the jar file unique code.
* For example: Jar123File.jar then file_code = 123
*/
private String file_code;
/**
* This is the flag that will say whether or not the sending is successful.
* If failed = false means its succesful otherwise its failed.
*/
private boolean failed = false;
/**
* Crates instance of SendMail
*/
public SendMail () {}
/**
* This method will be send an email with 2 file attachment to a recepient
* email address using the to email address and to smpt adress as given in
* the arguments. This email will also have the code as specified code.
* If the sending is failed then it will give user a message saying whats
* the possible error of it, and it will make the jar file changed into
* Unsend_Jar123File.jar file.
* If the sending is succesful then it will try to send all the other jar files
* that were failed to send before.And then it will deletes all the jar files
* that are send successfully.
* @param args the argument provided to send the email.
* @param messages hold the resource of the words that is stored in Messages.properties file.
* @param code the code of the email to be send.
*/
public void sendMail(String[] args,ResourceBundle messages,String code)
{
argument = args;
msg = messages;
file_code = code;
if(args.length<5)
{
System.out.println("Args for example:");
System.out.println("String[] args = new String(5);");
System.out.println("args[0] = " + "recepient@maildomain.com;");
System.out.println("args[1] = " + "Jar123.jar;");
System.out.println("args[2] = " + "mail.waikato.ac.nz;");
System.out.println("args[3] = " + "to@cs.waikato.ac.nz;");
System.out.println("args[4] = " + "parser.jar;");
return;
}
File attachment;
attachment =new File(args[1]);
if(attachment.isDirectory())
{
System.out.println("Attachment should not be a directory.\n It should be a file.");
return;
}
if(!attachment.exists())
{
System.out.println("Attachment file not found.");
return;
}
Properties p;
p = System.getProperties();
p.put("mail.smtp.host",args[2]);
Session session;
session =Session.getInstance(p);
Message mail;
mail =new MimeMessage(session);
Transport t;
try
{
Address [] addresses;
addresses = new Address[]{new InternetAddress(args[0])};
mail.setFrom(new InternetAddress(args[3]));
mail.setSubject("Send Feedback" + code);
MimeBodyPart part1;
part1 =new MimeBodyPart();
String attmnt;
attmnt = "Hereby attached 2 jar files.Please download them at the same place and parser.jar " +
"is an executable jar file.\n" + "Please keep referencing to this ID Code.\n" +
"ID Code:";
part1.setText(attmnt+" " + code);
MimeBodyPart part2;
part2 =new MimeBodyPart();
FileDataSource source;
source =new FileDataSource(args[1]);
part2.setDataHandler(new DataHandler(source));
part2.setFileName(source.getName());
MimeBodyPart part3;
part3 = new MimeBodyPart();
FileDataSource source2;
source2 = new FileDataSource(args[4]);
part3.setDataHandler(new DataHandler(source2));
part3.setFileName(source2.getName());
Multipart part;
part =new MimeMultipart();
part.addBodyPart(part1);
part.addBodyPart(part2);
part.addBodyPart(part3);
mail.setContent(part);
t = session.getTransport(addresses[0]);
t.addTransportListener(new javax.mail.event.TransportAdapter()
{
public void messageNotDelivered(javax.mail.event.TransportEvent te)
{
Address a[];
a =te.getInvalidAddresses();
failed = true;
System.out.println("Message not delivered for the following addresses:");
int i;
for(i=0;i 0))
{
argument[2] = s;
String s2;
s2 = (String)JOptionPane.showInputDialog(messages.getString("NotFoundProvider2"));
if ((s2 != null) && (s2.length() > 0))
argument[3] = s2;
sendMail(argument,msg,file_code);
//Conformation.setFinish(true);
return;
}
else
JOptionPane.showMessageDialog(null,messages.getString("Emaillater"),messages.getString("Error"),
JOptionPane.INFORMATION_MESSAGE);
}
catch (SendFailedException nspe)
{
failed = true;
int ans;
ans = JOptionPane.showConfirmDialog(null,messages.getString("SendFailed"),
messages.getString("Error"),JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if (ans == JOptionPane.YES_OPTION)
{
sendMail(argument,msg,file_code);
//Conformation.setFinish(true);
return;
}
else
JOptionPane.showMessageDialog(null,messages.getString("Emaillater"),messages.getString("Error"),
JOptionPane.INFORMATION_MESSAGE);
}
catch (Exception nspe)
{
failed = true;
JOptionPane.showMessageDialog(null,messages.getString("InternalError")+ "\n" +
messages.getString("Emaillater"),messages.getString("Error"),
JOptionPane.ERROR_MESSAGE);
//Conformation.setFinish(true);
}
String dirname;
dirname = "xmlfeedback/";
if (failed == false)
{
File f;
f = new File(dirname + "Jar" + file_code + "File.jar");
f.delete();
File file;
file = new File(dirname + ".");
File[] fileList;
fileList = file.listFiles();
int y;
for (y = 0 ; y < fileList.length ; y++)
{
String filename;
filename = isRightJarFile(fileList[y]);
if (filename != null)
{
renameTo(fileList[y],new File(filename));
argument[1] = filename;
sendMail(argument,msg,getCode(filename));
}
}
}
else
{
File f2;
f2 = new File(dirname + "Jar" + file_code + "File.jar");
renameTo(f2,new File(dirname + "Unsend_Jar" + file_code + "File.jar"));
}
//Conformation.setFinish(true);
}
/**
* This method will rename file.
* @param src is the name of the file to be renamed.
* @param dest is the desired file name.
*/
private void renameTo (File src, File dest)
{
if (src.renameTo(dest) == false)
{
try
{
FileInputStream in;
in = new FileInputStream(src);
FileOutputStream out;
out = new FileOutputStream(dest);
int next;
while (true)
{
next = in.read();
if (next == -1)
break;
else
out.write((byte) next);
}
in.close();
out.close();
}
catch (IOException exp) {exp.printStackTrace();}
}
}
/**
* This method will get the extension of the file.
* @param f is the file that we want to get the extension.
* @return the file extension
*/
private String getExtension(File f)
{
String ext;
ext = null;
String s;
s = f.getName();
int i;
i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1)
{
ext = s.substring(i+1).toLowerCase();
}
return ext;
}
/**
* This method will get all the name of the file after "_" if the file its a jar file.
* For example Unsend_Jar123File.jar then this method will return Jar123File, otherwise
* file with any other format will return null.
* @param f is the file that we want to get the name.
*/
private String isRightJarFile(File f)
{
String ext;
ext = null;
if (f.isDirectory() == true)
return null;
if (getExtension(f).compareTo("jar") == 0)
{
String s;
s = f.getName();
int i;
i = s.lastIndexOf("_");
if (i > 0 && i < s.length() - 1)
{
ext = s.substring(i+1,s.length());
}
else
ext = null;
return ext;
}
else
return null;
}
/**
* This method will getting the code that inside the Jar filename.
* For example Jar123File then this method will return 123.
*/
private String getCode (String s)
{
int i;
i = s.lastIndexOf("Jar");
int j;
j = s.indexOf("File");
String code;
code = s.substring((i+3),j);
return code;
}
}