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
1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Specifically it was the transport/send parts of this that helped me out. Thank you for the references.
1

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

1 Comment

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.
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.

1 Comment

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!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.