从Android应用程序发送电子邮件到用户电子邮件

时间:2014-01-16 12:32:57

标签: android

当我尝试从Android应用程序向用户电子邮件发送电子邮件时

我的应用程序显示异常:

java.lang.NullPointerException

我的代码是:

        Mail m = new Mail("mbrm.orion@gmail.com", "*******************");
        String[] toArr = { em };
        m.setTo(toArr);
        m.setFrom("mbrm.yu@gmail.com");
        m.setSubject("[MBRM Orion] Current location of phone: " + pn);
        m.setBody(text);
        try {
            if (m.send()) {
                Toast.makeText(getApplication(),
                        "Email was sent successfully.", Toast.LENGTH_LONG);// .show();
            } else {
                Toast.makeText(getApplication(), "Email was not sent.",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(getApplication(), e.toString(), Toast.LENGTH_LONG).show();
            Log.e("MailApp", "Could not send email", e);
        }

任何人都可以帮助我

LogCat是:

01-16 13:22:55.955: E/MailApp(6423): Could not send email
01-16 13:22:55.955: E/MailApp(6423): java.lang.NullPointerException: value == null
01-16 13:22:55.955: E/MailApp(6423):    at java.util.Hashtable.put(Hashtable.java:367)
01-16 13:22:55.955: E/MailApp(6423):    at com.example.MBRMOrion.Mail._setProperties(Mail.java:148)
01-16 13:22:55.955: E/MailApp(6423):    at com.example.MBRMOrion.Mail.send(Mail.java:89)
01-16 13:22:55.955: E/MailApp(6423):    at com.example.MBRMOrion.GPS.SendingEmail(GPS.java:637)
01-16 13:22:55.955: E/MailApp(6423):    at com.example.MBRMOrion.GPS$8$1.run(GPS.java:604)
01-16 13:22:55.955: E/MailApp(6423):    at android.os.Handler.handleCallback(Handler.java:725)
01-16 13:22:55.955: E/MailApp(6423):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-16 13:22:55.955: E/MailApp(6423):    at android.os.Looper.loop(Looper.java:137)
01-16 13:22:55.955: E/MailApp(6423):    at android.app.ActivityThread.main(ActivityThread.java:5041)
01-16 13:22:55.955: E/MailApp(6423):    at java.lang.reflect.Method.invokeNative(Native Method)
01-16 13:22:55.955: E/MailApp(6423):    at java.lang.reflect.Method.invoke(Method.java:511)
01-16 13:22:55.955: E/MailApp(6423):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-16 13:22:55.955: E/MailApp(6423):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-16 13:22:55.955: E/MailApp(6423):    at dalvik.system.NativeStart.main(Native Method)

提前感谢...

1 个答案:

答案 0 :(得分:1)

可能是变量' em'或者' text'或者' pn'未初始化。您是否存储了值或它们是否为null? 您还可以使用意图使用默认电子邮件应用程序发送电子邮件:

                Intent i=new Intent(Intent.ACTION_SEND);
                i.setType("message/rfc822");
                i.putExtra(Intent.EXTRA_SUBJECT, "subject");
                i.putExtra(Intent.EXTRA_EMAIL,new String[]{"example@dev.com"});
                startActivity(Intent.createChooser(i, "Choose Messaging app:"));