无法通过javamail-android发送邮件

时间:2015-02-11 14:29:25

标签: android smtp javamail

我阅读了javamail-android的指南和stackoverflow上的一些帖子。例如。 How do you send mail in Android using JavaMail API? 我收到消息“邮件发送成功...”。但邮件不会收到收件人地址。我有以下输出:

02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ android.os.NetworkOnMainThreadException
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at java.net.InetAddress.getByName(InetAddress.java:289)
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.URLName.getHostAddress(URLName.java:487)
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.URLName.hashCode(URLName.java:463)
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at java.util.Collections.secondaryHash(Collections.java:3405)
02-11 09:53:38.988    2674-2674/com.example.mailapp W/System.err﹕ at java.util.Hashtable.get(Hashtable.java:265)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.Session.getPasswordAuthentication(Session.java:823)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.Service.connect(Service.java:271)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.Service.connect(Service.java:169)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.Service.connect(Service.java:118)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.Transport.send0(Transport.java:188)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at javax.mail.Transport.send(Transport.java:118)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at com.example.mailapp.GMailSender.sendMail(GMailSender.java:64)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at com.example.mailapp.MyActivity$1.onClick(MyActivity.java:35)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at android.view.View.performClick(View.java:4438)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-11 09:53:38.998    2674-2674/com.example.mailapp W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

如何修复或使用其他方式发送电子邮件?

2 个答案:

答案 0 :(得分:0)

您收到此错误是因为您正在主线程上执行网络操作,网络操作需要前所未有的时间,不应在主线程上调用。使用Async task即可。异步任务将创建另一个线程来执行网络任务(您也可以将它用于任何任务)。

添加代码以在doInBackground方法中执行操作,并使用以下方法调用它:

   new senmailAsync().execute();

例如:

private class senmailAsync extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            GMailSender gMailSender = new GMailSender();
            gMailSender.sendMail("hi", "hi", authPreferences.getUser(), authPreferences.getToken(), "ranjithdevacc@gmail.com");
            Log.v("ranjapp", "sent mail " + authPreferences.getUser() + "  " + authPreferences.getToken());
            return null;
        }
    }

完成教程here

答案 1 :(得分:0)

您还可能需要将参数传递给类。你可以这样做:

public class MailSenderAsyncClass extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... params) {
        MailSenderClass sender = new MailSenderClass(Settings.Login, Settings.Pass);
        try {
            sender.sendMail(params[2],params[3],params[0],params[1],params[4]);
        } catch (Exception e) {
            Log.e("SendMail", e.getMessage(), e);
            e.printStackTrace();
        }

        return null;
    }
}

new MailSenderAsyncClass().execute(Settings.EmailFrom, Settings.EmailTo, "Subject", "text of mail", Settings.AnotherParam);