0

我正在尝试使用 Google 的 Gmail API 和 OAuth 2.0 发送电子邮件。我设法获得了似乎是有效的 SMTPTransport(不完全确定它是否有效),但是当我尝试发送电子邮件时,我在 sendMessage 发生的行上收到了 NullPointerException。实际上,异常并不完全发生在那条线上。奇怪的是,在 Eclipse 中,调试器中的当前行返回到 sendMail 方法的第一行,然后返回到调用客户端并返回 NullPointerException。我不知道是什么导致了空异常。smtpTransport 和 msg 变量不为空:

  private Session session;

  public SMTPTransport connectToSmtp(String host, int port, String userEmail, String oauthToken, boolean debug) throws Exception
  {
    try
    {
      Properties props = new Properties();
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.starttls.required", "true");
      props.put("mail.smtp.sasl.enable", "false");
      session = Session.getInstance(props);
      session.setDebug(debug);

      final URLName unusedUrlName = null;
      SMTPTransport transport = new SMTPTransport(session, unusedUrlName);

      // If the password is non-null, SMTP tries to do AUTH LOGIN.
      final String emptyPassword = null;
      transport.connect(host, port, userEmail, emptyPassword);

      byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", userEmail, oauthToken).getBytes();
      response = BASE64EncoderStream.encode(response);

      transport.issueCommand("AUTH XOAUTH2 " + new String(response), 235);

      return transport;
    }
    catch (Exception ex)
    {
      return null;
    }
  }

  public synchronized void sendMail(String subject, String body, String user, String oauthToken, String recipients)
  {
    try
    {
      SMTPTransport smtpTransport = connectToSmtp("smtp.gmail.com", 587, user, oauthToken, true);

      MimeMessage message = new MimeMessage(session);
      DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
      message.setSender(new InternetAddress(user));
      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));

      smtpTransport.sendMessage(message, message.getAllRecipients());
    }
    catch (Exception e)
    {
    }
  }

编辑: 我更新了 sendMail 中的代码,但仍然有同样的问题。logcat 没有显示任何异常。事实上,与服务器的连接是被接受的。但是,如果我多次运行代码,我最终会在 logcat 中得到一个异常,如下所示:

找不到从方法 myapp.sendMail 引用的类“javax.activation.DataHandler”

不知道为什么它找不到 DataHandler。可能缺少在编译时未检测到但仅在运行时检测到的 jar 文件?

02-28 14:33:14.619: I/ActivityManager(199): Starting: Intent { act=com.google.android.gms.common.account.CHOOSE_ACCOUNT cmp=com.google.android.gms/.common.account.AccountPickerActivity (has extras) } from pid 967
02-28 14:33:14.619: E/ActivityManager(199): exception bw.write()java.io.IOException: Transport endpoint is not connected
02-28 14:33:14.619: D/PowerManagerService(199): acquireWakeLock flags=0x1 tag=ActivityManager-Launch
02-28 14:33:14.899: E/ActivityRecord(199): sendActivityPerformanceInfo exception occurs: java.io.IOException: Transport endpoint is not connected
02-28 14:33:14.899: I/ActivityManager(199): Displayed com.google.android.gms/.common.account.AccountPickerActivity: +260ms
02-28 14:33:14.909: D/PowerManagerService(199): releaseWakeLock flags=0x1 tag=ActivityManager-Launch
02-28 14:33:16.879: E/ActivityManager(199): exception bw.write()java.io.IOException: Transport endpoint is not connected
02-28 14:33:16.879: D/PowerManagerService(199): acquireWakeLock flags=0x1 tag=ActivityManager-Launch
02-28 14:33:17.089: I/ActivityManager(199): Process com.google.android.gms (pid 642) has died.
02-28 14:33:18.629: D/PowerManagerService(199): acquireWakeLock flags=0xa tag=KEEP_SCREEN_ON_FLAG
02-28 14:33:18.649: W/InputManagerService(199): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@40928628 (uid=10025 pid=1085)
02-28 14:33:18.719: D/PowerManagerService(199): releaseWakeLock flags=0x1 tag=ActivityManager-Launch
02-28 14:33:22.139: D/PowerManagerService(199): releaseWakeLock flags=0xa tag=KEEP_SCREEN_ON_FLAG
02-28 14:33:23.969: I/System.out(967): DEBUG: setDebug: JavaMail version 1.4.6
02-28 14:33:23.989: I/System.out(967): DEBUG SMTP: useEhlo true, useAuth false
02-28 14:33:24.009: I/System.out(967): DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
02-28 14:33:24.139: I/System.out(967): 220 mx.google.com ESMTP 44sm11905543eek.5 - gsmtp
02-28 14:33:24.139: I/System.out(967): DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
02-28 14:33:24.149: I/System.out(967): EHLO localhost
02-28 14:33:24.219: I/System.out(967): 250-mx.google.com at your service, [217.238.138.215]
02-28 14:33:24.239: I/System.out(967): 250-SIZE 35882577
02-28 14:33:24.239: I/System.out(967): 250-8BITMIME
02-28 14:33:24.239: I/System.out(967): 250-STARTTLS
02-28 14:33:24.259: I/System.out(967): 250 ENHANCEDSTATUSCODES
02-28 14:33:24.269: I/System.out(967): DEBUG SMTP: Found extension "SIZE", arg "35882577"
02-28 14:33:24.269: I/System.out(967): DEBUG SMTP: Found extension "8BITMIME", arg ""
02-28 14:33:24.269: I/System.out(967): DEBUG SMTP: Found extension "STARTTLS", arg ""
02-28 14:33:24.279: I/System.out(967): DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
02-28 14:33:24.289: I/System.out(967): STARTTLS
02-28 14:33:24.359: I/System.out(967): 220 2.0.0 Ready to start TLS
02-28 14:33:25.319: I/System.out(967): EHLO localhost
02-28 14:33:25.589: I/System.out(967): 250-mx.google.com at your service, [217.238.138.215]
02-28 14:33:25.599: I/System.out(967): 250-SIZE 35882577
02-28 14:33:25.609: I/System.out(967): 250-8BITMIME
02-28 14:33:25.609: I/System.out(967): 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
02-28 14:33:25.619: I/System.out(967): 250 ENHANCEDSTATUSCODES
02-28 14:33:25.629: I/System.out(967): DEBUG SMTP: Found extension "SIZE", arg "35882577"
02-28 14:33:25.639: I/System.out(967): DEBUG SMTP: Found extension "8BITMIME", arg ""
02-28 14:33:25.639: I/System.out(967): DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2"
02-28 14:33:25.639: I/System.out(967): DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
02-28 14:33:25.679: I/System.out(967): AUTH XOAUTH2 ??????????????????????????????==
02-28 14:33:26.309: I/System.out(967): 235 2.7.0 Accepted
4

1 回答 1

0

事实证明,用于邮件内容的 jar 文件不兼容。Android 所需的 jar 文件可以在以下位置下载:

https://code.google.com/p/javamail-android/downloads/list

于 2013-02-28T14:20:32.407 回答