Java: Send A Letter with SendLetterAndAddressV2()

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.SendLetterAndAddressV2Response;
import com.postalmethods.client.model.Address;
 
public class SendLetterAndAddressV2Test {
 
    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.";
 
        System.out.println("Making call to SendLetterAndAddressV2...");
        SendLetterAndAddressV2Response response = client.sendLetterAndAddressV2(description, TestConstants.SAMPLE_LETTER_FILENAME, 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);
    }
}