Python 通过解码登录发送电子邮件

Python 通过解码登录发送电子邮件,python,python-2.7,email,gmail,Python,Python 2.7,Email,Gmail,我有两个脚本,第一个脚本被设计成在两个分开的文本文件中写下一个电子邮件地址和一个密码(01.txt和02.txt)。使用一个简单的Caesar算法来隐藏它们。 在第二个脚本中,我想解开邮件地址和密码的编码,然后发送一封邮件。 这很奇怪,因为当我这样做的时候,我打印了正确的登录和密码,但它不会发送任何东西,引发了一个错误:SMTPAuthenticationError,这不是真的,因为通过我的打印,我看到我输入了正确的信息,这些信息被正确解码。你有什么线索吗 mail\u begin.py: de

我有两个脚本,第一个脚本被设计成在两个分开的文本文件中写下一个电子邮件地址和一个密码(01.txt和02.txt)。使用一个简单的Caesar算法来隐藏它们。 在第二个脚本中,我想解开邮件地址和密码的编码,然后发送一封邮件。 这很奇怪,因为当我这样做的时候,我打印了正确的登录和密码,但它不会发送任何东西,引发了一个错误:SMTPAuthenticationError,这不是真的,因为通过我的打印,我看到我输入了正确的信息,这些信息被正确解码。你有什么线索吗

mail\u begin.py:

def user():
    log = open("01.txt", "w")
    from_add = raw_input("Your mail address is : ")

    from_add = from_add.decode("utf-8")

    key = 3


    lettres = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    lettres = lettres.decode("utf-8")

    crypted = ""


    for car in from_add:

        if car in lettres:

            num = lettres.find(car)

            num += key

            if num >= len(lettres):
                num = num - len(lettres)

            crypted += lettres[num]

        else:
            crypted += car

    log.write(crypted)
    log.close()

def password():

    log2 = open("02.txt", "w")
    passw2 = raw_input("Enter your password to log-in:")

    passw2 = passw2.decode("utf-8")

    key = 3


    lettres = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    lettres = lettres.decode("utf-8")

    crypted = ""


    for car in passw2:

        if car in lettres:

            num = lettres.find(car)

            num += key

            if num >= len(lettres):
                num = num - len(lettres)

            crypted += lettres[num]

        else:
            crypted += car

    log2.write(crypted)
    log2.close()

user()
password()
邮件\u已完成。py

import smtplib
import os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

msg = MIMEMultipart()
name = open("01.txt",'r')
passw = open("02.txt",'r')
f_l1 = str(name.readlines())
f_l2 = str(passw.readlines())

print (f_l1)
print (f_l2)

f_l1 = f_l1.decode("utf-8")
f_l2 = f_l2.decode("utf-8")
key = 3


lettres = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
lettres = lettres.decode("utf-8")


decrypted = ""
decrypted2 =""

for car in f_l1:
    if car in lettres:
        num = lettres.find(car)

        num -= key

        if num < 0 :
            num = num + len(lettres)

        decrypted += lettres[num]
    else:
        decrypted += car

for car in f_l2:
    if car in lettres:
        num = lettres.find(car)

        num -= key

        if num < 0 :
            num = num + len(lettres)

        decrypted2 += lettres[num]
    else:
        decrypted2 += car

msg['From'] = str(decrypted)
msg['To'] = str(decrypted)

body = "Time to go back to the lab ! Your scan is over"
msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
print decrypted # e-mail clear
print decrypted2 # password clear
server.login(decrypted, decrypted2) # not working, bad authentification...
text = msg.as_string()
server.sendmail(str(decrypted),str(decrypted), text)
server.quit()

name.close()
passw.close()

os.remove('01.txt')
os.remove('02.txt')
导入smtplib
导入操作系统
从email.MIMEMultipart导入MIMEMultipart
从email.MIMEText导入MIMEText
msg=MIMEMultipart()
name=open(“01.txt”,“r”)
passw=open(“02.txt”,“r”)
f_l1=str(name.readlines())
f_l2=str(passw.readlines())
打印(f_l1)
打印(f_l2)
f_l1=f_l1.解码(“utf-8”)
f_l2=f_l2.解码(“utf-8”)
键=3
lettres=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz”
lettres=lettres.解码(“utf-8”)
解密=“”
decrypted2=“”
对于f_l1中的车辆:
如果车辆处于出租状态:
num=lettres.find(汽车)
num-=键
如果num<0:
num=num+len(lettres)
已解密+=lettres[num]
其他:
解密+=汽车
对于f_l2中的车辆:
如果车辆处于出租状态:
num=lettres.find(汽车)
num-=键
如果num<0:
num=num+len(lettres)
decrypted2+=lettres[num]
其他:
解密的2+=汽车
msg['From']=str(已解密)
msg['To']=str(已解密)
body=“该回实验室了!你的扫描结束了”
msg.attach(MIMEText(正文“普通”))
server=smtplib.SMTP('SMTP.gmail.com',587)
server.starttls()
打印解密#电子邮件清除
打印解密2#密码清除
server.login(解密,解密2)#不工作,身份验证错误。。。
text=msg.as_string()
sendmail(str(已解密)、str(已解密)、text)
server.quit()
name.close()
passw.close()
删除操作系统('01.txt')
删除操作系统('02.txt')
当一切都完成后,它删除了txt文件,因此没有人可以解码它们。 你能在家里试试吗,看看问题出在哪里? 谢谢

readlines()
通过调用列表上的
str()
返回列表,您将生成列表的字符串表示形式

str(['mylist']) 

相反,您应该使用
readline
而不是
readlines
,只读取所需的行-它已经是一个字符串,无需一直调用
str()

with open("01.txt") as f:
    name = f.readline().strip()

with open("02.txt") as f:
    password = f.readline().strip()

实际上,如果我只是修改:

f_l1 = (name.readlines())
f_l2 = (passw.readlines())

它正在工作。谢谢你的回答nosklo

f_l1 = (name.readlines())
f_l2 = (passw.readlines())
f_l1 = (name.readline())
f_l2 = (passw.readline())