send mail from android without using smtp and user interaction

烈酒焚心 提交于 2019-12-07 04:57:25

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)
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!