In this post, I  will show you how to send a fax from Java. You have a number of different options, free and commercial. The current state of the market is in favor of commercial solutions for the following reasons: good documentation and excellent support.

I will cover the commercial solution from Interfax. (full disclaimer: I do not work for InterFax or have any vested interest in the company. I’m simply a satisfied user.)  Feel free to try the other options, open-source etc. I didn’t have much success with them, but your mileage may vary.

The Interfax server supports sending and receiving of faxes. This includes plain text and binary documents (PDF, MS Word, PowerPoint, etc…). Interfax exposes their fax server via a web service API. Once you sign up with the service, they will provide you with a user name a password.

Sending a Plain Text Fax
The code below shows you how to send a plain text fax. You should update the code with your InterFax username and password.

[code]
import net.interfax.outbound.SendCharFax;
import net.interfax.outbound.SendCharFaxResponse;
import net.interfax.outbound.InterFaxSoapStub;

public class FaxFromJava {

/******** Begin settings ********/
private static final String USERNAME = ""; // Enter your Interfax username here
private static final String PASSWORD = ""; // Enter your Interfax password here
private static final String FAX_NUMBER = ""; // Enter your designated fax number here in the format +[country code][area code][fax number], for example: +12125554874
private static final String TEXT_TO_FAX = "My text goes here";
private static final String FILE_TYPE = "TXT";
/******** End settings ********/

public static void main(String[] args) {
try {
InterFaxSoapStub theBinding =
(InterFaxSoapStub) 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();
}
}
}

[/code]

Sending a Binary Document (PDF, MS Word, PowerPoint, etc…)
The code below shows you how to send a binary document such as MS Word, PowerPoint, etc…

[code]
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import net.interfax.outbound.Sendfax;
import net.interfax.outbound.SendfaxResponse;
import net.interfax.outbound.InterFaxSoapStub;

public class SendOneFileAttachment {

/******** Begin settings ********/
private static final String USERNAME = ""; // Enter your Interfax username here
private static final String PASSWORD = ""; // Enter your Interfax password here
private static final String FAX_NUMBER = ""; // Enter the destination fax number here, e.g. +497116589658
private static final String FILE_NAME = "etc/sample.pdf"; // A file in your filesystem
/******** End settings ********/

public static void main(String[] args) {
try {
InterFaxSoapStub theBinding = (InterFaxSoapStub) net.interfax.outbound.InterFaxLocator().getInterFaxSoap();
theBinding.setTimeout(60000);
System.out.println("Sending Fax using Sendfax()");

// Read file data into a byte[].
byte[] fileData = transformToBytes(FILE_NAME);

System.out.println("Sending Fax using sendFax(). Document size: " + fileData.length);

Sendfax theParams = new Sendfax(USERNAME,
PASSWORD,
FAX_NUMBER,
fileData,
"PDF");
SendfaxResponse theResponse = theBinding.sendfax(theParams);
System.out.println("Sendfax() call returned with code: " + theResponse.getSendfaxResult());
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Helper function to read a local file into a byte[].
*/
private static byte[] transformToBytes(String aFilename) throws Exception {

if (aFilename == null) {
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( (theBytesRead = theIs.read(theBuffer)) != -1) {
if (theBytesRead < 1024) {
byte theSlice[] = new byte[theBytesRead];
System.arraycopy(theBuffer, 0, theSlice, 0, theBytesRead);
theRawData.write(theSlice);
} else {
theRawData.write(theBuffer);
}
}
} finally {
theIs.close();
theRawData.close();
}

return theRawData.toByteArray();
}
}
[/code]