Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中定制电子邮件客户端_Android_Jakarta Mail - Fatal编程技术网

在android中定制电子邮件客户端

在android中定制电子邮件客户端,android,jakarta-mail,Android,Jakarta Mail,我最近启动了我的第一个大学应用程序,作为我应用程序的一部分,我需要做一个自定义电子邮件客户端。这意味着有一个登录活动,用户可以输入他/她的gmail地址和密码来连接到该帐户。(此处安全风险不重要,此应用程序不会上载到商店,仅限“内部”使用) 连接后,这将带我们进入下一个活动,用户可以在其中选择发送新电子邮件、获取收件箱消息或获取已发送消息。(稍后还有3个活动)所有这些功能都有如何执行的指南(关于stackoverflow、TutorialPoint等,尽管其中许多不是专门针对android的,也

我最近启动了我的第一个大学应用程序,作为我应用程序的一部分,我需要做一个自定义电子邮件客户端。这意味着有一个登录活动,用户可以输入他/她的gmail地址和密码来连接到该帐户。(此处安全风险不重要,此应用程序不会上载到商店,仅限“内部”使用)

连接后,这将带我们进入下一个活动,用户可以在其中选择发送新电子邮件、获取收件箱消息或获取已发送消息。(稍后还有3个活动)所有这些功能都有如何执行的指南(关于stackoverflow、TutorialPoint等,尽管其中许多不是专门针对android的,也不是使用AsyncTask的),我知道我检查了很多,但所有这些都假设我们希望从提供地址和密码的活动中获得这些。我的第一个目标是只连接到用户的帐户,但我似乎无法实现。我做到了这一点:

电子邮件\u LoginActivity.java

public boolean isOnline() {
    ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnected();
}


public void connectToTheServer() {
    if (isOnline()) {
        if (usernameInput.getText().toString().isEmpty() || passwordInput.getText().toString().isEmpty())
            Toast.makeText(this, "Enter both username and password!", Toast.LENGTH_LONG).show();
        else {

            class LoginToEmailAccount extends AsyncTask<String, Integer, String> {
//I made this class here so I can get a dialogbox to the activity until the user waits.
                ProgressDialog progress;
                private Folder inbox;
                private Folder sent;
                public static final String INBOX_STRING = "INBOX";
                public static final String SENT_ITEMS_STRING = "MMS_Sent";

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    progress = ProgressDialog.show(Email_LoginActivity.this, null, "Login in progress, please wait.", true);
                }

                @Override
                protected String doInBackground(String... args) {

                    final String username = args[0];
                    final String password = args[1];

                    String host = "pop.gmail.com";

                    Properties properties = new Properties();
                    properties.put("mail.pop3s.host", host);
                    properties.put("mail.pop3s.port", "995");
                    properties.put("mail.pop3s.starttls.enable", "true");

                    // Setup authentication, get session
                    Session emailSession = Session.getInstance(properties,
                            new javax.mail.Authenticator() {
                                protected PasswordAuthentication getPasswordAuthentication() {
                                    return new PasswordAuthentication(
                                            username, password);
                                }
                            });

                    try {

                        // create the POP3 store object and connect with the pop server
                        Store store = emailSession.getStore("pop3s");
                        store.connect();

                        //should i create the folders here too?
                        inbox = store.getFolder(INBOX_STRING);
                        sent = store.getFolder(SENT_ITEMS_STRING);

                        //create
                        inbox.create(Folder.HOLDS_MESSAGES);
                        sent.create(Folder.HOLDS_MESSAGES);


                        // open folders
                        inbox.open(Folder.READ_WRITE);
                        sent.open(Folder.READ_WRITE);

                        if(store.isConnected()){
                            Log.i("mytag", "connected");
                        }
                        else{
                            Log.i("mytag", "not connected)");
                        }

                    } catch (NoSuchProviderException e) {
                        e.printStackTrace();
                    } catch (MessagingException e) {
                        e.printStackTrace();
                        Log.i("mytag", "sad times");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                    return "Login complete";

                }

                @Override
                protected void onPostExecute(String result) {
                    super.onPostExecute(result);
                    progress.dismiss();
                }


            }
            new LoginToEmailAccount().execute(usernameInput.getText().toString(), passwordInput.getText().toString());
        }

    } else {
        Toast.makeText(this, "Please make sure you have internet connection!", Toast.LENGTH_LONG).show();
    }
}

public void startEmailMainActivity(View view) {
    connectToTheServer(); //here we need a condition to check if the connection was successful before calling the next activity
    Intent intent = new Intent(this, Email_MainActivity.class); //probably gonna need some extra intent info too
    startActivity(intent);
}
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
public boolean isOnline(){
连接管理器cm=
(ConnectionManager)getSystemService(Context.CONNECTIVITY_服务);
NetworkInfo netInfo=cm.getActiveNetworkInfo();
返回netInfo!=null&&netInfo.isConnected();
}
public void connectToTheServer(){
if(isOnline()){
if(usernameInput.getText().toString().isEmpty()| | passwordInput.getText().toString().isEmpty())
Toast.makeText(这是“输入用户名和密码!”,Toast.LENGTH_LONG.show();
否则{
类LoginToEmailAccount扩展异步任务{
//我在这里创建了这个类,这样我就可以在用户等待之前获取活动的对话框。
进程对话进程;
私人文件夹收件箱;
私人文件夹发送;
公共静态最终字符串收件箱\u String=“收件箱”;
公共静态最终字符串已发送\u项目\u String=“MMS\u已发送”;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progress=ProgressDialog.show(Email_LoginActivity.this,null,“正在登录,请稍候”,true);
}
@凌驾
受保护的字符串doInBackground(字符串…args){
最终字符串username=args[0];
最终字符串密码=args[1];
String host=“pop.gmail.com”;
属性=新属性();
properties.put(“mail.pop3s.host”,host);
properties.put(“mail.pop3s.port”,“995”);
properties.put(“mail.pop3s.starttls.enable”、“true”);
//设置身份验证,获取会话
会话emailSession=Session.getInstance(属性,
新的javax.mail.Authenticator(){
受保护的密码身份验证getPasswordAuthentication(){
返回新密码验证(
用户名、密码);
}
});
试一试{
//创建POP3存储对象并连接pop服务器
Store Store=emailSession.getStore(“pop3s”);
store.connect();
//我也应该在这里创建文件夹吗?
收件箱=store.getFolder(收件箱\字符串);
sent=store.getFolder(已发送的\u项\u字符串);
//创造
收件箱。创建(文件夹。保存邮件);
已发送。创建(文件夹。保存邮件);
//打开文件夹
收件箱。打开(文件夹。读写);
发送。打开(文件夹。读写);
if(store.isConnected()){
Log.i(“mytag”、“已连接”);
}
否则{
Log.i(“mytag”,“未连接”);
}
}捕获(无此提供异常e){
e、 printStackTrace();
}捕获(消息异常e){
e、 printStackTrace();
Log.i(“我的标签”,“悲伤的时光”);
}捕获(例外e){
e、 printStackTrace();
}
返回“登录完成”;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
进步。解散();
}
}
新的LoginToEmailAccount().execute(usernameInput.getText().toString(),passwordInput.getText().toString());
}
}否则{
Toast.makeText(这是“请确保您已连接internet!”,Toast.LENGTH_LONG.show();
}
}
public void startemailmain活动(视图){
connectToTheServer();//在调用下一个活动之前,我们需要一个条件来检查连接是否成功
Intent Intent=new Intent(这个,Email_main activity.class);//可能还需要一些额外的Intent信息
星触觉(意向);
}
(usernameInput和passwordInput是用户向我们提供信息的2 EditText视图,在onCreate中声明)

AndroidManifest.xml包含以下内容:

public boolean isOnline() {
    ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnected();
}


public void connectToTheServer() {
    if (isOnline()) {
        if (usernameInput.getText().toString().isEmpty() || passwordInput.getText().toString().isEmpty())
            Toast.makeText(this, "Enter both username and password!", Toast.LENGTH_LONG).show();
        else {

            class LoginToEmailAccount extends AsyncTask<String, Integer, String> {
//I made this class here so I can get a dialogbox to the activity until the user waits.
                ProgressDialog progress;
                private Folder inbox;
                private Folder sent;
                public static final String INBOX_STRING = "INBOX";
                public static final String SENT_ITEMS_STRING = "MMS_Sent";

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    progress = ProgressDialog.show(Email_LoginActivity.this, null, "Login in progress, please wait.", true);
                }

                @Override
                protected String doInBackground(String... args) {

                    final String username = args[0];
                    final String password = args[1];

                    String host = "pop.gmail.com";

                    Properties properties = new Properties();
                    properties.put("mail.pop3s.host", host);
                    properties.put("mail.pop3s.port", "995");
                    properties.put("mail.pop3s.starttls.enable", "true");

                    // Setup authentication, get session
                    Session emailSession = Session.getInstance(properties,
                            new javax.mail.Authenticator() {
                                protected PasswordAuthentication getPasswordAuthentication() {
                                    return new PasswordAuthentication(
                                            username, password);
                                }
                            });

                    try {

                        // create the POP3 store object and connect with the pop server
                        Store store = emailSession.getStore("pop3s");
                        store.connect();

                        //should i create the folders here too?
                        inbox = store.getFolder(INBOX_STRING);
                        sent = store.getFolder(SENT_ITEMS_STRING);

                        //create
                        inbox.create(Folder.HOLDS_MESSAGES);
                        sent.create(Folder.HOLDS_MESSAGES);


                        // open folders
                        inbox.open(Folder.READ_WRITE);
                        sent.open(Folder.READ_WRITE);

                        if(store.isConnected()){
                            Log.i("mytag", "connected");
                        }
                        else{
                            Log.i("mytag", "not connected)");
                        }

                    } catch (NoSuchProviderException e) {
                        e.printStackTrace();
                    } catch (MessagingException e) {
                        e.printStackTrace();
                        Log.i("mytag", "sad times");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                    return "Login complete";

                }

                @Override
                protected void onPostExecute(String result) {
                    super.onPostExecute(result);
                    progress.dismiss();
                }


            }
            new LoginToEmailAccount().execute(usernameInput.getText().toString(), passwordInput.getText().toString());
        }

    } else {
        Toast.makeText(this, "Please make sure you have internet connection!", Toast.LENGTH_LONG).show();
    }
}

public void startEmailMainActivity(View view) {
    connectToTheServer(); //here we need a condition to check if the connection was successful before calling the next activity
    Intent intent = new Intent(this, Email_MainActivity.class); //probably gonna need some extra intent info too
    startActivity(intent);
}
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

我在调试模式下进行了检查,在sent.open(Folder.READ\u WRITE)之后得到了一个MessaginException;线路。我的主要想法是跟随,只是为了连接到帐户。顺便说一句,我在AsyncTask的帮助下进行了测试,它只是有一个相同的问题,我想在一个不同的活动中发送电子邮件,而不必再次提供用户名和密码

该怎么办