Java: Send A Letter with SendLetterAndAddress()

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

This sample demonstrates how to send a letter through PostalMethods using the Address Outside method.

package com.postalmethods.client;
 
import static org.junit.Assert.assertTrue;
 
import org.junit.Before;
import org.junit.Test;
 
import com.postalmethods.client.api.SendLetterAndAddressResponse;
import com.postalmethods.client.model.Address;
import com.postalmethods.client.model.LetterContent;
import com.postalmethods.client.model.LocalLetterContent;
 
/**
 * Integration test that calls the SendLetterAndAddress web method.
 * <p>
 * For details about PostalMethods.SendLetterAndAddress see 
 * <a href="http://www.postalmethods.com/method/2009-02-26/SendLetterAndAddress">the PostalMethods.SendLetterAndAddress reference</a>
 */
public class SendLetterAndAddressWithLocalLetterContent {
 
    private PostalMethodsClient client;
 
    @Before
    public void setUp() {
        client = new PostalMethodsClientFactory().build();
    }
 
    @Test
    public void testSendLetterAndAddress() {
 
        Address address = new Address("George Washington", "", "The White House", "1600 Pennsylvania Ave", "", "Washington", "DC", "20500", "USA");
        String description = "Test letter with address.";
 
        LetterContent letterContent = new LocalLetterContent( TestConstants.SAMPLE_LETTER_LOCAL_FILENAME );
 
        System.out.println("Making call to SendLetterAndAddress...");
        SendLetterAndAddressResponse response = client.sendLetterAndAddress(description, letterContent, address);
        System.out.println("Service call completed:\n" + response.getResult().toString());
 
        int code = response.getResult().getCode();
        assertTrue(code > 0);
 
        System.out.println("New letter id is: " + code);
    }
}