简体   繁体   中英

send mail from android without using smtp and user interaction

I want to send email from my android application without any user interaction . How can i implement services like Mailgun API in android?

I found a piece of code in mailgun that works in java . For this implementation i got some libraries that doesnot works with android. Anybody have tried to use mailgun in android?

I think what you are asking is outbound sender email which is similar to Mailgun. One such service is Amazon SES which is already incorporated into AWS Mobile SDK . It even has a nice tutorial to implement it in Android. Both SES and Mailgun require a verified sender so technically you will be sending emails from your own domain and have nothing to do with user's email.

Send Mail basic implementation, via MailGun API and Retrofit for Android:

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();
    }

}

Complete Github gist: https://gist.github.com/hpsaturn/5fd39a4e7d6ffb156197

I would suggest to use javamail. It works fine for me and works on android. Here is the link to the google code project . Although you don't have as much possibilities as in mailgun, for just sending a mail javamail is enough.

let key = "dfasewr4353terf34t43fefdf34r"

let EmailBody  = "<html><body><table border='1'><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr>td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></table></body></html>"

let parameters = [
  "from":from@fromme.com,
  "to": to@tome.com,
  "subject": "my Email Subject",
  "html": EmailBody,
 "text" = "some text instead of html.Only one is aloowed either text or HTML"  
]


Alamofirerequest(.POST, "https://api.mailgun.net/v3/<MAILGUN-DOMAIN>/messages", parameters:parameters)
  .authenticate(user: "api", password: key)
  .response { (request, response, data, error) in
    println(request)
    println(response)
    println(error)
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM