-
-
Save pralhadstha/0031c910f784f215ea1e215d175bef25 to your computer and use it in GitHub Desktop.
Send Mail basic implementation via MailGun API and Retrofit for Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]>"; | |
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment