Skip to content

Instantly share code, notes, and snippets.

@hpsaturn
Last active April 2, 2020 17:10
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hpsaturn/5fd39a4e7d6ffb156197 to your computer and use it in GitHub Desktop.
Save hpsaturn/5fd39a4e7d6ffb156197 to your computer and use it in GitHub Desktop.
Send Mail basic implementation via MailGun API and Retrofit for Android
import android.util.Base64;
import com.google.gson.Gson;
import com.hpsaturn.robotsanta.Config;
import com.hpsaturn.robotsanta.models.MailGunResponse;
import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.converter.GsonConverter;
import retrofit.http.Field;
import retrofit.http.FormUrlEncoded;
import retrofit.http.Header;
import retrofit.http.Headers;
import retrofit.http.POST;
/**
* Created by Antonio Vanegas @hpsaturn on 11/15/15.
*/
public class MailGun {
private static final String TAG = MailGun.class.getSimpleName();
private static final boolean DEBUG = Config.DEBUG;
private static final String ENDPOINT = "https://api.mailgun.net/v3/yourdomain.com/";
public static final String ACCEPT_JSON_HEADER = "Accept: application/json";
public static final String BASIC = "Basic";
private SendMailApi sendMailApi;
public interface SendMailApi {
@Headers({ACCEPT_JSON_HEADER})
@FormUrlEncoded
@POST("/messages")
void authUser(
@Header("Authorization") String authorizationHeader,
@Field("from") String from,
@Field("to") String to,
@Field("subject") String subject,
@Field("text") String text,
Callback<MailGunResponse> cb
);
}
public void sendMail(String to, String subject, String msg, Callback<MailGunResponse> cb){
String from = "User Name Maybe <mailgun@yourdomain.com>";
String clientIdAndSecret = "api" + ":" + "key-AdFEFtggxxxYourApiKey";
String authorizationHeader = BASIC + " " + Base64.encodeToString(clientIdAndSecret.getBytes(), Base64.NO_WRAP);
sendMailApi.authUser(authorizationHeader,from, to, subject, msg, cb);
}
public MailGun() {
RestAdapter restAdapter = getAuthAdapter();
sendMailApi = restAdapter.create(SendMailApi.class);
}
private RestAdapter getAuthAdapter(){
RestAdapter.LogLevel logLevel = RestAdapter.LogLevel.NONE;
if(DEBUG)logLevel = RestAdapter.LogLevel.FULL;
return new RestAdapter.Builder()
.setEndpoint(ENDPOINT)
.setConverter(new GsonConverter(new Gson()))
.setLogLevel(logLevel)
.build();
}
}
@jyb27chang
Copy link

I'm struggling to find the gradle compile for 'com.hpsaturn.robotsanta'. Will someone please provide it?

@adityajoshi12
Copy link

can you provide this file com.hpsaturn.robotsanta.models.MailGunResponse

@milosmns
Copy link

It should be fairly simple as Mailgun's responses are JSON objects with id and message if 200 OK is sent.
So something like this would do:

class MailGunResponse {
    public String id;
    public String message;
}

I don't think you have to annotate it with @JsonEntity or something similar, but that's the way to go.

@dileepasisila
Copy link

dileepasisila commented Dec 17, 2018

Im using retrofit 2.0 version and got com.sun.jdi.InternalException: Unexpected JDWP Error: 103 error, Any idea ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment