Search code examples
androidemailgmailjakarta-mail

Sending an email using javamail without user interaction - java.net.ConnectException


Hello
I am attempting to create an Android application that sends emails without the need for user interaction (the user will be informed of any emails being sent). I am using this javamail: http://code.google.com/p/javamail-android/downloads/list and following this example: http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_%28no_Intents%29_in_Android

but I am getting the following errors:

10-07 09:23:10.373: E/MailApp(9173): Could not send email
10-07 09:23:10.373: E/MailApp(9173): javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
10-07 09:23:10.373: E/MailApp(9173):   nested exception is:
10-07 09:23:10.373: E/MailApp(9173):    java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 25): connect failed: ECONNREFUSED (Connection refused)
10-07 09:23:10.373: E/MailApp(9173):    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
10-07 09:23:10.373: E/MailApp(9173):    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)



code for sending the email:

Thread emailSendThread = new Thread(new Runnable() {

    @Override
    public void run() {


            while (true) {  

                try {
                    Thread.sleep(100);
                } 

                catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                    if (sendEmail == true) {

                        try {

                             MailSender m = new MailSender("[email protected]", "password"); 

                             String[] toArr = {"[email protected]"}; 
                             m.setTo(toArr); 
                             m.setFrom("[email protected]"); 
                             m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device."); 
                             m.setBody("Email body."); 

                             try { 

                                 m.send();

                               if(m.send()) { 
                                 Toast.makeText(MainActivity.this, "Email was sent successfully.", Toast.LENGTH_LONG).show(); 
                               } else { 
                                 Toast.makeText(MainActivity.this, "Email was not sent.", Toast.LENGTH_LONG).show(); 
                               } 
                             } catch(Exception e) { 
                               //Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show(); 
                               Log.e("MailApp", "Could not send email", e); 
                             } 


                        } 

                        catch (Exception e) {
                            Log.e("email", "Error", e);

                        }

                            try {
                                Thread.sleep(100);
                            }

                            catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }


                            sendEmail = false;
                    }

            }
    }

});

I have substituted my email username and password for example ones in this code.
to send an email I execute sendEmail = true;


Solution

  • You can only send mails using an SMTP server. If your SMTP server is not at the device which is running the application, then you need to provide the address of the SMTP which can send the email. It is actually expected that your application does not successfully send the email if the SMTP details are not correct.