Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Request a SpamAssassin score for a sample email

See more Email Object Examples

Submit a synthetic message to Postmark’s SpamCheck endpoint. The service receives the complete MIME you provide. Request a short result and check both the HTTP status and the API success field before reading the numeric score. SpamCheck API documentation.

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // Assumes Chilkat is unlocked. See Global Unlock Sample.

        Email email = new Email();
        email.setFrom("Workshop team <events@example.com>");
        try {
            email.addTo("Alex Rivera", "alex@example.net");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        email.setSubject("Workshop registration");
        email.setBody("Your workshop registration is confirmed.");
        JsonObject request = new JsonObject();
        request.updateString("email", email.getMime());
        request.updateString("options", "short");
        Http http = new Http();
        http.setRequireSslCertVerify(true);
        HttpResponse resp = new HttpResponse();
        try {
            http.httpJson("POST", "https://spamcheck.postmarkapp.com/filter", request, "application/json", resp);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if ((!success) || (resp.getStatusCode() != 200)) {
            System.out.println("SpamCheck request failed. HTTP " + resp.getStatusCode());
            return;
        }

        JsonObject result = new JsonObject();
        try {
            result.load(resp.getBodyStr());
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        if (!result.boolOf("success")) {
            System.out.println(result.stringOf("message"));
            return;
        }

        System.out.println("SpamAssassin score: " + result.stringOf("score"));
    }
}