密码和用户名正确,但我收到 SMTPAuthenticationError

问题描述

我正在构建此脚本以通过网络摄像头检测我的脸,并让模型检测我是否戴口罩。如果检测到的人脸没有戴口罩,则会向管理员发送一封电子邮件以提醒他们。 除电子邮件部分外,一切正常。它不断给出 SMTPAuthenticationError 说我的用户名密码不正确,但它们是正确的。我可能做错了什么,我该如何解决? 我已通过设置授权 Google 授权安全性较低的应用程序。我也禁用了两步验证,但我仍然收到此错误

这是产生错误代码的一部分。

if (label == "No Mask"):
            # Throw a Warning Message to tell user to wear a mask if not wearing one. This will stay
            #open and No Access will be given He/She wears the mask
            messageBox.showwarning("Warning","Access Denied. Please wear a Face Mask")
            
            # Send an email alert to the administrator if access denied/user not wearing face mask 
            message = 'Subject: {}\n\n{}'.format(SUBJECT,TEXT)
            mail = smtplib.SMTP('smtp.gmail.com',587)
            mail.ehlo()
            mail.starttls()
            mail.login('[email protected]','#####')
            mail.sendmail('[email protected]','[email protected]',message)
            mail.close
            
        else:
            pass

这是错误: SMTPAuthenticationError: (535,b'5.7.8 用户名密码不被接受。了解更多信息:\n5.7.8 https://support.google.com/mail/?p=BadCredentials v25sm14100155wmh.4 - gsmtp')

这是整个脚本文件

from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
from imutils.video import VideoStream
import numpy as np
import imutils
import time
import cv2
import os

import tkinter
from tkinter import messageBox
import smtplib

# Initialize Tkinter
root = tkinter.Tk()
root.withdraw()

SUBJECT = "Subject"   
TEXT = "One Visitor violated Face Mask Policy. See in the camera to recognize user. A Person has been detected without a face mask in the Hotel Lobby Area 9. Please Alert the authorities."



def find_face_and_mask(frame,detectFace,detectMask):
#get face dimensions and construct blob
    (h,w) = frame.shape[:2]
    blob = cv2.dnn.blobFromImage(frame,1.0,(224,224),(104.0,177.0,123.0))

    #obtaining the face
    detectFace.setInput(blob)
    detections = detectFace.forward()
    print(detections.shape)


    faces = [] # faces
    locs = [] #locations
    preds = [] #predictions

    for i in range(0,detections.shape[2]):
        
        confidence = detections[0,i,2]

        if confidence > 0.5: #setting min confidence
            Box = detections[0,3:7] * np.array([w,h,w,h])
            (startX,startY,endX,endY) = Box.astype("int")

            (startX,startY) = (max(0,startX),max(0,startY))
            (endX,endY) = (min(w - 1,endX),min(h - 1,endY))

            # extracting the face ROI and preprocessing
            face = frame[startY:endY,startX:endX]
            face = cv2.cvtColor(face,cv2.COLOR_BGR2RGB)
            face = cv2.resize(face,224))
            face = img_to_array(face)
            face = preprocess_input(face)

            #append faces and locations
            faces.append(face) 
            locs.append((startX,endY))

    # only make a predictions if at least one face was detected
    if len(faces) > 0:
        # making batch predictions on *all* faces at the same time rather than one-by-one predictions
        faces = np.array(faces,dtype="float32")
        preds = detectMask.predict(faces,batch_size=100)

    return (locs,preds) #2-tuple of the face locations and their corresponding locations

# the serialized face detector model from disk
prototxtPath = r"face_detector\deploy.prototxt"
weightsPath = r"face_detector\rES10_300x300_ssd_iter_140000.caffemodel"
detectFace = cv2.dnn.readNet(prototxtPath,weightsPath)

# face mask detector model from disk
detectMask = load_model("mask_det_final_model.h5")


vid_stream = VideoStream(src=0).start()

# loop over the frames from the video stream
while True:

    frame = vid_stream.read()
    frame = imutils.resize(frame,width=600)

    # detect faces in the frame and determine if the detected face is wearing a face mask or not
    (locs,preds) = find_face_and_mask(frame,detectMask)

    for (Box,pred) in zip(locs,preds):
        # unpack the bounding Box and predictions
        (startX,endY) = Box
        (mask,withoutMask) = pred


        label = "Mask" if mask > withoutMask else "No Mask" #labels of the Box
        color = (0,255,0) if label == "Mask" else (0,255)
    
        
        
        
        if (label == "No Mask"):
            # Throw a Warning Message to tell user to wear a mask if not wearing one. This will stay
            #open and No Access will be given He/She wears the mask
            messageBox.showwarning("Warning",'######')
            mail.sendmail('[email protected]',message)
            mail.close
            
        else:
            pass
       
     

        # include the probability in the label
        label = "{}: {:.2f}%".format(label,max(mask,withoutMask) * 100)

        # display the label and bounding Box rectangle on the output frame
        cv2.putText(frame,label,(startX,startY - 10),cv2.FONT_HERShey_SIMPLEX,0.45,color,2)
        cv2.rectangle(frame,startY),(endX,endY),2)

    cv2.imshow("Face mask detector Live webcam",frame)
    key = cv2.waitKey(1) & 0xFF

    if key == ord("q"):
        break

cv2.destroyAllWindows()
vid_stream.stop()

解决方法

如果您的用户名和密码正确,这些是在任何客户端上运行 Gmail 的步骤。

# Visit https://accounts.google.com
#
# 1. Click on [Security] menu
# 2. Scroll to section [Less secure app access] and set it to ON
# 3. Scroll to section [Signing in to Google] and set [Use your phone to sign in] to OFF 
# 4. Scroll to section [Signing in to Google] and set [2-step Verification] to OFF

现在发送一个简单的文本/普通电子邮件示例:

To: "Jane Doe" <[email protected]>
From: "John Doe" <[email protected]>
Subject: A text/plain email
MIME-Version: 1.0 (Created with SublimeText 3)
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit

Good morning.

This is a message in text/plain format.
It contains text/plain.
It contains no base64 inline images or attachments.
Each line is no more than 76 characters in length.

Please reach out if you have any questions.


Thank you,John Doe
SENIOR DEVELOPER
XYZ Solutions Inc.
P | 999.555.1234

然后,您可以使用 CURL 发送它:

# GMAIL TEST - text-plain.eml
curl --verbose -ssl smtps://smtp.gmail.com:465 --login-options AUTH=PLAIN --user [email protected]:passwword123 --mail-from [email protected] --mail-rcpt [email protected] --mail-rcpt-allowfails --upload-file text-plain.eml