1

I've read the question linked here. I've tried this method to send email programmatily when, for example, user press a button.

It does works on 2.3.7 version of Android.

The issue is when trying to launch the app on more recent version of Android (like 4.1.2) it does nothin. On those devices the toast appears but email is not send.

So, i try this code, but it does not work neither.

...
sendButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {

            //Read EditText
            EditText text = (EditText)findViewById(R.id.editText1);
            String my_mail = text.getText().toString().toLowerCase();

            if (my_mail.matches("")){
                Toast toast = Toast.makeText(context, "Please insert a valid email address!", Toast.LENGTH_LONG); 
                toast.show();
            }

            //send mail
            try {   
                            WebSendMail(my_mail);

                            Toast toast = Toast.makeText(context, "Email sent!",     Toast.LENGTH_LONG); 
                toast.show();
                    } catch (Exception e) {   
                            Log.e("SendMail", e.getMessage(), e);   
                    }

        }
    });

public static boolean WebSendMail(String mail){

       //email sender parameter
       String smtpHost = "smtp.gmail.com"; 
       String addressFrom = "account@gmail.com";
       String login = addressFrom;
       String password = "password";
       String subject = "Subject!";

       String message = "html code";

       try {
       Properties props = new Properties();
       props.setProperty("mail.host", smtpHost);
       props.setProperty("mail.smtp.port", "587");
       props.setProperty("mail.smtp.auth", "true");
       props.setProperty("mail.smtp.starttls.enable", "true");

       Authenticator auth = new SMTPAuthenticator(login, password);

       Session session = Session.getInstance(props, auth);

       MimeMessage msg = new MimeMessage(session);
       msg.setText(message);
       msg.setSubject(subject);
       msg.setContent(message, "text/html");
       msg.setFrom(new InternetAddress(addressFrom));
       msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mail));
       Transport.send(msg);
       return true;

       } catch (Exception ex) {
           return false;
       }

}

Any help?

Community
  • 1
  • 1
Stefano Munarini
  • 2,711
  • 2
  • 22
  • 26
  • I think you'd be better off figuring out why the solution in the linked question (which seems pretty complete) is not working in 4.1.2 than going with a new SMTP-based solution. Have you tried debugging that version? – antlersoft May 15 '13 at 15:47
  • @antlersoft yes when debugging it, on pressing the bottom that should send email, debugger says: **Thread [<12> android.hardware.SystemSensorManager$SensorThread] (Running)** That is the exactly message that compare when i go into the page that contain that button – Stefano Munarini May 15 '13 at 15:51
  • I've resolved the problem adding: *props.put("mail.smtp.connectiontimeout", "t1");* *props.put("mail.smtp.timeout", "t2");* where String t1 = "2000"; String t2 = "1000"; – Stefano Munarini May 27 '13 at 12:39

1 Answers1

1

You might be figuring this error

android.os.NetworkOnMainThreadException

See this http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html

and use this http://developer.android.com/reference/android/os/AsyncTask.html

  • Hey. Thanks for your help. I'm not figuring out any kind of error. It just does not work! I tried to execute it in background (as shown [here] http://stackoverflow.com/questions/14374578/using-asynctask-to-send-android-email ) but it does not work either. – Stefano Munarini May 16 '13 at 09:40
  • Why don't you use the emailIntent ? http://www.codeproject.com/Tips/268122/Send-email-with-attachment-by-android – Jonathan Lobato May 17 '13 at 19:41