3

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();
        }
    }

1 Answer 1

4

I had this problem some months ago.

You have to allowing less secure apps to access your google account.

Change it here

Now your email must be correctly sent ;)

More information : https://support.google.com/accounts/answer/6010255?hl=en

Sign up to request clarification or add additional context in comments.

3 Comments

Hey man...thanks ..i have done. now am able to send mail. But am having another issue. I have hard coded my mail id and password. but it should not be the case. How can i do this task? do you have any idea??
Sorry, I'm not a security expert but this link may help you.
Thank you..let me try it..:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.