码迷,mamicode.com
首页 > 系统相关 > 详细

Shell第三篇:基本语法

时间:2017-03-22 22:42:41      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:技术   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 !‘)
python邮件发送工具(smtplib)
#!/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 哈哈哈

 

 

 

 

 技术分享

技术分享

 

 

技术分享

 

Shell第三篇:基本语法

标签:技术   str   log   pass   smtp   pwd   quit   class   error:   

原文地址:http://www.cnblogs.com/linhaifeng/p/6602149.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!