Java Fax Tutorial – Sending a Fax

This section demonstrates how to send a Java fax: creating a fax script, testing the fax script, viewing the fax status and going live with your script.

If you haven’t built your web service client or signed up for an InterFAX developer account, refer to the Building a Web Service Client section.

Creating Java Fax Script

  1. Create a Java file which will be your main faxing script, call it FaxFromJava.java.
  2. Copy the following code into your file:
    
    import net.interfax.outbound.SendCharFax;
    import net.interfax.outbound.SendCharFaxResponse;
    
    public class FaxFromJava {
    
        /******** Begin settings ********/
        static String USERNAME = "";   // Enter your Interfax username here
        static String PASSWORD = "";   // Enter your Interfax password here
        static String FAX_NUMBER = ""; // Enter your designated fax number here in the format +[country code][area code][fax number], for example: +12125554874
        static String TEXT_TO_FAX = "My text goes here";
        static String FILE_TYPE = "TXT";
        /******** End settings ********/
        
        public static void main(String[] anArgs) {
            try {
                net.interfax.outbound.InterFaxSoapStub theBinding = (net.interfax.outbound.InterFaxSoapStub)new net.interfax.outbound.InterFaxLocator().getInterFaxSoap();
                theBinding.setTimeout(60000);
                System.out.println("Sending Fax using sendCharFax()");
                SendCharFax theParams = new SendCharFax(USERNAME,
                                                        PASSWORD,
                                                        FAX_NUMBER,
                                                        TEXT_TO_FAX,
                                                        FILE_TYPE);
                SendCharFaxResponse theResponse = theBinding.sendCharFax(theParams);
    
                long theReturnCode = theResponse.getSendCharFaxResult();
                System.out.println("sendCharFax() call returned with code: " + theReturnCode);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    See the detailed SendCharFax code sample.

  3. Replace the variables in the Settings section. The FAX_NUMBER variable must be the same number that you designated when you registered as a developer.If you’re using an IDE like Eclipse, NetBeans or IntelliJ, you should now be able to run the FaxFromJava program. If you’re using the command line, you’ll have to compile and run the program manually as follows:Linux/Unix/OSX:
    javac -d classes -classpath lib/Interfax.jar:lib/axis.jar:lib/commons-discovery-0.2.jar:lib/commons-logging-1.0.4.jar:lib/jaxrpc.jar:lib/log4j-1.2.8.jar:lib/saaj.jar src/FaxFromJava.java 
    
    java -classpath classes:lib/log4j.properties:lib/Interfax.jar:lib/axis.jar:lib/commons-discovery-0.2.jar:lib/commons-logging-1.0.4.jar:lib/jaxrpc.jar:lib/log4j-1.2.8.jar:lib/saaj.jar:lib/wsdl4j-1.5.1.jar FaxFromJava

    Windows:

    javac -d classes -classpath lib\Interfax.jar;lib\axis.jar;lib\commons-discovery-0.2.jar;lib\commons-logging-1.0.4.jar;lib\jaxrpc.jar;lib/log4j-1.2.8.jar;lib\saaj.jar src\FaxFromJava.java
    
    java -classpath classes;lib\log4j.properties;lib\Interfax.jar;lib\axis.jar;lib\commons-discovery-0.2.jar;lib\commons-logging-1.0.4.jar;lib\jaxrpc.jar;lib\log4j-1.2.8.jar;lib\saaj.jar;lib\wsdl4j-1.5.1.jar FaxFromJava
    

    For your convenience, a Linux/Unix/OSX bash script and a Windows batch file to automate this build step have been included in the Java fax source download code samples and JAR.

Testing Your Fax Script

After a few seconds, you should receive a reply that includes text similar to the following:

sendCharFax() call returned with code: 123265193

The number returned is your fax’s transaction ID on the InterFAX system. This reply indicates that your Web service call has been successfully received on the InterFAX system and that InterFAX will now start processing your fax.

This does not indicate that your fax has been sent. Any result other than a positive number indicates an error (see Web Service Return Codes).

Viewing Java Fax Status

Log in to your InterFAX account at https://secure.interfax.net/Default.aspx. Once you’ve logged in, click Outbound Queues. Your fax should be visible as an entry in the table.

The status of your fax goes through several states, chronologically: Preprocessing, Ready, Sending, OK/Error. These are visible as icons in the outbound fax queue. Hover over the icon to view a toolbar tip of the status.

If all goes well, your fax should be received at your designated fax number within a minute or two. Congratulations!

Go Live

Once you have finished development and wish to be able to fax to any number, simply follow the instructions under question number 7 in the Interfax Developer FAQ.

Java Fax API – Learn More

InterFAX offers a variety of options for sending your faxes and retrieving status. To learn more about these capabilities, see the Outbound Web Service ReferenceJava fax Samples, and the Developer FAQ. If you have any questions, feel free to ask them here or to contact developer support.

Questions or comments on this tutorial? Post them in the developer forum.