我最近开始了我的大学第一个应用程序,作为我的应用程序的一部分,我需要做一个自定义的电子邮件客户端。这意味着有一个登录活动,用户可以在其中输入他/她的Gmail地址和密码以连接到该帐户。 (安全风险在这里并不重要,这个应用程序不会上传到商店,只有“内部”使用)。在android中制作自定义电子邮件客户端
连接完成后,我们将进入下一个活动,用户可以选择发送新电子邮件,获取收件箱消息或获取发送的消息(后续3个活动)。所有这些功能都提供有关如何执行这些操作的指南(在stackoverflow,tutorialspoint等,虽然其中很多人不是专门针对android或不使用AsyncTask),我知道我检查了很多,但所有这些都假设我们需要这些来自活动的地址和密码。我的第一个目标是只连接到用户的帐户,而我似乎无法实现它。我做了这个至今:
Email_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);
}
(usernameInput和passwordInput是2的EditText图,其中用户发出信息给我们,在申报的onCreate)
AndroidManifest.xml有这些:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
我检查过在调试模式下,我得到一个MessagingException后sent.open(Folder.READ_WRITE);线。我的主要想法是按照this guide只是连接到该帐户。顺便说一句,我测试了我的this guide在AsyncTask的帮助下工作,它只是有同样的问题,我想要发送电子邮件部分在不同的活动,我不必再次提供用户名和密码。
我应该做些什么不同?对不起,如果问题太广泛,任何帮助,不胜感激。
'他/她的Gmail地址'。你的意思是'电子邮件地址'? – greenapps
@greenapps是的。 –