<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic send attachment file with transfert encoding to base64 in Talend Studio</title>
    <link>https://community.qlik.com/t5/Talend-Studio/send-attachment-file-with-transfert-encoding-to-base64/m-p/2280310#M55209</link>
    <description>hi, 
&lt;BR /&gt;I had to create a job to send binary files (generated by as400) by mail. 
&lt;BR /&gt;Unfortunatly , i lost one octet because a wrong default encoding "quoted printable" for the part attachment mail(with tSendMail) 
&lt;BR /&gt;So i've created a simple routine with javamail library that force the encoding value to "base64" 
&lt;BR /&gt;here's the code : 
&lt;BR /&gt; 
&lt;PRE&gt;// template routine Java&lt;BR /&gt;package routines.test;&lt;BR /&gt;import java.io.File;&lt;BR /&gt;import java.io.IOException;&lt;BR /&gt;import java.util.Date;&lt;BR /&gt;import java.util.Properties;&lt;BR /&gt;import javax.activation.DataHandler;&lt;BR /&gt;import javax.activation.DataSource;&lt;BR /&gt;import javax.activation.FileDataSource;&lt;BR /&gt;import javax.mail.*;&lt;BR /&gt;import javax.mail.internet.InternetAddress;&lt;BR /&gt;import javax.mail.internet.MimeBodyPart;&lt;BR /&gt;import javax.mail.internet.MimeMessage;&lt;BR /&gt;import javax.mail.internet.MimeMultipart;&lt;BR /&gt;/**&lt;BR /&gt; * &lt;BR /&gt; * @author raulier&lt;BR /&gt; *&lt;BR /&gt; */&lt;BR /&gt;public class attachementBase64EncodingTransfert {&lt;BR /&gt;	/**&lt;BR /&gt;	 * &lt;BR /&gt;	 */&lt;BR /&gt;	public attachementBase64EncodingTransfert(){}&lt;BR /&gt;	&lt;BR /&gt;	&lt;BR /&gt;	/**&lt;BR /&gt;	 * &lt;BR /&gt;	 * @param session&lt;BR /&gt;	 * @param bodypart&lt;BR /&gt;	 * @param message&lt;BR /&gt;	 * @param smtpHost&lt;BR /&gt;	 * @throws MessagingException&lt;BR /&gt;	 */&lt;BR /&gt;	public static void sendMail(Session session, MimeBodyPart bodypart, MimeMessage message,String smtpHost) throws MessagingException&lt;BR /&gt;	{&lt;BR /&gt;		Multipart multipart = new MimeMultipart();&lt;BR /&gt;		multipart.addBodyPart(bodypart);&lt;BR /&gt;		message.setContent(multipart);&lt;BR /&gt;		message.saveChanges();&lt;BR /&gt;		// forcer l'encodage &lt;BR /&gt;		bodypart.setHeader("Content-Transfer-Encoding", "base64");&lt;BR /&gt;		&lt;BR /&gt;		Transport transport = session.getTransport("smtp");&lt;BR /&gt;		transport.connect(smtpHost,null,null);&lt;BR /&gt;		transport.sendMessage(message, message.getAllRecipients());&lt;BR /&gt;		transport.close();&lt;BR /&gt;	}&lt;BR /&gt;	&lt;BR /&gt;	&lt;BR /&gt;	/**&lt;BR /&gt;	 * &lt;BR /&gt;	 * @param message&lt;BR /&gt;	 * @param adresseMail&lt;BR /&gt;	 * @return&lt;BR /&gt;	 * @throws MessagingException&lt;BR /&gt;	 */&lt;BR /&gt;	public static Message createMessage(Message message, String adresseMail) throws MessagingException&lt;BR /&gt;	{&lt;BR /&gt;		Address adresse = new InternetAddress(adresseMail);&lt;BR /&gt;		message.addRecipient(javax.mail.Message.RecipientType.TO, adresse);&lt;BR /&gt;		message.setFrom(adresse);&lt;BR /&gt;		message.setSentDate(new Date());&lt;BR /&gt;		message.setFileName("retour facture");&lt;BR /&gt;		&lt;BR /&gt;		return message;&lt;BR /&gt;	}&lt;BR /&gt;	&lt;BR /&gt;	&lt;BR /&gt;	/**&lt;BR /&gt;	 * &lt;BR /&gt;	 * @param file&lt;BR /&gt;	 * @param name&lt;BR /&gt;	 * @return&lt;BR /&gt;	 * @throws MessagingException&lt;BR /&gt;	 * @throws IOException&lt;BR /&gt;	 */&lt;BR /&gt;	public static MimeBodyPart createAttachment64(String file,String name) throws MessagingException, IOException&lt;BR /&gt;	{&lt;BR /&gt;		File f = new File(file);&lt;BR /&gt;		//System.out.println(f.length());&lt;BR /&gt;		//System.out.println(f.compareTo(new File(file)));&lt;BR /&gt;		//System.out.println(f.getTotalSpace()/1048576);&lt;BR /&gt;		DataSource source = new FileDataSource(f);&lt;BR /&gt;		&lt;BR /&gt;		BodyPart bodypart = new MimeBodyPart();&lt;BR /&gt;		&lt;BR /&gt;		bodypart.setDataHandler(new DataHandler(source));&lt;BR /&gt;		//System.out.println(bodypart.getSize());&lt;BR /&gt;		bodypart.setFileName(name);&lt;BR /&gt;		bodypart.addHeader("Content-type", "text/plain; charset=us-ascii");&lt;BR /&gt;		bodypart.addHeader("Content-Transfert-Encoding","base64");&lt;BR /&gt;		bodypart.addHeader("Content-Disposition", "attachment; filename=EOIA");&lt;BR /&gt;		&lt;BR /&gt;		&lt;BR /&gt;		&lt;BR /&gt;		&lt;BR /&gt;		return (MimeBodyPart)bodypart;&lt;BR /&gt;	}&lt;BR /&gt;	&lt;BR /&gt;	&lt;BR /&gt;	/**&lt;BR /&gt;	 * &lt;BR /&gt;	 * @return&lt;BR /&gt;	 */&lt;BR /&gt;	public static Session getSession()&lt;BR /&gt;	{&lt;BR /&gt;		Properties props = new Properties();&lt;BR /&gt;		&lt;BR /&gt;		return Session.getDefaultInstance(props, new javax.mail.Authenticator() {&lt;BR /&gt;			protected javax.mail.PasswordAuthentication getPasswordAuthentication() {&lt;BR /&gt;				return new javax.mail.PasswordAuthentication("raulier", "lmlr1");&lt;BR /&gt;			}});&lt;BR /&gt;	}&lt;BR /&gt;	&lt;BR /&gt;	&lt;BR /&gt;	&lt;BR /&gt;	/**&lt;BR /&gt;	 * &lt;BR /&gt;	 * @param smtpHost&lt;BR /&gt;	 * @param adresseMail&lt;BR /&gt;	 * @param attachment&lt;BR /&gt;	 * @param name&lt;BR /&gt;	 * @throws IOException&lt;BR /&gt;	 */&lt;BR /&gt;	public static void runJob(String smtpHost, String adresseMail, String attachment, String name) throws IOException&lt;BR /&gt;	{&lt;BR /&gt;		try &lt;BR /&gt;		{&lt;BR /&gt;			Session session = getSession();&lt;BR /&gt;			MimeMessage message =  new MimeMessage(session);&lt;BR /&gt;			message = (MimeMessage) createMessage(message, adresseMail);&lt;BR /&gt;			MimeBodyPart mimeBodyPart = createAttachment64(attachment,name);&lt;BR /&gt;			sendMail(session, mimeBodyPart, message, smtpHost);&lt;BR /&gt;			&lt;BR /&gt;		} &lt;BR /&gt;		catch (MessagingException e) &lt;BR /&gt;		{&lt;BR /&gt;			// TODO Auto-generated catch block&lt;BR /&gt;			e.printStackTrace();&lt;BR /&gt;		}&lt;BR /&gt;	}&lt;BR /&gt;}&lt;/PRE&gt; 
&lt;BR /&gt;to run the routine i've just have to call the runJob method in a tJavaFlex component 
&lt;BR /&gt;the Start 
&lt;BR /&gt; 
&lt;PRE&gt;// start part of your Java code&lt;BR /&gt;String host =  System.getProperty("HOST");&lt;BR /&gt;String email = System.getProperty("EMAIL");&lt;BR /&gt;String path = System.getProperty("PATH");&lt;BR /&gt;String name = System.getProperty("NAME");&lt;/PRE&gt; 
&lt;BR /&gt;the Main 
&lt;BR /&gt; 
&lt;PRE&gt;// here is the main part of the component,&lt;BR /&gt;// a piece of code executed in the row&lt;BR /&gt;// loop&lt;BR /&gt;routines.test.attachementBase64EncodingTransfert rta = new routines.test.attachementBase64EncodingTransfert();&lt;BR /&gt;rta.runJob(host,email,path,name);&lt;/PRE&gt; 
&lt;BR /&gt;I initialize variables with set environement 
&lt;BR /&gt;TfileCopy for ... copy the file in archive and delete the source 
&lt;BR /&gt;TsendMail to notifie the end's job to the someone 
&lt;BR /&gt; 
&lt;BR /&gt;ps 
&lt;span class="lia-inline-image-display-wrapper" image-alt="0683p000009MAB6.png"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/158321i00588DF41617C922/image-size/large?v=v2&amp;amp;px=999" role="button" title="0683p000009MAB6.png" alt="0683p000009MAB6.png" /&gt;&lt;/span&gt;erhaps it's better to use context variable ! 
&lt;BR /&gt;that 's it</description>
    <pubDate>Sat, 16 Nov 2024 14:11:34 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2024-11-16T14:11:34Z</dc:date>
    <item>
      <title>send attachment file with transfert encoding to base64</title>
      <link>https://community.qlik.com/t5/Talend-Studio/send-attachment-file-with-transfert-encoding-to-base64/m-p/2280310#M55209</link>
      <description>hi, 
&lt;BR /&gt;I had to create a job to send binary files (generated by as400) by mail. 
&lt;BR /&gt;Unfortunatly , i lost one octet because a wrong default encoding "quoted printable" for the part attachment mail(with tSendMail) 
&lt;BR /&gt;So i've created a simple routine with javamail library that force the encoding value to "base64" 
&lt;BR /&gt;here's the code : 
&lt;BR /&gt; 
&lt;PRE&gt;// template routine Java&lt;BR /&gt;package routines.test;&lt;BR /&gt;import java.io.File;&lt;BR /&gt;import java.io.IOException;&lt;BR /&gt;import java.util.Date;&lt;BR /&gt;import java.util.Properties;&lt;BR /&gt;import javax.activation.DataHandler;&lt;BR /&gt;import javax.activation.DataSource;&lt;BR /&gt;import javax.activation.FileDataSource;&lt;BR /&gt;import javax.mail.*;&lt;BR /&gt;import javax.mail.internet.InternetAddress;&lt;BR /&gt;import javax.mail.internet.MimeBodyPart;&lt;BR /&gt;import javax.mail.internet.MimeMessage;&lt;BR /&gt;import javax.mail.internet.MimeMultipart;&lt;BR /&gt;/**&lt;BR /&gt; * &lt;BR /&gt; * @author raulier&lt;BR /&gt; *&lt;BR /&gt; */&lt;BR /&gt;public class attachementBase64EncodingTransfert {&lt;BR /&gt;	/**&lt;BR /&gt;	 * &lt;BR /&gt;	 */&lt;BR /&gt;	public attachementBase64EncodingTransfert(){}&lt;BR /&gt;	&lt;BR /&gt;	&lt;BR /&gt;	/**&lt;BR /&gt;	 * &lt;BR /&gt;	 * @param session&lt;BR /&gt;	 * @param bodypart&lt;BR /&gt;	 * @param message&lt;BR /&gt;	 * @param smtpHost&lt;BR /&gt;	 * @throws MessagingException&lt;BR /&gt;	 */&lt;BR /&gt;	public static void sendMail(Session session, MimeBodyPart bodypart, MimeMessage message,String smtpHost) throws MessagingException&lt;BR /&gt;	{&lt;BR /&gt;		Multipart multipart = new MimeMultipart();&lt;BR /&gt;		multipart.addBodyPart(bodypart);&lt;BR /&gt;		message.setContent(multipart);&lt;BR /&gt;		message.saveChanges();&lt;BR /&gt;		// forcer l'encodage &lt;BR /&gt;		bodypart.setHeader("Content-Transfer-Encoding", "base64");&lt;BR /&gt;		&lt;BR /&gt;		Transport transport = session.getTransport("smtp");&lt;BR /&gt;		transport.connect(smtpHost,null,null);&lt;BR /&gt;		transport.sendMessage(message, message.getAllRecipients());&lt;BR /&gt;		transport.close();&lt;BR /&gt;	}&lt;BR /&gt;	&lt;BR /&gt;	&lt;BR /&gt;	/**&lt;BR /&gt;	 * &lt;BR /&gt;	 * @param message&lt;BR /&gt;	 * @param adresseMail&lt;BR /&gt;	 * @return&lt;BR /&gt;	 * @throws MessagingException&lt;BR /&gt;	 */&lt;BR /&gt;	public static Message createMessage(Message message, String adresseMail) throws MessagingException&lt;BR /&gt;	{&lt;BR /&gt;		Address adresse = new InternetAddress(adresseMail);&lt;BR /&gt;		message.addRecipient(javax.mail.Message.RecipientType.TO, adresse);&lt;BR /&gt;		message.setFrom(adresse);&lt;BR /&gt;		message.setSentDate(new Date());&lt;BR /&gt;		message.setFileName("retour facture");&lt;BR /&gt;		&lt;BR /&gt;		return message;&lt;BR /&gt;	}&lt;BR /&gt;	&lt;BR /&gt;	&lt;BR /&gt;	/**&lt;BR /&gt;	 * &lt;BR /&gt;	 * @param file&lt;BR /&gt;	 * @param name&lt;BR /&gt;	 * @return&lt;BR /&gt;	 * @throws MessagingException&lt;BR /&gt;	 * @throws IOException&lt;BR /&gt;	 */&lt;BR /&gt;	public static MimeBodyPart createAttachment64(String file,String name) throws MessagingException, IOException&lt;BR /&gt;	{&lt;BR /&gt;		File f = new File(file);&lt;BR /&gt;		//System.out.println(f.length());&lt;BR /&gt;		//System.out.println(f.compareTo(new File(file)));&lt;BR /&gt;		//System.out.println(f.getTotalSpace()/1048576);&lt;BR /&gt;		DataSource source = new FileDataSource(f);&lt;BR /&gt;		&lt;BR /&gt;		BodyPart bodypart = new MimeBodyPart();&lt;BR /&gt;		&lt;BR /&gt;		bodypart.setDataHandler(new DataHandler(source));&lt;BR /&gt;		//System.out.println(bodypart.getSize());&lt;BR /&gt;		bodypart.setFileName(name);&lt;BR /&gt;		bodypart.addHeader("Content-type", "text/plain; charset=us-ascii");&lt;BR /&gt;		bodypart.addHeader("Content-Transfert-Encoding","base64");&lt;BR /&gt;		bodypart.addHeader("Content-Disposition", "attachment; filename=EOIA");&lt;BR /&gt;		&lt;BR /&gt;		&lt;BR /&gt;		&lt;BR /&gt;		&lt;BR /&gt;		return (MimeBodyPart)bodypart;&lt;BR /&gt;	}&lt;BR /&gt;	&lt;BR /&gt;	&lt;BR /&gt;	/**&lt;BR /&gt;	 * &lt;BR /&gt;	 * @return&lt;BR /&gt;	 */&lt;BR /&gt;	public static Session getSession()&lt;BR /&gt;	{&lt;BR /&gt;		Properties props = new Properties();&lt;BR /&gt;		&lt;BR /&gt;		return Session.getDefaultInstance(props, new javax.mail.Authenticator() {&lt;BR /&gt;			protected javax.mail.PasswordAuthentication getPasswordAuthentication() {&lt;BR /&gt;				return new javax.mail.PasswordAuthentication("raulier", "lmlr1");&lt;BR /&gt;			}});&lt;BR /&gt;	}&lt;BR /&gt;	&lt;BR /&gt;	&lt;BR /&gt;	&lt;BR /&gt;	/**&lt;BR /&gt;	 * &lt;BR /&gt;	 * @param smtpHost&lt;BR /&gt;	 * @param adresseMail&lt;BR /&gt;	 * @param attachment&lt;BR /&gt;	 * @param name&lt;BR /&gt;	 * @throws IOException&lt;BR /&gt;	 */&lt;BR /&gt;	public static void runJob(String smtpHost, String adresseMail, String attachment, String name) throws IOException&lt;BR /&gt;	{&lt;BR /&gt;		try &lt;BR /&gt;		{&lt;BR /&gt;			Session session = getSession();&lt;BR /&gt;			MimeMessage message =  new MimeMessage(session);&lt;BR /&gt;			message = (MimeMessage) createMessage(message, adresseMail);&lt;BR /&gt;			MimeBodyPart mimeBodyPart = createAttachment64(attachment,name);&lt;BR /&gt;			sendMail(session, mimeBodyPart, message, smtpHost);&lt;BR /&gt;			&lt;BR /&gt;		} &lt;BR /&gt;		catch (MessagingException e) &lt;BR /&gt;		{&lt;BR /&gt;			// TODO Auto-generated catch block&lt;BR /&gt;			e.printStackTrace();&lt;BR /&gt;		}&lt;BR /&gt;	}&lt;BR /&gt;}&lt;/PRE&gt; 
&lt;BR /&gt;to run the routine i've just have to call the runJob method in a tJavaFlex component 
&lt;BR /&gt;the Start 
&lt;BR /&gt; 
&lt;PRE&gt;// start part of your Java code&lt;BR /&gt;String host =  System.getProperty("HOST");&lt;BR /&gt;String email = System.getProperty("EMAIL");&lt;BR /&gt;String path = System.getProperty("PATH");&lt;BR /&gt;String name = System.getProperty("NAME");&lt;/PRE&gt; 
&lt;BR /&gt;the Main 
&lt;BR /&gt; 
&lt;PRE&gt;// here is the main part of the component,&lt;BR /&gt;// a piece of code executed in the row&lt;BR /&gt;// loop&lt;BR /&gt;routines.test.attachementBase64EncodingTransfert rta = new routines.test.attachementBase64EncodingTransfert();&lt;BR /&gt;rta.runJob(host,email,path,name);&lt;/PRE&gt; 
&lt;BR /&gt;I initialize variables with set environement 
&lt;BR /&gt;TfileCopy for ... copy the file in archive and delete the source 
&lt;BR /&gt;TsendMail to notifie the end's job to the someone 
&lt;BR /&gt; 
&lt;BR /&gt;ps 
&lt;span class="lia-inline-image-display-wrapper" image-alt="0683p000009MAB6.png"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/158321i00588DF41617C922/image-size/large?v=v2&amp;amp;px=999" role="button" title="0683p000009MAB6.png" alt="0683p000009MAB6.png" /&gt;&lt;/span&gt;erhaps it's better to use context variable ! 
&lt;BR /&gt;that 's it</description>
      <pubDate>Sat, 16 Nov 2024 14:11:34 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/send-attachment-file-with-transfert-encoding-to-base64/m-p/2280310#M55209</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-11-16T14:11:34Z</dc:date>
    </item>
  </channel>
</rss>

