我试图使用Google的Gmail API和OAuth 2.0发送电子邮件。我设法得到看起来是有效的SMTPTransport(不完全确定它是否有效),但是当我尝试发送电子邮件时,sendMessage发生的线路上会出现NullPointerException。实际上,这个例外并不是完全在这条线上发生的。奇怪的是,在Eclipse中,调试器中的当前行返回到sendMail方法的第一行,然后返回给调用客户端,并发生NullPointerException异常。我不知道是什么导致了空例外。该smtpTransport和味精的变量是不是空:NullPointerException尝试在Android中使用OAuth发送电子邮件
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中得到一个异常如下:
找不到类“的javax.activation.DataHandler”,从 方法myapp.sendMail引用
不知道为什么找不到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 [email protected] (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
包括你的logcat – 2013-02-28 13:22:44
我加入它,以及更新的代码在Sendmail的方法。 logcat中没有显示出来。 – AndroidDev 2013-02-28 13:45:37