Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
// template routine Java
package routines.test;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
*
* @author raulier
*
*/
public class attachementBase64EncodingTransfert {
/**
*
*/
public attachementBase64EncodingTransfert(){}
/**
*
* @param session
* @param bodypart
* @param message
* @param smtpHost
* @throws MessagingException
*/
public static void sendMail(Session session, MimeBodyPart bodypart, MimeMessage message,String smtpHost) throws MessagingException
{
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(bodypart);
message.setContent(multipart);
message.saveChanges();
// forcer l'encodage
bodypart.setHeader("Content-Transfer-Encoding", "base64");
Transport transport = session.getTransport("smtp");
transport.connect(smtpHost,null,null);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
/**
*
* @param message
* @param adresseMail
* @return
* @throws MessagingException
*/
public static Message createMessage(Message message, String adresseMail) throws MessagingException
{
Address adresse = new InternetAddress(adresseMail);
message.addRecipient(javax.mail.Message.RecipientType.TO, adresse);
message.setFrom(adresse);
message.setSentDate(new Date());
message.setFileName("retour facture");
return message;
}
/**
*
* @param file
* @param name
* @return
* @throws MessagingException
* @throws IOException
*/
public static MimeBodyPart createAttachment64(String file,String name) throws MessagingException, IOException
{
File f = new File(file);
//System.out.println(f.length());
//System.out.println(f.compareTo(new File(file)));
//System.out.println(f.getTotalSpace()/1048576);
DataSource source = new FileDataSource(f);
BodyPart bodypart = new MimeBodyPart();
bodypart.setDataHandler(new DataHandler(source));
//System.out.println(bodypart.getSize());
bodypart.setFileName(name);
bodypart.addHeader("Content-type", "text/plain; charset=us-ascii");
bodypart.addHeader("Content-Transfert-Encoding","base64");
bodypart.addHeader("Content-Disposition", "attachment; filename=EOIA");
return (MimeBodyPart)bodypart;
}
/**
*
* @return
*/
public static Session getSession()
{
Properties props = new Properties();
return Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication("raulier", "lmlr1");
}});
}
/**
*
* @param smtpHost
* @param adresseMail
* @param attachment
* @param name
* @throws IOException
*/
public static void runJob(String smtpHost, String adresseMail, String attachment, String name) throws IOException
{
try
{
Session session = getSession();
MimeMessage message = new MimeMessage(session);
message = (MimeMessage) createMessage(message, adresseMail);
MimeBodyPart mimeBodyPart = createAttachment64(attachment,name);
sendMail(session, mimeBodyPart, message, smtpHost);
}
catch (MessagingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// start part of your Java code
String host = System.getProperty("HOST");
String email = System.getProperty("EMAIL");
String path = System.getProperty("PATH");
String name = System.getProperty("NAME");
// here is the main part of the component,
// a piece of code executed in the row
// loop
routines.test.attachementBase64EncodingTransfert rta = new routines.test.attachementBase64EncodingTransfert();
rta.runJob(host,email,path,name);