#!/usr/bin/python
#-*- coding:utf-8 -*-
'''
Created on 2016年6月25日
@author: Administrator
文件名: smtp.py
'''
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
'''
Python高级编程---SMTP简单邮件传输协议
#http://www.runoob.com/python/python-email.html
'''
import smtplib
#运行该方法需本机安装sendmail访问
def sendmail1():
sender = '2gzhame@163.com'
receivers = ['2286268956@qq.com'] #接收邮件
#三个参数: 第一个是文本内容, 第二个plain 设置文本格式, 第三个 utf-8 设置编码
message = MIMEText('Python 邮件发送测试.....', 'plain', 'utf-8')
message['From'] = Header('菜鸟教程', 'utf-8')
message['to'] = Header('测试', 'utf-8')
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender,receivers,message.as_string())
print "邮件发送成功"
except smtplib.SMTPException:
print "Error:无法发送邮件"
-------2016-06-26 01:10
@modaokeng.yongtaixincun.baiyunqu.guangzhoushi.guangdongsheng
#使用第三方的SMTP服务
def sendmail2():
#第三方SMTP 服务
mail_host = "smtp.163.com"
mail_user = "2gzhame@163.com"
mail_pass = "***********"
sender = 'gzhame@163.com'
receivers = ['2286268956@qq.com'] #接收邮件
#三个参数: 第一个是文本内容, 第二个plain 设置文本格式, 第三个 utf-8 设置编码
message = MIMEText('Python 邮件发送测试.....', 'plain', 'utf-8')
message['From'] = Header('菜鸟教程', 'utf-8') #设置发件人
message['to'] = Header('测试', 'utf-8') #设置收件人
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8') #设置邮件标题
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25为 SMTP端口号
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender,receivers,message.as_string())
print "邮件发送成功"
except smtplib.SMTPException:
print "Error:无法发送邮件"
#发送带附件的邮件
def senmail3():
#发送带附件的邮件,首先要创建MIMEMultipart()实例,然后构造附件,如果有多个附件,可依次构造,最后利用smtplib.smtp发送。
#第三方SMTP 服务
mail_host = "smtp.163.com"
mail_user = "gzhame@163.com"
mail_pass = "********"
sender = 'gzhame@163.com'
receivers = ['286268956@qq.com'] #接收邮件
#三个参数: 第一个是文本内容, 第二个plain 设置文本格式, 第三个 utf-8 设置编码
message = MIMEMultipart()
message.attach(MIMEText('Python 邮件发送附件.....', 'plain', 'utf-8'))
message['From'] = Header('听雨轩 ', 'utf-8') #设置发件人
message['to'] = Header('楓林左岸', 'utf-8') #设置收件人
subject = 'Python SMTP 发送附件'
message['Subject'] = Header(subject, 'utf-8') #设置邮件标题
#构造附件
att2 = MIMEText(open(r'C:\Users\Administrator\Desktop\test.txt','rb').read(),'base64','utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment;filename="test.txt"'
message.attach(att2)
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25为 SMTP端口号
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender,receivers,message.as_string())
print "邮件发送成功"
except smtplib.SMTPException, e:
print "Error:无法发送邮件"
print e
'''
Error:无法发送邮件
(554, 'DT:SPM 163 smtp12,EMCowABHf6QYuG5XakRCAA--.46705S2 1466873880,please see http://mail.163.com/help/help_spam_16.htm?ip=14.151.131.125&hostid=smtp12&time=1466873880')
554错误码的定义为:554 DT:SPM 发送的邮件内容包含了未被许可的信息,或被系统识别为垃圾邮件。请检查是否有用户发送病毒或者垃圾邮件;
'''
if __name__ == '__main__':
senmail3()
#-*- coding:utf-8 -*-
'''
Created on 2016年6月25日
@author: Administrator
文件名: smtp.py
'''
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
'''
Python高级编程---SMTP简单邮件传输协议
#http://www.runoob.com/python/python-email.html
'''
import smtplib
#运行该方法需本机安装sendmail访问
def sendmail1():
sender = '2gzhame@163.com'
receivers = ['2286268956@qq.com'] #接收邮件
#三个参数: 第一个是文本内容, 第二个plain 设置文本格式, 第三个 utf-8 设置编码
message = MIMEText('Python 邮件发送测试.....', 'plain', 'utf-8')
message['From'] = Header('菜鸟教程', 'utf-8')
message['to'] = Header('测试', 'utf-8')
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender,receivers,message.as_string())
print "邮件发送成功"
except smtplib.SMTPException:
print "Error:无法发送邮件"
-------2016-06-26 01:10
@modaokeng.yongtaixincun.baiyunqu.guangzhoushi.guangdongsheng
#使用第三方的SMTP服务
def sendmail2():
#第三方SMTP 服务
mail_host = "smtp.163.com"
mail_user = "2gzhame@163.com"
mail_pass = "***********"
sender = 'gzhame@163.com'
receivers = ['2286268956@qq.com'] #接收邮件
#三个参数: 第一个是文本内容, 第二个plain 设置文本格式, 第三个 utf-8 设置编码
message = MIMEText('Python 邮件发送测试.....', 'plain', 'utf-8')
message['From'] = Header('菜鸟教程', 'utf-8') #设置发件人
message['to'] = Header('测试', 'utf-8') #设置收件人
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8') #设置邮件标题
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25为 SMTP端口号
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender,receivers,message.as_string())
print "邮件发送成功"
except smtplib.SMTPException:
print "Error:无法发送邮件"
#发送带附件的邮件
def senmail3():
#发送带附件的邮件,首先要创建MIMEMultipart()实例,然后构造附件,如果有多个附件,可依次构造,最后利用smtplib.smtp发送。
#第三方SMTP 服务
mail_host = "smtp.163.com"
mail_user = "gzhame@163.com"
mail_pass = "********"
sender = 'gzhame@163.com'
receivers = ['286268956@qq.com'] #接收邮件
#三个参数: 第一个是文本内容, 第二个plain 设置文本格式, 第三个 utf-8 设置编码
message = MIMEMultipart()
message.attach(MIMEText('Python 邮件发送附件.....', 'plain', 'utf-8'))
message['From'] = Header('听雨轩 ', 'utf-8') #设置发件人
message['to'] = Header('楓林左岸', 'utf-8') #设置收件人
subject = 'Python SMTP 发送附件'
message['Subject'] = Header(subject, 'utf-8') #设置邮件标题
#构造附件
att2 = MIMEText(open(r'C:\Users\Administrator\Desktop\test.txt','rb').read(),'base64','utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment;filename="test.txt"'
message.attach(att2)
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25为 SMTP端口号
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender,receivers,message.as_string())
print "邮件发送成功"
except smtplib.SMTPException, e:
print "Error:无法发送邮件"
print e
'''
Error:无法发送邮件
(554, 'DT:SPM 163 smtp12,EMCowABHf6QYuG5XakRCAA--.46705S2 1466873880,please see http://mail.163.com/help/help_spam_16.htm?ip=14.151.131.125&hostid=smtp12&time=1466873880')
554错误码的定义为:554 DT:SPM 发送的邮件内容包含了未被许可的信息,或被系统识别为垃圾邮件。请检查是否有用户发送病毒或者垃圾邮件;
'''
if __name__ == '__main__':
senmail3()