I want to develop an application from which user can send an email (without using Intent) from anywhere and on the other hand it can be received by me on my Gmail id.
Here's my code, when I click on button..
switch (v.getId()) {
case R.id.bBack:
finish();
break;
case R.id.bSend:
// send an email
sendEmail();
break;
}
And the other class I used is..
private void sendEmail() {
mName = name.getText().toString();
mEmail = "something@gmail.com";
mPassword = "password";
mQuery = query.getText().toString();
Properties 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");
// jis email id se mail bhejni hai.. wo email id & passwrd..
session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mEmail, mPassword);
}
});
pdialog = ProgressDialog.show(this, "", "Sending Mail..", true);
RetreiveFeedTask task = new RetreiveFeedTask();
task.execute();
}
class RetreiveFeedTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(mEmail));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(RecTo));
message.setSubject(subject);
message.setContent(mQuery, "text/plain; charset=utf-8");
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (mName == "" || mEmail == "" || mPassword == "" || subject == ""
|| mQuery == "") {
Toast.makeText(getApplicationContext(), "Enter Details",
Toast.LENGTH_LONG).show();
} else {
name.setText("");
phone.setText("");
query.setText("");
Toast.makeText(getApplicationContext(), "Message Sent",
Toast.LENGTH_LONG).show();
}
pdialog.dismiss();
}
}
The problem is that my code is neither sending emails nor showing any errors.