标签:技术 str log pass smtp pwd quit class error:
补充:发送邮件
发件箱:python4_mail@163.com alex3714 自定义客户端登录帐号和密码分别为:python4_mail@163.com sbalex3714
收件箱:python4_recvmail@163.com alex371
python通过SMTP发送邮件失败:
错误1:smtplib.SMTPAuthenticationError: (550, b‘User has no permission‘)
我们使用python发送邮件时相当于自定义客户端根据用户名和密码登录,然后使用SMTP服务发送邮件,新注册的163邮箱是默认不开启客户端授权的(对指定的邮箱大师客户端默认开启),因此登录总是被拒绝,解决办法(以163邮箱为例):进入163邮箱-设置-客户端授权密码-开启(授权码是用于登录第三方邮件客户端的专用密码)
错误2:smtplib.SMTPAuthenticationError: (535, b‘Error: authentication failed‘)
以163邮箱为例,在开启POP3/SMTP服务,并开启客户端授权密码时会设置授权码,将这个授权码代替smtplib.SMTP().login(user,password)方法中的password即可。
#!/usr/bin/python # -*- coding: UTF-8 -*- import smtplib import email.mime.multipart import email.mime.text msg = email.mime.multipart.MIMEMultipart() msg[‘Subject‘] = ‘你是风儿我是沙,缠缠绵绵回我家‘ msg[‘From‘] = ‘python4_mail@163.com‘ msg[‘To‘] = ‘python4_recvmail@163.com‘ content = ‘‘‘ 来来来,一起摇摆 ‘‘‘ txt = email.mime.text.MIMEText(content,_charset=‘utf-8‘) msg.attach(txt) smtp = smtplib.SMTP() smtp.connect(‘smtp.163.com‘, ‘25‘) smtp.login(‘python4_mail‘, ‘sbalex3714‘) smtp.sendmail(‘python4_mail@163.com‘, ‘python4_recvmail@163.com‘, msg.as_string()) smtp.quit() print(‘邮件发送成功email has send out !‘)
#!/usr/bin/python # -*- coding: UTF-8 -*- import sys import smtplib import email.mime.multipart import email.mime.text server = ‘smtp.163.com‘ port = ‘25‘ def sendmail(server,port,user,pwd,msg): smtp = smtplib.SMTP() smtp.connect(server,port) smtp.login(user, pwd) smtp.sendmail(msg[‘from‘], msg[‘to‘], msg.as_string()) smtp.quit() print(‘邮件发送成功email has send out !‘) if __name__ == ‘__main__‘: msg = email.mime.multipart.MIMEMultipart() msg[‘Subject‘] = ‘你是风儿我是沙,缠缠绵绵回我家‘ msg[‘From‘] = ‘python4_mail@163.com‘ msg[‘To‘] = ‘python4_recvmail@163.com‘ user = ‘python4_mail‘ pwd = ‘sbalex3714‘ content=sys.argv[-1] txt = email.mime.text.MIMEText(content, _charset=‘utf-8‘) msg.attach(txt) sendmail(server,port,user,pwd,msg) #使用方法:python 上述内容的文件名.py 你好啊 egon 哈哈哈
标签:技术 str log pass smtp pwd quit class error:
原文地址:http://www.cnblogs.com/linhaifeng/p/6602149.html