0

我正在尝试在后台发送一封自动电子邮件作为用户通知,我在下一行收到空指针异常,

Transport.send(message);  

任何人都可以帮忙吗?谢谢。

这是我的片段,

 public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {   
    try{

    MimeMessage message = new MimeMessage(session);   
    DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));   
    message.setSender(new InternetAddress(sender));   
    message.setSubject(subject);   
    message.setDataHandler(handler);   
    if (recipients.indexOf(',') > 0)   
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
    else  
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   

       //    Log.v("ziad", "wanna send...");
         Transport.send(message);   
      //   Log.v("ziad", "sent...");
    }catch(Exception e){

        Log.v("ziad",e.getMessage());
    }
} 

我也添加了互联网许可。

这是我的 logCat,

05-02 17:57:26.266: ERROR/SendMail(1220): println needs a message
05-02 17:57:26.266: ERROR/SendMail(1220): java.lang.NullPointerException: println needs a message
05-02 17:57:26.266: ERROR/SendMail(1220):     at android.util.Log.println_native(Native Method)
05-02 17:57:26.266: ERROR/SendMail(1220):     at android.util.Log.v(Log.java:116)
05-02 17:57:26.266: ERROR/SendMail(1220):     at zaid.project.EmailSender.GmailSender.sendMail(GmailSender.java:74)
05-02 17:57:26.266: ERROR/SendMail(1220):     at zaid.project.EmailSender.EmailSenderActivity$1.onClick(EmailSenderActivity.java:33)
05-02 17:57:26.266: ERROR/SendMail(1220):     at android.view.View.performClick(View.java:2485)
05-02 17:57:26.266: ERROR/SendMail(1220):     at android.view.View$PerformClick.run(View.java:9080)
05-02 17:57:26.266: ERROR/SendMail(1220):     at android.os.Handler.handleCallback(Handler.java:587)
05-02 17:57:26.266: ERROR/SendMail(1220):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-02 17:57:26.266: ERROR/SendMail(1220):     at android.os.Looper.loop(Looper.java:123)
05-02 17:57:26.266: ERROR/SendMail(1220):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-02 17:57:26.266: ERROR/SendMail(1220):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 17:57:26.266: ERROR/SendMail(1220):     at java.lang.reflect.Method.invoke(Method.java:507)
05-02 17:57:26.266: ERROR/SendMail(1220):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-02 17:57:26.266: ERROR/SendMail(1220):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-02 17:57:26.266: ERROR/SendMail(1220):     at dalvik.system.NativeStart.main(Native Method)
05-02 17:57:27.946: DEBUG/dalvikvm(324): GC_EXPLICIT freed 4K, 54% free 2538K/5511K, external 1625K/2137K, paused 254ms
05-02 17:57:32.875: DEBUG/dalvikvm(136): GC_EXPLICIT freed 38K, 51% free 2958K/5959K, external 4845K/5329K, paused 126ms
4

1 回答 1

1

也许MimeMessage需要它的文本集?尝试像这样设置一个空白消息:

message.setText("");

看看这是否摆脱了你的 NPE。

编辑:实际上,我认为可能是这一行:

Log.v("ziad",e.getMessage());

尝试将其注释掉,看看是否有帮助。

于 2012-05-02T15:08:42.540 回答