0

My sendEmail() function works in Eclipse with straight Java and I can send emails without any issue, but once I plug the function into my Android application none of the emails are being sent. I have added the javax.mail and javax.activation JARs to my build path in both cases. Here is the code for the function and then the LogCat reading. On a button click sendEmailCustomer() is called and nothing occurs. Any suggestions are more than appreciated.

public void sendEmailCustomer() {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.stmp.user", "user");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    Session session = Session.getDefaultInstance(props,
            new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    String username = "user";
                    String password = "password";
                    return new PasswordAuthentication(username, password);
                }
            });
    EditText e = (EditText) findViewById(R.id.enterEmail);
    String to = e.toString();
    String from = "user";
    String subject = "Testing...";
    MimeMessage msg = new MimeMessage(session);
    try {
        msg.setFrom(new InternetAddress(from));
        msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(
                to));
        msg.setSubject(subject);
        msg.setText("Did you get this?");
        Transport transport = session.getTransport("smtp");
        transport.send(msg);
    } catch (Exception exc) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(exc.toString());
        builder.setTitle("Error!");
        builder.setNeutralButton("OK",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                });

    }
}

Updated LogCat errors. It sounds like the application cannot access the internet even though it has permission in my manifest. Why is this?

04-03 12:38:07.166: W/IInputConnectionWrapper(2528): showStatusIcon on inactive InputConnection
04-03 12:38:07.166: W/IInputConnectionWrapper(2528):    InputConnection =               
  com.android.internal.widget.EditableInputConnection@411c5f28, active client = false
STLCards77
  • 121
  • 14
  • Possible duplicate of [Sending Email in Android](http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a) – rrirower Apr 03 '14 at 20:16

4 Answers4

1

Make sure you're referencing your JAR libraries in your project and at the same time, use the Android version of JavaMail too.

GoRoS
  • 5,183
  • 2
  • 43
  • 66
  • I believe this was an issue since I did take it straight from my other Eclipse application and didn't even think about changing libs. Thank you. I no longer am getting the LogCat errors but the emails are still not coming through. But as I said, thank you for this catch. – STLCards77 Apr 03 '14 at 20:48
1

You'll want to fix these common mistakes, and this mistake. You might just want to copy this code for connecting to Gmail. If that still doesn't work for you, follow these debugging steps to get more information so we can help you.

Note also that you can use the latest JavaMail jar file instead of the older mail.jar file linked to above.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
0

try to add this lines in your "AndroidManifest.xml" file

<manifest... >
....

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

....
</manifest>

maybe your problem is with the internet permission.

  • I did have the first line you suggest I add in place, I added the second and am still getting the same error. Thank you for your time and energy! – STLCards77 Apr 03 '14 at 20:34
0

Please refer to this SO thread for the solution. I also reccomend checking the permissions in your AndroidManifest.xml like SUNDAY suggested.

Community
  • 1
  • 1
Alvin Baena
  • 903
  • 1
  • 10
  • 24