Pregunta

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
¿Fue útil?

Solución

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.

Otros consejos

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

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.

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top