在Android中打印使用python发送的电子邮件内容的问题

时间:2016-06-01 15:32:56

标签: android python email javamail

我正在制作一个必须接收电子邮件的Android应用程序,该电子邮件已通过python脚本发送到gmail帐户,并在主题和邮件正文中搜索某些短语。我没有设置电子邮件服务的任何问题(我使用smtp库发送python,javamail IMAP在android studio中读取),或者自己发送和打开电子邮件。我也能够在主题中搜索我想要的任何短语或字符,但是当我编写一些测试代码来尝试打印消息体时,没有任何内容被打印出来。我已经检查过电子邮件实际上是通过在浏览器中打开gmail来正确格式化的,并且内容肯定存在,但是当我使用javamail的getContent()并将其转换为字符串时,没有任何内容被打印出来。我知道getContent()可能很奇怪,因为它返回一个对象,但由于我发送的消息只是文本,没有图像或附件,我以为只使用toString()就行了。

这是适用的代码(删除了用户名和密码)

的Python:

message = """From:  Sender <send address>
To: Receiver <receive address>
Subject: test

test message
"""
smtpObj = smtplib.SMTP('smtp.gmail.com')
smtpObj.starttls()
smtpObj.login(username, password)
smtpObj.sendmail(sender, receiver, message)

Android(我已经跳过所有登录和会话代码,因为我知道它有效)

Message messages[] = inbox.getMessages();
System.out.println("Reading messages...");
    for(Message message:messages) {
        for (Address a: message.getFrom()){
            System.out.println("From:" + a);
            System.out.println("Title: " + message.getSubject());
            System.out.println();
            System.out.println(message.getContent().toString());
            System.out.println("---");
        }
    }

这是打开邮件时android studio中的打印输出:

I/System.out: From:Sender <send address>
I/System.out: Title: test
I/System.out: ---

所以它只是打印好像内容为空,但是当我在浏览器中打开电子邮件时它肯定存在。我确定我在使用message.getContent()做错了,但我查找并尝试的所有内容都有相同的结果。关于如何正确打印和存储内容为字符串,或正确使用getContent()的任何建议将不胜感激。谢谢!

编辑:更新了javamail,现在getContent()行有classDef错误,我不知道如何解决。

DEBUG: setDebug: JavaMail version 1.5.5
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]
W/art: Before Android 4.1, method javax.mail.Session com.sun.mail.imap.IMAPStore.getSession() would have incorrectly overridden the package-private method in javax.mail.Service
DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: closeFoldersOnStoreFailure
DEBUG IMAPS: trying to connect to host "imap.gmail.com", port 993, isSSL true
* OK Gimap ready for requests from 71.53.10.66 m64mb113652972qkl
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH
A0 OK Thats all she wrote! m64mb113652972qkl
DEBUG IMAPS: AUTH: XOAUTH2
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: AUTH: PLAIN-CLIENTTOKEN
DEBUG IMAPS: AUTH: OAUTHBEARER
DEBUG IMAPS: AUTH: XOAUTH
DEBUG IMAPS: protocolConnect login, host=imap.gmail.com, user=********, password=<non-null>
DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAPS: AUTHENTICATE PLAIN command result: A1 OK ******** authenticated (Success)
A2 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT APPENDLIMIT=35882577 LIST-EXTENDED LIST-STATUS
A2 OK Success
DEBUG IMAPS: connection available -- size: 1
A3 SELECT Inbox
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)
* OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing \*)] Flags permitted.
* OK [UIDVALIDITY 1] UIDs valid.
* 8 EXISTS
* 0 RECENT
* OK [UIDNEXT 1204] Predicted next UID.
* OK [HIGHESTMODSEQ 84995]
A3 OK [READ-WRITE] Inbox selected. (Success)
I/art: Rejecting re-init on previously-failed class java.lang.Class<javax.mail.internet.MimeBodyPart$MimePartDataHandler>
I/art: Rejecting re-init on previously-failed class java.lang.Class<javax.mail.internet.MimePartDataSource>
I/art: Rejecting re-init on previously-failed class java.lang.Class<com.sun.mail.imap.IMAPMultipartDataSource>
I/System.out: Reading messages...
A4 FETCH 1 (ENVELOPE INTERNALDATE RFC822.SIZE)
* 1 FETCH (RFC822.SIZE 3287 INTERNALDATE "03-Jun-2016 02:10:05 +0000" ENVELOPE ("Thu, 02 Jun 2016 19:10:04 -0700 (PDT)" "test" ((NIL NIL "network.test.recv" "gmail.com")) ((NIL NIL "network.test.recv" "gmail.com")) ((NIL NIL "network.test.recv" "gmail.com")) ((NIL NIL "network.test.send" "gmail.com")) NIL NIL NIL "<5750e6fc.335a8c0a.86b06.2ffb@mx.google.com>"))
A4 OK Success
I/System.out: From:*********
I/System.out: Title: test
A5 FETCH 1 (BODYSTRUCTURE)
* 1 FETCH (BODYSTRUCTURE ("TEXT" "PLAIN" NIL NIL NIL "7BIT" 14 1 NIL NIL NIL))
A5 OK Success
I/art: Rejecting re-init on previously-failed class java.lang.Class<javax.mail.internet.MimeBodyPart$MimePartDataHandler>

             --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: Thread-220
               Process: com.example.thecortex.myapplication, PID: 24663
               java.lang.NoClassDefFoundError: javax.mail.internet.MimeBodyPart$MimePartDataHandler
                               at javax.mail.internet.MimeMessage.getDataHandler(MimeMessage.java:1422)
                               at com.sun.mail.imap.IMAPMessage.getDataHandler(IMAPMessage.java:776)
                               at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:1454)
                               at com.example.thecortex.myapplication.MainActivity$1$override.run(MainActivity.java:50)
                               at com.example.thecortex.myapplication.MainActivity$1$override.access$dispatch(MainActivity.java)
                               at com.example.thecortex.myapplication.MainActivity$1.run(MainActivity.java:0)
                               at java.lang.Thread.run(Thread.java:818)
DEBUG IMAPS: IMAPStore cleanup, force false
DEBUG IMAPS: close folder
A6 UNSELECT
A6 OK Returned to authenticated state. (Success)
DEBUG IMAPS: added an Authenticated connection -- size: 1
A7 LOGOUT
* BYE LOGOUT Requested
A7 OK 73 good day (Success)
DEBUG IMAPS: IMAPStore cleanup done
DEBUG IMAPS: IMAPStore cleanup, not connected

1 个答案:

答案 0 :(得分:0)

我过去也有一些问题,这是由于一个奇怪的缩进。如果你看下面,From / TO / Subjct从行的开头就没有空格。 取自一个def:

terminate called after throwing an instance of 'std::bad_array_new_length'
  what():  std::bad_array_new_length

之后我正在使用smtp.sendmail(FROM,TO,message)。缩进非常相关,我将替换消息中已有的FROM和TO,SUBJECT和TEXT。我希望这会有所帮助。