简体   繁体   English

如何从gmail android接收电子邮件

[英]How to receive email from gmail android

I am new to android programming.我是安卓编程的新手。

I got my app with Gmail account sends emails.我用 Gmail 帐户让我的应用程序发送电子邮件。 What I need now is how to receive new emails from G mail?我现在需要的是如何接收来自Gmail的新邮件? Or at least how to get a notification that there is a new mail in my inbox?或者至少如何获得收件箱中有新邮件的通知?

I don't want to use Gmail app from market or embedded email android app or so...I'm making my own app that manages Gmail accounts (like some kind of widget in my own app).我不想使用市场上的 Gmail 应用程序或嵌入式电子邮件 android 应用程序等等......我正在制作自己的应用程序来管理 Gmail 帐户(就像我自己的应用程序中的某种小部件)。

In order to implement this functionality ,first you need to establish the connection with the gmail server,then you need to check the inbox folder for new messages.为了实现这个功能,首先你需要建立与gmail服务器的连接,然后你需要检查收件箱文件夹中是否有新邮件。 If find then send the notification to the user using NotificationManager.如果找到,则使用 NotificationManager 将通知发送给用户。 please follow this links http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_%28no_Intents%29_in_Android and another link is请按照此链接http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_%28no_Intents%29_in_Android另一个链接是

Sending Email in Android using JavaMail API without using the default/built-in app 使用 JavaMail API 在 Android 中发送电子邮件而不使用默认/内置应用程序

Try this:试试这个:

Properties props = new Properties();
    //IMAPS protocol
    props.setProperty(“mail.store.protocol”, “imaps”);
    //Set host address
    props.setProperty(“mail.imaps.host”, imaps.gmail.com);
    //Set specified port
    props.setProperty(“mail.imaps.port”, “993″);
    //Using SSL
    props.setProperty(“mail.imaps.socketFactory.class”, “javax.net.ssl.SSLSocketFactory”);
    props.setProperty(“mail.imaps.socketFactory.fallback”, “false”);
    //Setting IMAP session
    Session imapSession = Session.getInstance(props);

Store store = imapSession.getStore(“imaps”);
//Connect to server by sending username and password.
//Example mailServer = imap.gmail.com, username = abc, password = abc
store.connect(mailServer, account.username, account.password);
//Get all mails in Inbox Forlder
inbox = store.getFolder(“Inbox”);
inbox.open(Folder.READ_ONLY);
//Return result to array of message
Message[] result = inbox.getMessages();

You need to first grant permission to "Notification accept " so your app can receive any notifications from device apps.您需要先授予“接受通知”权限,以便您的应用可以接收来自设备应用的任何通知。

You need to follow the steps below to enable the "Notification accept" permission:您需要按照以下步骤启用“通知接受”权限:

Setting => Apps => Special access => Notification accept设置 => 应用程序 => 特殊访问权限 => 通知接受

You need to give your application permission in AndroidManifest.xml:您需要在 AndroidManifest.xml 中授予您的应用程序权限:

   <service android:name="com.secondclone.UINotificationService"
    android:label="@string/app_name_notification"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
    <intent-filter>
    <action android:name="android.service.notification.NotificationListenerService" 
    />
    </intent-filter>
    </service>

Then, you write down the conditions to only receive notifications for new email notifications然后,您写下仅接收新电子邮件通知的条件

/* This is the class that helps you receive notifications when there are new emails */
public class UINotificationService extends NotificationListenerService {

    @Override
    public void onCreate()
    {
        super.onCreate(); 
    }

@Override

public void onNotificationPosted(StatusBarNotification sbn)
{

    // Get notification of new messages of the Gmail app com.google.android.gm
    if (sbn.getPackageName().equals("com.google.android.gm"))
    {

       /* What you need to handle when a new email is here */
       Bundle extras = sbn.getNotification().extras;
                if (!contentGmail.equals(extras.getCharSequence("android.bigText").toString()))
                {
                    contentGmail = Objects.requireNonNull(extras.getCharSequence("android.bigText")).toString();

                    // This is the recipient's Gmail name information.
                    String mreceiver = extras.getString("android.subText");

                    // This is the sender's name.
                    String mSender = extras.getString("android.title");

                    // This is the Email subject.
                    String mSubject = Objects.requireNonNull(extras.getCharSequence("android.text")).toString();

                    // This is the text of this new mail.
                    String mContent = Objects.requireNonNull(extras.getCharSequence("android.bigText")).toString();

                    //Notification.EXTRA_TEXT
                    time = sbn.getPostTime() / 1000;
                    Log.i("tsMail", "Sender = " + mSender + " Receiver= " + receiver + " Content Gmail= " + mContent );

                    }
                }

    }
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
    Log.i("Msg","Notification Removed");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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