import java.io.IOException; import java.io.StringReader; import java.io.UnsupportedEncodingException; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; import java.util.Vector; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; 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 javax.swing.text.BadLocationException; import javax.swing.text.Document; import javax.swing.text.html.HTMLEditorKit; import com.mysql.jdbc.Statement; public class SendMail_Dist extends Thread{ private static String CHARSET = "charset=ISO-8859-1"; private static int port_smtp = 25; private static Session _session; private String utilisateur; private String pass; private String serveurName; private int port; Vector> amail; private Base base; private String head; private String from; private String foot; private Vector headFoot; private String mailfrom; private String sujet; private String contenu; public SendMail_Dist(Vector> mail){ this.amail = mail; base = new Base(); String path = System.getProperty("user.dir" ); Param fc; try { fc = new Param(path+"/Synchro_SDK.ini"); this.pass = fc.getProperty("Mailer", "password"); this.utilisateur = fc.getProperty("Mailer", "login"); this.serveurName = fc.getProperty("Mailer", "SMTP_server"); this.mailfrom = fc.getProperty("Mailer", "mailfrom"); this.from = fc.getProperty("Mailer", "from"); this.port = Integer.parseInt(fc.getProperty("Mailer", "SMTP_port")); String strPort = String.valueOf(port); Properties props = new Properties(); props.put("mail.smtp.host", serveurName); props.put("mail.smtp.port", strPort); if (null == utilisateur || null == pass) { _session = Session.getDefaultInstance(props, null); } else { _session = Session.getDefaultInstance(props, new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(utilisateur, pass); } }); } sendMail(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public SendMail_Dist(String servName, String user, String password) { this.pass = password; this.utilisateur = user; this.serveurName = servName; this.port = port_smtp; String strPort = String.valueOf(port); Properties props = new Properties(); props.put("mail.smtp.host", serveurName); props.put("mail.smtp.port", strPort); if (null == utilisateur || null == pass) { _session = Session.getDefaultInstance(props, null); } else { _session = Session.getDefaultInstance(props, new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(utilisateur, pass); } }); } } public SendMail_Dist(String servName, int numPort, String user, String password) { this.pass = password; this.utilisateur = user; this.serveurName = servName; this.port = numPort; String path = System.getProperty("user.dir" ); Param fc; try { fc = new Param(path+"/Synchro_SDK.ini"); fc.putProperty("Mail", "SMTP_server", servName); fc.putProperty("Mail", "SMTP_port", ""+port); fc.putProperty("Mail", "login", user); fc.putProperty("Mail", "password", password); fc.saveFile(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } String strPort = String.valueOf(port); Properties props = new Properties(); props.put("mail.smtp.host", serveurName); props.put("mail.smtp.port", strPort); if (null == utilisateur || null == pass) { _session = Session.getDefaultInstance(props, null); } else { _session = Session.getDefaultInstance(props, new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(utilisateur, pass); } }); } } // Convertit un texte au format html en texte brut private static final String HtmlToText(final String s) { final HTMLEditorKit kit = new HTMLEditorKit(); final Document doc = kit.createDefaultDocument(); try { kit.read(new StringReader(s), doc, 0); return doc.getText(0, doc.getLength()).trim(); } catch (final IOException ioe) { return s; } catch (final BadLocationException ble) { return s; } } // Dfini les fichiers joindre private void setAttachmentPart(final String[] attachmentPaths, final MimeMultipart related, final MimeMultipart attachment, final String body, final boolean htmlText) throws MessagingException { for (int i = 0; i < attachmentPaths.length; ++i) { // Cration du fichier inclure final MimeBodyPart messageFilePart = new MimeBodyPart(); final DataSource source = new FileDataSource(attachmentPaths[i]); final String fileName = source.getName(); messageFilePart.setDataHandler(new DataHandler(source)); messageFilePart.setFileName(fileName); // Image inclure dans un texte au format HTML ou pice jointe if (htmlText && null != body && body.matches( ".*]*src=[\"|']?cid:([\"|']?" + fileName + "[\"|']?)[^>]*>.*")) { // " <-- pour viter une coloration syntaxique dsastreuse... messageFilePart.setDisposition("inline"); messageFilePart.setHeader("Content-ID", '<' + fileName + '>'); related.addBodyPart(messageFilePart); } else { messageFilePart.setDisposition("attachment"); attachment.addBodyPart(messageFilePart); } } } // Texte alternatif = texte + texte html private void setHtmlText(final MimeMultipart related, final MimeMultipart alternative, final String body) throws MessagingException { // Plain text final BodyPart plainText = new MimeBodyPart(); plainText.setContent(HtmlToText(body), "text/html; " + CHARSET); alternative.addBodyPart(plainText); // Html text ou Html text + images incluses final BodyPart htmlText = new MimeBodyPart(); htmlText.setContent(body, "text/html; " + CHARSET); if (0 != related.getCount()) { related.addBodyPart(htmlText, 0); final MimeBodyPart tmp = new MimeBodyPart(); tmp.setContent(related); alternative.addBodyPart(tmp); } else { alternative.addBodyPart(htmlText); } } // Dfinition du corps de l'e-mail private void setContent(final Message message, final MimeMultipart alternative, final MimeMultipart attachment, final String body) throws MessagingException { if (0 != attachment.getCount()) { // Contenu mixte: Pices jointes + texte if (0 != alternative.getCount() || null != body) { // Texte alternatif = texte + texte html final MimeBodyPart tmp = new MimeBodyPart(); tmp.setContent(alternative); attachment.addBodyPart(tmp, 0); } else { // Juste du texte final BodyPart plainText = new MimeBodyPart(); plainText.setContent(body, "text/html"); attachment.addBodyPart(plainText, 0); } message.setContent(attachment); } else { // Juste un message texte if (0 != alternative.getCount()) { // Texte alternatif = texte + texte html message.setContent(alternative); }else { // Texte message.setText(body); } } } public boolean sendMessage2(final ServiceMail_Dist mailMsg,String totalmsg) throws MessagingException{ final Message message = new MimeMessage(_session); System.out.println("Sujet "+mailMsg.getSubject()); message.setSubject(mailMsg.getSubject()); // Expditeur message.setFrom(mailMsg.getFrom()); System.out.println("From "+mailMsg.getFrom()); // Destinataires System.out.println("To "+mailMsg.getTo()[0]); message.setRecipients(Message.RecipientType.TO, mailMsg.getTo()); message.setRecipients(Message.RecipientType.CC, mailMsg.getCc()); message.setRecipients(Message.RecipientType.BCC, mailMsg.getBcc()); // Contenu + pices jointes final MimeMultipart related = new MimeMultipart("related"); final MimeMultipart attachment = new MimeMultipart("mixed"); final MimeMultipart alternative = new MimeMultipart("alternative"); final String[] attachments = mailMsg.getAttachmentURL(); final String body = (String) mailMsg.getContent(); final boolean html = mailMsg.isHtml(); // if (null != attachments) // setAttachmentPart(attachments, related, attachment, body, html); message.setContent(totalmsg, "text/html"); // setHtmlText(related,alternative,body); //setContent(message, alternative, attachment, body); System.out.println("Date d'envoi "+mailMsg.getSendDate()); // Date d'envoi System.out.println("Envoi... oups !"); Transport.send(message); // Rinitialise le message mailMsg.reset(); System.out.println("Message envoy"); return true; } public boolean sendMessage(final ServiceMail_Dist mailMsg) throws MessagingException{ final Message message = new MimeMessage(_session); System.out.println("Sujet "+mailMsg.getSubject()); message.setSubject(mailMsg.getSubject()); // Expditeur message.setFrom(mailMsg.getFrom()); System.out.println("From "+mailMsg.getFrom()); // Destinataires System.out.println("To "+mailMsg.getTo()[0]); message.setRecipients(Message.RecipientType.TO, mailMsg.getTo()); message.setRecipients(Message.RecipientType.CC, mailMsg.getCc()); message.setRecipients(Message.RecipientType.BCC, mailMsg.getBcc()); // Contenu + pices jointes final MimeMultipart related = new MimeMultipart("related"); final MimeMultipart attachment = new MimeMultipart("mixed"); final MimeMultipart alternative = new MimeMultipart("alternative"); final String[] attachments = mailMsg.getAttachmentURL(); final String body = (String) mailMsg.getContent(); final boolean html = mailMsg.isHtml(); if (null != attachments) setAttachmentPart(attachments, related, attachment, body, html); message.setContent(body, "text/html"); // setHtmlText(related,alternative,body); //setContent(message, alternative, attachment, body); System.out.println("Date d'envoi "+mailMsg.getSendDate()); // Date d'envoi System.out.println("Envoi... oups !"); Transport.send(message); // Rinitialise le message mailMsg.reset(); System.out.println("Message envoy"); return true; } public String replace( String s, String f, String r ) { if (s == null) return s; if (f == null) return s; if (r == null) r = ""; int index01 = s.indexOf( f ); while (index01 != -1) { s = s.substring(0,index01) + r + s.substring(index01+f.length()); index01 += r.length(); index01 = s.indexOf( f, index01 ); } return s; } private Vector readHeadFoot() { // TODO Auto-generated method stub try { headFoot = new Vector(); Statement st = (Statement) base.getStat(); ResultSet res1 = st.executeQuery("SELECT valpar from par2 where nompar = 'Dist_head'"); res1.next(); headFoot.add(replace(res1.getString("valpar"), "'", "\'")); ResultSet res2 = st.executeQuery("SELECT valpar from par2 where nompar = 'Dist_foot'"); res2.next(); headFoot.add(replace(res2.getString("valpar"), "'", "\'")); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return headFoot; } public boolean sendMail () { headFoot = readHeadFoot(); head = headFoot.get(0); foot = headFoot.get(1); int cp = 1; for(Vector vect : amail){ try { String toutlemail = ""; toutlemail += head; String maimag; cp++; System.out.println("Tour: "+cp+ " size "+amail.size()); maimag = (String) vect.get(0); System.out.println("Le mail : "+maimag); contenu = vect.get(1); //System.out.println("Le contenu : "+contenu); sujet = vect.get(2); System.out.println("Le sujet : "+sujet); toutlemail += contenu; toutlemail += foot; System.out.println("Le message en Html : "+toutlemail); final ServiceMail_Dist msg = new ServiceMail_Dist(); msg.setFrom(new InternetAddress(from)); msg.setTo(new InternetAddress(maimag)); msg.setSubject(sujet); msg.setContent(toutlemail, true); //sendMessage(msg); sendMessage2(msg,toutlemail); SendMail_Dist.sleep(1*1000); }/* catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("On poursuit ..."); } */catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("On poursuit ..."); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return true; } }