Java Fax - Send Faxes to Multiple Recipients Using SendfaxEx_2

Sending a Java fax to one or more recipients with one or more attached files is done using the SendfaxEx_2 method. This method has more flexible sending capabilities than Sendfax (see Side-by-side Comparison of Web Service Methods for more details).

This Java fax API snippet reads a number of files, joins them into one string, and submits the fax with multiple attachments to the InterFAX Web service. Finally, an indication is received, by which you can see whether the submission succeeded or failed: If you get the transaction ID (positive number) of the fax in the system, this means the transaction was successful. Otherwise, you will receive any other return value, indicating a failure (for more details, see Web Service Return Codes).

Find the Java fax code sample in this section as part of the ZIP file above (see the green 'Download' box), under the path interfaxJavaSamples > src > SendMultipleFilesToMultipleRecipients, or see below:

import java.util.ArrayList;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;

import net.interfax.outbound.SendfaxEx_2;
import net.interfax.outbound.SendfaxEx_2Response;

public class SendMultipleFilesToMultipleRecipients {

    /******** Begin settings ********/
    static String USERNAME = "";      // Enter your Interfax username here
    static String PASSWORD = "";     // Enter your Interfax password here
    static String FAX_NUMBERS = "+12077301111;+492077301222;+442077301333";  // semicolon-delimited list of destination fax numbers
    static String CONTACTS = "Rachel Burns;Albie Eins;Max Planck";  // optional semicolon-delimited list of recipient names; one per fax number
    static String[] FILES = new String[] { "etc/sample.pdf", "etc/sample.html" }; // List of files to fax
                                                        // These files need to be resident on the file system
    static int RETRIES = 3;
    static String CSID = "AA CSID";
    static String PAGE_HEADER = "To: {To} From: {From} Pages: {TotalPages}";
    static String SUBJECT = "Anything goes";
    static String REPLY_EMAIL = "";
    static String PAGE_SIZE = "A4";
    static String PAGE_ORIENTATION = "Portrait";
    static boolean HIGH_RESOLUTION = false;
    static boolean FINE_RENDERING = true;
    /******** End settings ********/
    public static void main(String[] anArgs) {
        try {
            net.interfax.outbound.InterFaxSoapStub theBinding = (net.interfax.outbound.InterFaxSoapStub)new net.interfax.outbound.InterFaxLocator().getInterFaxSoap();

            System.out.println("Sending Faxes using sendfaxEx_2()");

            // Read files into a single byte[].
            List fileLengths = new ArrayList(FILES.length);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            for( String filename : FILES ) {
                byte[] currFileBytes = transformToBytes( filename );
                os.write( currFileBytes );
                fileLengths.add( Integer.toString(currFileBytes.length) );
            byte[] fileData = os.toByteArray();

            // Build a String describing the length of each text fax (e.g. "15;16")
            String fileLengthsStr = join(fileLengths,";");

            // Create a Calendar instance.
            GregorianCalendar cal = new GregorianCalendar(2001,12,31,0,0); // December 31, 2001, 00:00

            // Invoke the sendfaxEx_2 method
            SendfaxEx_2 params = new SendfaxEx_2(
                    cal,              // postpone
                    "",               // jobId

            SendfaxEx_2Response response = theBinding.sendfaxEx_2(params);
            long theResult = response.getSendfaxEx_2Result();

            if (theResult > 0) {
                // Positive result indicates that the fax was sent successfully.
                // The return value is the TransactionID.
                System.out.println("Fax was sent successfully. Transaction ID: " + theResult);
            } else {
                // Fax send failure.  Please see the documentation for a list of
                // error codes and their meanings:
                System.out.println("Error sending fax!" + theResult);
        } catch (Exception e) {

     * Helper function to read a local file into a byte[].
    private static byte[] transformToBytes(String aFilename) throws Exception {
        if(null==aFilename) {
            throw new NullPointerException("aFilename is null");
        File theFile = new File(aFilename);
        if(!theFile.isFile()) {
            throw new IllegalArgumentException("Path doesn't represent a file: " + aFilename);
        if(!theFile.exists()) {
            throw new IllegalArgumentException("File not found: " + aFilename);
        InputStream theIs = new BufferedInputStream(new FileInputStream(theFile));
        ByteArrayOutputStream theRawData = new ByteArrayOutputStream();
        byte theBuffer[] = new byte[1024];
        int theBytesRead;
        try {
            while(( != -1) {
                if( theBytesRead < 1024 ) {
                    byte theSlice[] = new byte[theBytesRead];
                    System.arraycopy(theBuffer, 0, theSlice, 0, theBytesRead);
                } else {
        } finally {
        return theRawData.toByteArray();
     * Helper method to join strings using a delimeter.
    private static String join(Collection<? extends Object> c, String d) {
        StringBuffer buffer = new StringBuffer();
        Iterator<? extends Object> it = c.iterator();
        while (it.hasNext()) {
            if (it.hasNext()) {
        return buffer.toString();

Contact us today

Talk to a member of our team about the benefits InterFAX can bring to your organization's communications processes.

Contact us today