1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
| package javamail;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.csvreader.CsvReader;
/**
* Classe qui permet d'envoyer un mail indiquant le nombre de personne
* d'un service qui sont passé par le proxy web de la société.
* @author billon
*
*/
public class JavaMail {
@SuppressWarnings("null")
public static boolean copier( File source, File dest ) {
try {
// Création du fichier :
dest.createNewFile();
// Declaration et ouverture des flux
java.io.FileInputStream sourceFile = new java.io.FileInputStream(source);
try {
java.io.FileOutputStream destinationFile = null;
try {
// Lecture par segment de 0.5Mo
byte buffer[]=new byte[512*1024];
int nbLecture;
while( (nbLecture = sourceFile.read(buffer)) != -1 ) {
destinationFile.write(buffer, 0, nbLecture);
}
} finally { destinationFile.close(); }
} finally { sourceFile.close(); }
} catch (IOException e) {
e.printStackTrace();
return false; // Erreur
}
return true; // Résultat OK
}
public static boolean deplacer(File source,File destination) {
if( !destination.exists() ) {
// On essaye avec renameTo
boolean result = source.renameTo(destination);
if( !result ) {
// On essaye de copier
result = true;
result &= copier(source,destination);
if(result) result &= source.delete();
} return(result);
} else {
// Si le fichier destination existe, on annule ...
return(false);
}
}
public static void main(String[] args) throws IOException {
//Récupération du fichier Properties a utiliser
String ficName= args[0];
Properties prop = new Properties();
prop.load(new FileInputStream(ficName));
String userMail = prop.getProperty ( "mail" ) ;
String userSmtp = prop.getProperty ( "SMTP" ) ;
String userNom = prop.getProperty ( "nomU" ) ;
String nomEntite = prop.getProperty ( "nomE" );
String cheminCSV = prop.getProperty("cCSV");
String cheminPDF = prop.getProperty("cPDF");
String cheminLog = prop.getProperty("cLog");
//Lecture du fichier CSV
int cpt=0;
CsvReader cr = new CsvReader(cheminCSV);
while (cr.readRecord()){
cpt++;
}
int nbPerso = cpt-1;
/*Deuxième étape on envoie le mail*/
String emailfrom ="test@nomios.fr";
String emailto =userMail ;
String host= userSmtp;
// Récupére les propriétés du systéme
Properties propse = System.getProperties();
// Spécification du serveur mail
propse.put("mail.smtp.host", host);
// Récupère la session
Session session = Session.getDefaultInstance(propse, null);
// Définition du message
MimeMessage message = new MimeMessage(session);
//Spécification de l'expéditeur
try {
message.setFrom(new InternetAddress(emailfrom));
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Spécification du destinataire
try {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailto));
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Sujet du message
try {
message.setSubject("Rapport entite");
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BodyPart messageBodyPart = new MimeBodyPart();
//Texte du message
try {
messageBodyPart.setText("Bonjour Mr "+ userNom +", dans le service "+ nomEntite +", "+ nbPerso +" personnes sont passé par le proxy.\nVeuillez trouver ci-joint le rapport PDF.\n");
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Multipart multipart = new MimeMultipart();
try {
multipart.addBodyPart(messageBodyPart);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Partie de la pièce jointe
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(cheminPDF);
try {
messageBodyPart.setDataHandler(new DataHandler(source));
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
messageBodyPart.setFileName("rapport.pdf");
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Ajout de la partie pièce jointe
try {
multipart.addBodyPart(messageBodyPart);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
message.setContent(multipart);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Envoie le message
try {
Transport.send(message);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File fLog = new File(cheminLog+"archive");
if (!fLog.exists()){
fLog.mkdirs();
}
File newPDF = new File(cheminLog+"archive/rapport.pdf");
File newCSV = new File(cheminLog+"archive/test.csv");
File fPDF = new File(cheminPDF);
File fCSV = new File(cheminCSV);
deplacer(fPDF,newPDF);
deplacer(fCSV,newCSV);
}
} |
Partager