python错误登录,甚至从gmail启用imap

2022-05-21 07:49:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python中使用imap,但每次都会出错,我按照所有步骤从gmail激活imap并解锁captcha,但在第一次运行后,我收到了此错误,请立即提供帮助,谢谢

raise self.error(dat[-1])
imaplib.error: b'[AUTHENTICATIONFAILED] Invalid credentials (Failure)'

gmail发布后,我收到了来自gmail的Alert security,来自imap的所有访问都被阻止

使用的代码是`

 # Importing libraries 
import imaplib, email 

    user = 'USER_EMAIL_ADDRESS'
    password = 'USER_PASSWORD'
    imap_url = 'imap.gmail.com'
    
    # Function to get email content part i.e its body part 
    def get_body(msg): 
        if msg.is_multipart(): 
            return get_body(msg.get_payload(0)) 
        else: 
            return msg.get_payload(None, True) 
    
    # Function to search for a key value pair 
    def search(key, value, con): 
        result, data = con.search(None, key, '"{}"'.format(value)) 
        return data 
    
    # Function to get the list of emails under this label 
    def get_emails(result_bytes): 
        msgs = [] # all the email data are pushed inside an array 
        for num in result_bytes[0].split(): 
            typ, data = con.fetch(num, '(RFC822)') 
            msgs.append(data) 
    
        return msgs 
    
    # this is done to make SSL connnection with GMAIL 
    con = imaplib.IMAP4_SSL(imap_url) 
    
    # logging the user in 
    con.login(user, password) 
    
    # calling function to check for email under this label 
    con.select('Inbox') 
    
    # fetching emails from this user "tu**h*****1@gmail.com" 
    msgs = get_emails(search('FROM', 'MY_ANOTHER_GMAIL_ADDRESS', con)) 
    
    # Uncomment this to see what actually comes as data 
    # print(msgs) 
    
    
    # Finding the required content from our msgs 
    # User can make custom changes in this part to 
    # fetch the required content he / she needs 
    
    # printing them by the order they are displayed in your gmail 
    for msg in msgs[::-1]: 
        for sent in msg: 
            if type(sent) is tuple: 
    
                # encoding set as utf-8 
                content = str(sent[1], 'utf-8') 
                data = str(content) 
    
                # Handling errors related to unicodenecode 
                try: 
                    indexstart = data.find("ltr") 
                    data2 = data[indexstart + 5: len(data)] 
                    indexend = data2.find("</div>") 
    
                    # printtng the required content which we need 
                    # to extract from our email i.e our body 
                    print(data2[0: indexend]) 
    
                except UnicodeEncodeError as e: 
                    pass


'

Tags: thetoinfordatagetemailmsgcontentthiscongmailimapusermsgs
1条回答
网友
1楼 ·

如果您是在云主机或类似设备上运行此功能,那么如果不实现OAuth身份验证(或者特定于应用程序的密码),此功能可能永远无法工作

谷歌的帐户安全设置似乎只允许可能的最终用户主机登录。如果您在您的个人计算机上运行它(您也从该计算机登录),则必须激活简化的安全设置、生成特定于应用程序的密码或实现OAuth