简体   繁体   中英

Sending email from Android app

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

you can not use

System.getProperties();

in android use

emailIntent.putExtra

try this example

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM