I want to send an email in android app without using email client(without user interaction). I have code and I tried it in my app. But when I tried to run, it showed a notification "Message sent". But I did not receive any email from the particular id which I mentioned in the code. I did not get any error or warnings too. Please anyone help me to get out of this problem. Thanks
Here is my code
//Import files
public class SendReport extends ActionBarActivity implements View.OnClickListener {
Button button;
EditText editText, editText2;
String recipient;
Session session = null;
ProgressDialog pdialog = null;
Context context = null;
String textmessage = null;
String username = "[email protected]";
String password = "senderpassword";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_report);
context = this;
button = (Button) findViewById(R.id.sendbutton);
editText = (EditText) findViewById(R.id.textView2);
editText2 = (EditText) findViewById(R.id.textView4);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
recipient = editText.getText().toString();
textmessage = editText2.getText().toString();
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
pdialog = ProgressDialog.show(context, "", "Sending Mail...", true);
RetrieveFeedTask task = new RetrieveFeedTask();
task.execute();
}
class RetrieveFeedTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
try
{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(recipient));
message.setContent(textmessage,"text/html; charset=utf-8");
Transport.send(message);
}
catch(MessagingException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String feed) {
pdialog.dismiss();
editText2.setText("");
editText.setText("");
Toast.makeText(getApplicationContext(), "Message sent", Toast.LENGTH_SHORT).show();
}
}