在python中发送电子邮件时出错 [英] Error sending email in python

查看:96
本文介绍了在python中发送电子邮件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中开发一个发送电子邮件的应用程序。当登录凭据作为变量传递时,:server.login(fromadd,password)我收到用户名和密码不被接受的错误。我用过gmail smtp服务器。但是当我传递凭证时:server.login('xxxx','xxxx')其中(xxxx =电子邮件和密码)邮件已发送。我已将debuglevel设置为true .Below是代码和错误。

------------------------------------------- ---------------------------------

I a developing application in python that send email. When login credentials are passed as variables such that: "server.login(fromadd,password)" I get error that username and password not accepted. I have used gmail smtp server. But when I pass credentials such that: "server.login('xxxx','xxxx')" where (xxxx=email and password) the mail is sent.I have set debuglevel to true .Below is the code and error.
----------------------------------------------------------------------------

from tkinter import *
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.mime.base import MIMEBase
import smtplib
import os
import sqlite3
import MainWindow    

filelist=[]
password=""



def set_credentials(fromadd):
    con=sqlite3.connect("EmailDB.db")
    cursor=con.execute("SELECT PASSWORD FROM REGISTRATION WHERE EMAIL='%s'"%(fromadd))
    for row in cursor:
        password=row[0]
    print(password)
    
        
        
    
def getFile():
    file_path=filedialog.askopenfilename()
    MailWindow.Attch.set(file_path)
    filelist.append(file_path)
    
def getFile():
    file_path=filedialog.askopenfilename()
    Attch.set(file_path)
    filelist.append(file_path)

def mail_log():
    con=sqlite3.connect("EmailDB.db")
    cursor=con.execute("INSERT INTO MAILBOX (TOADD,FROMADD,SUBJECT,BODY)VALUES(?,?,?,?)",(From.get(),To.get(),Subject.get(),txtBody.get("1.0",'end-1c')))
    con.commit()
    
def send():
    
    domain=str(From.get())
    server=smtplib.SMTP('smtp.gmail.com',587)

   
    fromadd=str(From.get())
    toadd=To.get()
    subject=Subject.get()
    set_credentials(fromadd)
    
    msg=MIMEMultipart()
    msg['From']=fromadd
    msg['To']=toadd
    msg['Subject']=subject
    body=txtBody.get("1.0",'end-1c')
    msg.attach(MIMEText(body,'plain'))
  
    if filelist is not None:
        for file in filelist:
            part=MIMEBase('application','octet-stream')
            part.set_payload(open(file,"rb").read())
            encoders.encode_base64(part)
            part.add_header('Content-Disposition',"attachment; filename=%s"%os.path.basename (file))
            msg.attach(part)

# Send Email Code

    try:
        server.starttls()
        server.set_debuglevel(True)
        server.login(fromadd,password)
        text=msg.as_string()
        server.sendmail(fromadd,toadd.split(","),text)
        messagebox.showinfo('Mail Sent')
    except:
        messagebox.showerror('Error','Cannot send mail')
    finally:
        server.quit()
        mail_log()


m=Tk()
m.geometry("800x550+300+100")


To=StringVar()
From=StringVar()
Cc=StringVar()
Subject=StringVar()
Attch=StringVar()

lblFrom=Label(m,text="From:",bg='#FFC9A5').place(x=20,y=60)
txtFrom=Entry(m,width=80,textvariable=From).place(x=90,y=60)


lblTo=Label(m,text="To:",bg='#FFC9A5').place(x=20,y=90)
txtTo=Entry(m,width=80,textvariable=To).place(x=90,y=90)

lblCc=Label(m,text="Cc/Bcc:",bg='#FFC9A5').place(x=20,y=120)
txtCc=Entry(m,width=80,textvariable=Cc).place(x=90,y=120)

lblSubject=Label(m,text="Subject:",bg='#FFC9A5').place(x=20,y=150)
txtSubject=Entry(m,width=80,textvariable=Subject).place(x=90,y=150)

lblAttch=Label(m,text="Attactment:",bg='#FFC9A5').place(x=20,y=180)
txtAttch=Entry(m,width=80,textvariable=Attch).place(x=90,y=180)
btnAttch=Button(m,text="Attach",fg="dark blue",width=10,command=getFile).place(x=580,y=175)

lblBody=Label(m,text="Body:",bg='#FFC9A5').place(x=20,y=210)
txtBody=Text(m,width=60,height=15)
txtBody.place(x=90,y=210)

btnSend=Button(m,text="Send",fg="dark green",width=10,command=send).place(x=90,y=480)
m.configure(background='#FFC9A5')
m.mainloop()



----------------------- --------------------------------------------------

错误:

-------------------------------- ------------------------------------------


-------------------------------------------------------------------------
Error:
--------------------------------------------------------------------------

send: 'ehlo [192.168.1.15]\r\n'
reply: b'250-smtp.gmail.com at your service, [43.247.161.176]\r\n'
reply: b'250-SIZE 35882577\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH\r\n'
reply: b'250-ENHANCEDSTATUSCODES\r\n'
reply: b'250-PIPELINING\r\n'
reply: b'250-CHUNKING\r\n'
reply: b'250 SMTPUTF8\r\n'
reply: retcode (250); Msg: b'smtp.gmail.com at your service, [43.247.161.176]\nSIZE 35882577\n8BITMIME\nAUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8'
send: 'AUTH PLAIN AGFrYXNocGF0ZWw5MDBAZ21haWwuY29tAA==\r\n'
reply: b'535-5.7.8 Username and Password not accepted. Learn more at\r\n'
reply: b'535 5.7.8  https://support.google.com/mail/answer/14257 ir4sm26338302pbb.93 - gsmtp\r\n'
reply: retcode (535); Msg: b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/answer/14257 ir4sm26338302pbb.93 - gsmtp'
send: 'YWthc2hwYXRlbDkwMEBnbWFpbC5jb20=\r\n'
reply: b'502 5.5.1 Unrecognized command. ir4sm26338302pbb.93 - gsmtp\r\n'
reply: retcode (502); Msg: b'5.5.1 Unrecognized command. ir4sm26338302pbb.93 - gsmtp'
send: 'quit\r\n'
reply: b'221 2.0.0 closing connection ir4sm26338302pbb.93 - gsmtp\r\n'
reply: retcode (221); Msg: b'2.0.0 closing connection ir4sm26338302pbb.93 - gsmtp'

推荐答案

您的客户端发送通过base64编码的邮件地址和密码。

解码字符串会产生您的Gmail地址,但不会输入密码。因此,当将密码变量传递给 login 函数时,它似乎是空的。



您可以在登录前在代码中查看此内容。



另请注意,发布此类内容时请务必小心。如果密码将包含在字符串中,则每个人都可以检索它,
Your client sends mail address and password passed base64 encoded.
Decoding the string results in your Gmail address but no password. So it seems that your password variable is empty when passing it to the login function.

You may check this in your code just before login.

Note also that you should always be careful when posting such content. If the password would be contained in the string, everybody would be able to retrieve it,


AUTH PLAIN AGFrYXNocGF0ZWw5MDBAZ21haWwuY29tAA == \\\\ n应该是两个单独的消息 - 首先发送AUTH PLAIN \\ n,获得回复,然后在base64中发送帐户名
"AUTH PLAIN AGFrYXNocGF0ZWw5MDBAZ21haWwuY29tAA==\r\n" should be two separate messages- first send AUTH PLAIN\r\n, get a response, then send account name in base64


这篇关于在python中发送电子邮件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆