无法连接到python上的imap服务器

时间:2018-03-23 19:58:39

标签: python imap

我是一名网络学生,他的编程经验很少,我手上有简单的项目,我需要做一个简单的通知,通知用户当电子邮件到达Gmail时。在过去的几周里,我一直试图让这个工作起来,我老老实实地陷入了困境。我使用下面的代码,我认为问题是身份验证位,我无法连接到服务器。所有帮助表示赞赏。

import imaplib
import re
import time
import subprocess

## enter your account details bellow!
imapServer = "imap.gmail.com"
port = "993"
username = "example@gmail.com"
password = "password"

##how often to check ? give interval in seconds! Checking too often might performance and stability.
checkInterval = 120

Mailbox = imaplib.IMAP4_SSL(imapServer, port)
rc,resp = Mailbox.login(username,password)
if rc == 'OK':
    print("Connected to mail-server " + imapServer)
rc, message = Mailbox.status('INBOX', "(UNSEEN)")
unreadCount = int(re.search("UNSEEN (\d+)",str( message[0])).group(1))
oldValue = 0
file = open("%sytemdrive%\Windows\Temp\mailnotify.tmp", "w+")
file.write(str(unreadCount))
file.close
while(1):
    rc, message = Mailbox.status('INBOX', "(UNSEEN)")
unreadCount = int(re.search("UNSEEN (\d+)",str( message[0])).group(1))
file = open("%sytemdrive%\Windows\Temp\mailnotify.tmp", "w+")
oldValue = int(file.readline())
file.close()
if (unreadCount>oldValue):
    subprocess.call(["notify-send", "-u", "low", "low", "t", "5000", "New email!", "New email!",
                     "You have " + str(unreadCount) + " unread " + "emails!" if unreadCount > 1 else "email!",
                     "--icon=email"])
    if oldValue != unreadCount:
        file = open("%sytemdrive%\Windows\Temp\mailnotify.tmp", "w+")
    file.write(str(unreadCount))
    file.close()
    time.sleep(checkInterval)
else :
    print('Fail to connect')
    Mailbox.logout()
    file.remove()

0 个答案:

没有答案