My requirement:-
In My Android project I am using javamail to connect to gmail .. So,I want When I click the button,A progress dialog will start and in the background it will try to connect to gmail..If it the username and password is correct,means it connects to gmail..then will go to the next activity..otherwise it will dissmiss the progress dialog and show a toast --"something is wrong"
So I have this:--
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
pdialog = ProgressDialog.show(context, "", "Please wait...", true);
session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
RetreiveFeedTask task = new RetreiveFeedTask();
task.execute();
}
});
}
class RetreiveFeedTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try{
Transport transport = session.getTransport("smtp");
// Enter your correct gmail UserID and Password
transport.connect("smtp.gmail.com", username, password);
} catch(Exception e) {
//e.printStackTrace();
Toast.makeText(context, "something is wrong", Toast.LENGTH_SHORT).show();
}
return null;
}
@Override
protected void onProgressUpdate(Void... result) {
pdialog.show();
}
@Override
protected void onPostExecute(String result) {
pdialog.dismiss();
iget = new Intent(context,NextScreen.class);
startActivity(iget);
}
}
But the problem is:--if it connects to gmail id then it it goes to the next screen... If I put wrong username/password then it crashes..where Is the problem in my code??