Java Sample Code

This example posts an 'EMS_POST_CAMPAIGN' request with an attachment using Apache Commons HttpClient (3.x).

import java.io.File;
import java.net.URLEncoder;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;

public class NewBlastSubmission {

    private void sendFileToEnterprise(String url, String filename) {
        try {
            HttpClient client = new HttpClient();
            PostMethod method = new PostMethod(url);
            File f = new File(filename);

            Part[] parts = {
                new StringPart("method", "EMS_POST_CAMPAIGN"),
                new StringPart("userid", "xxxxxxxxxx"),
                new StringPart("password", "******"),
                new StringPart("v", "1.1"),
                new StringPart("recipients", URLEncoder.encode("[email protected]", "UTF-8")),
                new StringPart("subject", URLEncoder.encode("Test e-statement", "UTF-8")),
                new StringPart("content", "Testing e-statement from API Gupshup"),
                new StringPart("content_type", "text/html"),
                new StringPart("name", "GUPSHUP TECH"),
                new FilePart("attachment1", f)
            };

            method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
            int statusCode = client.executeMethod(method);

            System.out.println(filename + "," + method.getResponseBodyAsString());
            System.out.println(statusCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String url = "https://enterprise.webaroo.com/GatewayAPI/rest";
        String filePath = "/home/Desktop/SimpleEMailTesthtml.zip";
        new NewBlastSubmission().sendFileToEnterprise(url, filePath);
    }
}

Did this page help you?