send attachment file with transfert encoding to base64

Seventeen Stars

send attachment file with transfert encoding to base64

hi,
I had to create a job to send binary files (generated by as400) by mail.
Unfortunatly , i lost one octet because a wrong default encoding "quoted printable" for the part attachment mail(with tSendMail)
So i've created a simple routine with javamail library that force the encoding value to "base64"
here's the code :
// 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();
}
}
}

to run the routine i've just have to call the runJob method in a tJavaFlex component
the Start
// 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");

the Main
// 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);

I initialize variables with set environement
TfileCopy for ... copy the file in archive and delete the source
TsendMail to notifie the end's job to the someone

ps Smiley Tongueerhaps it's better to use context variable !
that 's it