You are here: Home > Download > Java code > Send advance message

Send Advance Message

    (get sources)

Message with attachment

In order to send message with file attachment you should encode your file to base64 encoding.
The following application demonstrate how to read a file located on your local disk, encode it to base64 and write the encoded file as a new file. For more information please consult our detailed documentation.
Note: the 3rd party package JavaMail is required.

  import java.io.*;
import javax.mail.internet.MimeUtility;


  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 boolean rawFileToBase64 (String inFile, String outFile) {
  try {
  // initialize inputFile
FileInputStream fin = new FileInputStream(inFile);
BufferedInputStream bis = new FBufferedInputStream(fin);

// initialize outputFile
FileOutputStream fos = new FileOutputStream(outFile);
OutputStream os = MimeUtility.encode(fos,"base64");
BufferedOutputStream bos = new BufferedOutputStream(os);

byte data[] = new new byte[1024];
int len;

// read the buffered stream
while(bis.available()>0)
{
    len = bis.read(data);
    bos.write(data,0,len);
}

bos.close();
bis.close();
 
} catch (Exception e) {
  System.out.println("Unable to encode the contents of the request file."+ e);
}

   
       }