2

I am trying to a develop an android app that sends email using JavaMail. I have tried the code bellow as console application and it works, but when I use in as an android app from the emulator it throws exception with no message. I have modified the manifest.xml and put , but it still doesn’t work. The exception is thrown at message.setText("Welcome to JavaMail"); So please help me out!

I am using the mail.jar and activation.jar from Sun.

Bellow is the full code on the ClickHandler.

 public void btnSendClickHandler(View view)
    {
         try{
            String host = "smtp.gmail.com";
            String from = "username@gmail.com";
            String pass = "password";
            Properties props = System.getProperties();
            props.put("mail.smtp.starttls.enable", "true"); // added this line
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.user", from);
            props.put("mail.smtp.password", pass);
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.auth", "true");

            String[] to = {"toEmailAddress@gmail.com"}; // added this line

            Session session = Session.getDefaultInstance(props, null);

            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));

            InternetAddress[] toAddress = new InternetAddress[to.length];

            // To get the array of addresses
            for( int i=0; i < to.length; i++ ) { 
                toAddress[i] = new InternetAddress(to[i]);
            }


            for( int i=0; i < toAddress.length; i++) {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }

            message.setSubject("sending in a group");
            message.setText("Welcome to JavaMail");//The exception is thrown here   

            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
    } catch(Exception e){Toast.makeText(this, e.toString(),
            Toast.LENGTH_LONG).show();}
}
MByD
  • 135,866
  • 28
  • 264
  • 277
user709807
  • 21
  • 1

2 Answers2

0

you can not use

System.getProperties();

in android use

emailIntent.putExtra

try this example

Nirmal- thInk beYond
  • 11,847
  • 8
  • 35
  • 46
0

I am not familiar with the JavaMail jar, but if it is referencing classes that are not a part of Android it will not work. I have found some sites that are creating ports of JavaMail for Android though.

http://code.google.com/p/javamail-android/

http://groups.google.com/group/android-developers/browse_thread/thread/9c7bca0a1b6957a9

Please note that the library at the second link is not free to use, and you should contact the developer before implementing.

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • I have found out that the problem is more than it seems. JavaMial doesn't work well on Android, but luckly I have found some website discussion that has fixed this problem ( www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android ). If you read the comments and do as the post, it will work. – user709807 Apr 26 '11 at 11:17