File support Base64 encoding utility

In order to send message with file attachment, you should encode your file to base64 encoding.

The following application demonstrates how to read a file located on your local disk, encode it to base64 and write the encoded file as a new file.

package telemessage.test;

import javax.mail.internet.MimeUtility;

public class Test {
    public static void main(String[] args) {
        //initialize input and output file name
        String strInputFileName = "enter file name (to encode) here";
        String strOutputFileName = "enter output file name here";
        rawFileToBase64(strInputFileName, strOutputFileName);

    public static void rawFileToBase64(String inFile, String outFile) {
        try {
            // initialize inputFile
            FileInputStream fin = new FileInputStream(inFile);
            BufferedInputStream bis = new BufferedInputStream(fin);
            // initialize outputFile
            FileOutputStream fos = new FileOutputStream(outFile);
            OutputStream os = MimeUtility.encode(fos, "base64");
            BufferedOutputStream bos = new BufferedOutputStream(os);
            byte data[] = new byte[1024];
            int len;
            // read the buffered stream
            while (bis.available() > 0) {
                len =;
                bos.write(data, 0, len);
        } catch (Exception e) {
            System.out.println("Unable to encode the contents of the request file."+ e);

To find more about Base64, click here.