Java: Send A Letter Using A Template

Download
Download our Java samples from this page.
Find this sample in directory /test/com/postalmethods/client

Instructions

  1. Download the PostalMethods Java client. Simply download the jar file and place it in your classpath.
  2. In order to send a letter using a template, you must first upload a template file using the PostalMethods UploadFile method. For this sample, download the invoice template sample (.MHT) and upload it to your account using the UploadFile method (see code sample). Remember the name you use to identify your uploaded file as you will need it below.
  3. Below is a working Java program. There are three static String variables that you will need to provide values for - TEMPLATE_NAME, USERNAME, and PASSWORD.
  4. Compile and run the class. If the call succeeds, you will see your letter in the PostalMethods Control Panel. If the response is a negative number, check the Web Service Status Codes section.

That is all that is required to send a letter using a pre-uploaded template and providing merge data. Using the other methods available in the PostalMethods API is just as easy.

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.postalmethods.client.PostalMethodsClient;
import com.postalmethods.client.PostalMethodsClientFactory;
import com.postalmethods.client.api.SendLetterAndAddressResponse;
import com.postalmethods.client.model.Address;
import com.postalmethods.client.model.LetterContent;
import com.postalmethods.client.model.TemplateLetterContent;
 
public class SendTemplateLetter {
 
    private static final String TEMPLATE_NAME = "CHANGE ME";
    private static final String USERNAME = "CHANGE ME";
    private static final String PASSWORD = "CHANGE ME";
 
    public static void main( String[] args ) {
 
        PostalMethodsClient client = new PostalMethodsClientFactory().build(USERNAME, PASSWORD);
 
        Address address = new Address("George Washington", "", "The White House", "1600 Pennsylvania Ave", "", "Washington", "DC", "20500", "USA");
        String description = "Invoice letter using a template.";
 
        System.out.println("Making call to SendLetterAndAddress...");
        List<String> remoteFilenames = Collections.emptyList();  // none for this test
 
        Map<String,Object> mergeData = new HashMap<String,Object>();
        mergeData.put( "ReturnAddress", "ABC Hardware Inc.<br/>93S. Jackson Street<br/>Seattle  WA 98104-2818<br/>United States" );
        mergeData.put( "Date", "July 09, 2009" );
        mergeData.put( "InvoiceID", "8830023-RRX-3200985" );
        mergeData.put( "Payment", "Net 30" );
        mergeData.put( "MakeFor", "ABC Hardware" );
        mergeData.put( "ProductID1", "ED-2343" );
        mergeData.put( "Description1","Paslode Cordless Framing Nailer, 30 Degree Paper Collated" );
        mergeData.put( "Quantity1","2" );
        mergeData.put( "Price1", "379.21" );
        mergeData.put( "Amount1", "758.42" );
        mergeData.put( "ProductID2", "FR-9485" );
        mergeData.put( "Description2", "Porta-Nails 2 In. Flooring Nails Master Carton (1000 items)" );
        mergeData.put( "Quantity2", "10" );
        mergeData.put( "Price2", "11.20" );
        mergeData.put( "Amount2", "112.020" );
 
        LetterContent letterContent = new TemplateLetterContent( TEMPLATE_NAME, mergeData, remoteFilenames );
 
        SendLetterAndAddressResponse response = client.sendLetterAndAddress(description, letterContent, address);
        System.out.println("Service call completed:\n" + response.getResult().toString());
 
        int code = response.getResult().getCode();
        if(code > 0) {
            System.out.println("New letter id is: " + code);
        } else {
            System.out.println("Error, result code is: " + code);
        }
    }
}