如何在 Android 中编写无声自动电子邮件更改程序?

How to program an silent automatic email alter in Android?

抱歉,我是 Android 的新手,所以我的 Activity 中有一个按钮,我希望每次有人单击该按钮时,都会向我的电子邮箱发送一封电子邮件地址。我找到了几个打开智能手机电子邮件应用程序的电子邮件 API,但我想要的是用户单击按钮,将显示感谢屏幕,然后它 returns 到第一个屏幕。同时我会通过邮件收到通知。我怎么做?

提前致谢

您可以按照 here 中的说明使用 GMailSender class。

要先在后台发送邮件,您需要获取 Oauth 代码。

设置 Google 开发者控制台,说明如下:

Obtain OAuth 2.0 credentials

You need OAuth 2.0 credentials, including a client ID and client secret, to authenticate users and gain access to Google's APIs.

To find your project's client ID and client secret, do the following:

Go to the Google Developers Console. Select a project, or create a new one. In the sidebar on the left, expand APIs & auth. Next, click APIs. In the list of APIs, make sure all of the APIs you are using show a status of ON. In the sidebar on the left, select Credentials. If you haven't done so already, create your project's OAuth 2.0 credentials by clicking Create new Client ID, and providing the information needed to create the credentials. Look for the Client ID and Client secret in the table associated with each of your credentials. Note that not all types of credentials use both a client ID and client secret and won't be listed in the table if they are not used.

参考这里的教程获取Oauth令牌并在应用程序中使用:

http://blog.tomtasche.at/2013/05/google-oauth-on-android-using.html

在发送邮件的范围更新中:

private final String SCOPE = GmailScopes.GMAIL_COMPOSE + " " + GmailScopes.GMAIL_MODIFY + " " + GmailScopes.MAIL_GOOGLE_COM;

获取 Oauth 后使用以下代码发送邮件:

import android.util.Log;

import com.sun.mail.smtp.SMTPTransport;
import com.sun.mail.util.BASE64EncoderStream;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;

public class GMailSender {
    private Session session;


    public SMTPTransport connectToSmtp(String host, int port, String userEmail,
                                       String oauthToken, boolean debug) throws Exception {
        Log.v("ranjapp", "came to connecttosmtp");

        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "true");
        props.put("mail.smtp.sasl.enable", "false");
        //props.put("mail.imaps.sasl.mechanisms.oauth2.oauthToken", oauthToken);
        session = Session.getInstance(props);
        session.setDebug(debug);

        final URLName unusedUrlName = null;
        SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
        // If the password is non-null, SMTP tries to do AUTH LOGIN.
        final String emptyPassword = null;
        transport.connect(host, port, userEmail, emptyPassword);
        Log.v("ranjapp", "came before gen response");
        byte[] response = String.format("user=%sauth=Bearer %s", userEmail, oauthToken).getBytes();
        response = BASE64EncoderStream.encode(response);

        Log.v("ranjapp", "came to call issuecommand " + transport.isConnected());
        Log.v("ranjapp", new String(response));

        transport.issueCommand("AUTH XOAUTH2 " + new String(response), 235);

        Log.v("ranjapp", "came after issue command");
        return transport;
    }


    public synchronized void sendMail(String subject, String body, String user,
                                      String oauthToken, String recipients) {
        try {
            SMTPTransport smtpTransport = connectToSmtp("smtp.gmail.com", 587, user, oauthToken, true);

            MimeMessage message = new MimeMessage(session);
            DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
            message.setSender(new InternetAddress(user));
            message.setSubject(subject);
            message.setDataHandler(handler);

            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));

            smtpTransport.sendMessage(message, message.getAllRecipients());

        } catch (Exception e) {
            Log.v("ranjith", e.toString());
        }

    }
}

使用下面的代码发送邮件(使用上面class):

private class senmailAsync extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            GMailSender gMailSender = new GMailSender();
            gMailSender.sendMail("hi", "hi", authPreferences.getUser(), authPreferences.getToken(), whomtosendgmailid);
            Log.v("ranjapp", "sent mail " + authPreferences.getUser() + "  " + authPreferences.getToken());
            return null;
        }
    }

使用以下方式调用它:

new senmailAsync().execute();