Sending mail through JavaMail api using token

别等时光非礼了梦想. 提交于 2020-06-17 14:43:13

问题


I am looking to send email through app from user's account through JavaMail api using google authentication token (instead of using a direct password). Here how I get the token after user picks an account:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken("requestIdToken")
            .requestEmail()
            .build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
            //.enableAutoManage(this, MainActivity.this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();


private void signIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN2);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_SIGN_IN2) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            final GoogleSignInAccount account = result.getSignInAccount();
            userMail = account.getEmail();
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        String scope = "oauth2:"+Scopes.EMAIL+" "+ Scopes.PROFILE;
                        accessToken = GoogleAuthUtil.getToken(getApplicationContext(), account.getAccount(), scope, new Bundle());
                        Log.d("myapp", "accessToken:"+accessToken); // I get accessToken:ya29.Gl...

Now I use this for JavaMail api

 Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.debug", "true");
    props.put("mail.smtp.port", "587");
    //props.setProperty("mail.smtp.socketFactory.port", "587");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    //props.put("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.EnableSSL.enable","true");
    props.put("mail.smtp.auth.mechanisms", "XOAUTH2");

    mSession  = Session.getInstance(props);
    try {
        transport = mSession.getTransport("smtp");
        transport.connect("smtp.gmail.com", userMail, token);
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    }

        MimeMessage msg = new MimeMessage(mSession);
        msg.setFrom(new InternetAddress(userMail, "test"));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(mEmail));
        msg.setSubject(mSubject);
        msg.setContent(mMessage, "text/html");

        //transport.issueCommand("AUTH XOAUTH2 " + new String(BASE64EncoderStream.encode(String.format("user=%s\1auth=Bearer %s\1\1", Utils.EMAIL, Utils.PASSWORD).getBytes())), 235);
        transport.sendMessage(msg, msg.getAllRecipients());

But I get exception: DEBUG SMTP: AUTH XOAUTH2 failed javax.mail.AuthenticationFailedException: OAUTH2 asked for more

Question is: Is there any issue with my code for JavaMail ? But more than that what I feel the scope which I defined to get access token, looks that's not enough to send a mail, but I am not sure.

If yes, is there any other way to achieve the same like using firebase token for JavaMail.

来源:https://stackoverflow.com/questions/61329912/sending-mail-through-javamail-api-using-token

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