新人用python实现smtp协议发邮件,出现了点问题,已经困扰本人两天,求大佬帮帮忙

xuzheng00 2017-12-30 10:19:34
问题在于下面两条虚线之间的身份验证,我换过163、sina等等邮箱都验证不通过,不是没有开服务的原因,我每个邮箱都开启了smtp服务。我觉得是命令格式的问题,但是我改了很多遍都没解决。。哪位大佬给看下吧。。。跪了。。
from socket import *           
import base64
import getpass
import codecs

msg = "\r\n I Love Computer Networks!" #Declarations
endmsg="\r\n.\r\n"
mailfrom = "MAIL FROM:<18390242095@163.com>\r\n"
rcptto="RCPT TO:<754845950@qq.com>\r\n"
data = "DATA\r\n"
quitmsg="QUIT\r\n"

#Choose a mail server (e.g. Google mail server) and call it mailserver
mailserver="smtp.163.com" #Use the local Host as SMTP Server
mailPort=25 #Specify Port For Local SMTP
connectaddress=(mailserver,mailPort)

#Create socket called clientsocket and establish a TCP connection with mailserver
clientSocket=socket(AF_INET,SOCK_STREAM) #Assign IP address and port number
clientSocket.connect(connectaddress) #Connect to Local SMTP
recv=clientSocket.recv(1024)
print(recv)
if recv[:3].decode()!='220': #Print Error Clause
print("220 reply not received from server")

#Send HELO command and print server response.
heloCommand="HELO Alice\r\n"
clientSocket.send(bytes(heloCommand.encode()))
recv1=clientSocket.recv(1024)
print(recv1.decode())
if recv1.decode()[:3]!="250":
print("250 reply not received from server.")

print('-----------------------------------')

loginini = b'AUTH LOGIN\r\n'
clientSocket.send(loginini)
recv2 = clientSocket.recv(1024).decode('utf-8')
print('222+ ',recv2)

userCommand = base64.b64encode(b'18390242095@163.com\r\n')#在这里发送用户名,必须是qq邮箱,然后base64,b64encode(b'username'),转换成base64编码,*****指的是转成base64格式后的用户名
clientSocket.send(userCommand)
recv3 = clientSocket.recv(1024).decode('utf-8')
print('333+ ', recv3)

passwordCommand = base64.b64encode(b'xudoudou3661615\r\n')#在这里发送密码,必须是qq邮箱授权码,需要开启邮箱smtp服务后才能发短信获取,具体方法参见qq邮箱账户设置,然后base64,b64encode(b'password'),转换成base64编码,*****指的是转成base64格式后的授权码
clientSocket.send(passwordCommand)
recv4 = clientSocket.recv(1024).decode('utf-8')
print('444+ ', recv4)

print('-----------------------------------')
#Send MAIL FROM command and print server response.
clientSocket.send(bytes(mailfrom.encode())) #send mail, had to convert to bytes
check=clientSocket.recv(1024)
print(check) #print confirmation of working messages

#Send RCPT TO command and print server response.
clientSocket.send(bytes(rcptto.encode())) #recieve, had to convert to bytes
check1=clientSocket.recv(1024)
print(check1) #print confirmation of working messages

#Send DATA command and print server response.
clientSocket.send(bytes(data.encode())) #DATA, had to convert to bytes
check2=clientSocket.recv(1024)
print(check2) #print confirmation of working messages

#Message ends with a single period.
clientSocket.send(bytes((mailfrom+msg+endmsg).encode())) #Concatinate bits for message
check3=clientSocket.recv(1024)
print(check3) #print confirmation of working messages

#Send QUIT command and get server response.
clientSocket.send(bytes(quitmsg.encode())) #Quit the session
check4=clientSocket.recv(1024)
print(check4) #print confirmation of working messages
...全文
3543 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
陈年椰子 2018-01-03
  • 打赏
  • 举报
回复
换下思路,试试 smtplib 。
AlbertS 2018-01-03
  • 打赏
  • 举报
回复
可以试试smtplib,很方便的

from email.mime.text import MIMEText
import smtplib

def send_text_mail(sender, password, receiver_list, subject = '', content = '', smtp_center= 'smtp.163.com'):

    # set message of needing to send
    msg = MIMEText(content, 'plain', 'utf-8')
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = ';'.join(receiver_list)

    # send email
    server = smtplib.SMTP(smtp_center, 25)
    server.set_debuglevel(1)
    server.login(sender, password)
    server.sendmail(sender, receiver_list, msg.as_string())
    server.quit()

def test_send_text_mail():

    from_addr = input('From:')
    password = input('Password:')
    to_adrr = '347070901@qq.com' #input('To:')

    subject = 'Python Email'
    content = 'This is a test email that created by python and sended by Albert!'

    # send email
    send_text_mail(from_addr, password, [to_adrr], subject, content)


if __name__ == '__main__':
    test_send_text_mail()
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧