Skip to content

Added proxy support #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/main/java/com/sendgrid/SendGrid.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package com.sendgrid;

import org.json.JSONObject;
import com.mashape.unirest.http.*;
import com.mashape.unirest.http.exceptions.*;
import com.sendgrid.smtpapi.SMTPAPI;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Map;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.sendgrid.smtpapi.SMTPAPI;
import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONObject;

import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.*;

public class SendGrid {
private HttpClient httpClient;
private String username;
private String password;
private String url;
private String port;
private String endpoint;

public SendGrid(String username, String password) {
this.username = username;
this.password = password;
this.url = "https://api.sendgrid.com";
this.endpoint = "/api/mail.send.json";
this.httpClient = HttpClientBuilder.create().build();
}

public SendGrid setUrl(String url) {
Expand All @@ -41,7 +41,15 @@ public SendGrid setEndpoint(String endpoint) {
return this;
}

public SendGrid setProxy(String host, int port) {
this.httpClient = HttpClientBuilder.create()
.setProxy(new HttpHost(host, port))
.build();
return this;
}

public SendGrid.Response send(Email email) throws SendGridException {
Unirest.setHttpClient(this.httpClient);
try {
HttpResponse<JsonNode> res = Unirest.post(this.url + this.endpoint)
.fields(email.toWebFormat()).field("api_user", this.username).field("api_key", this.password).asJson();
Expand Down