一. 邮件的发送
这里使用邮箱MyTest22@163.com发送数据到cay22@163.com和cay33@163.com
发送邮件主要用到了smtplib和email两个模块
1. 简单文本的发送
普通文本邮件发送的实现, 关键是要将MIMEText中_subtype设置为plain
#!/usr/bin/env python
# coding=utf-8
# Python 2.7.3
# 简答发送文本内容
import smtplib
from email.mime.text import MIMEText
def SendEMail(sendToList, sendTitle, sendContent):
mailHost = "smtp.163.com" # 服务器
mailUser = "MyTest22" # 用户名
mailPassword = "xxxxxxx" # 用户密码
mailPostfix = "163.com"
# 注意这里格式
me = mailUser + "<" + mailUser + "@" + mailPostfix + ">"
msg = MIMEText(sendContent)
# msg = MIMEText(content, _subtype = 'plain', _charset = 'gb2312')
msg['Subject'] = sendTitle
msg['From'] = me
msg['To'] = ";".join(sendToList)
try:
s = smtplib.SMTP()
s.connect(mailHost)
s.login(mailUser, mailPassword)
s.sendmail(me, sendToList, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
# sendTo列表
sendToList = ["cay22@163.com","cay33@163.com"]
# 标题
sendTitle = "Title subject"
# 邮件内容
sendContent = "How are you."
if __name__ == '__main__':
if SendEMail(sendToList, sendTitle, sendContent):
print "Send Success."
else:
print "Send Failed."
2. html邮件的发送
发送html邮件, 将MIMEText中_subtype设置为html. 如果不设置html, 则显示时就原原本本的显示
#!/usr/bin/env python
# coding=utf-8
# Python 2.7.3
# 简答发送文本内容
import smtplib
from email.mime.text import MIMEText
def SendEMail(sendToList, sendTitle, sendContent):
mailHost = "smtp.163.com" # 服务器
mailUser = "MyTest22" # 用户名
mailPassword = "xxxxxxxxxxxx" # 用户密码
mailPostfix = "163.com"
# 注意这里格式
me = mailUser + "<" + mailUser + "@" + mailPostfix + ">"
# msg = MIMEText(sendContent)
msg = MIMEText(sendContent, _subtype = 'html', _charset = 'gb2312')
msg['Subject'] = sendTitle
msg['From'] = me
msg['To'] = ";".join(sendToList)
try:
s = smtplib.SMTP()
s.connect(mailHost)
s.login(mailUser, mailPassword)
s.sendmail(me, sendToList, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
# sendTo列表
sendToList = ["cay22@163.com","cay33@163.com"]
# 标题
sendTitle = "Title subject"
# 邮件内容
sendContent = "<a href='http://blog.csdn.net/bagboy_taobao_com'>How are you.</a>"
if __name__ == '__main__':
if SendEMail(sendToList, sendTitle, sendContent):
print "Send Success."
else:
print "Send Failed."
3. 有附件的邮件发送
#!/usr/bin/env python
# coding=utf-8
# Python 2.7.3
# 发送附件和HTML文本内容
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def SendEMail(sendToList, sendTitle, sendContent, fileList):
mailHost = "smtp.163.com" # 服务器
mailUser = "MyTest22" # 用户名
mailPassword = "xxxxxxxxxx" # 用户密码
mailPostfix = "163.com"
# 注意这里格式
me = mailUser + "<" + mailUser + "@" + mailPostfix + ">"
msg = MIMEMultipart(_subtype = 'html')
msg['Subject'] = sendTitle
msg['From'] = me
msg['To'] = ";".join(sendToList)
# 构造附件1
att1 = MIMEText(open(fileList[0], 'rb').read(), 'base64', 'gb2312')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="F1.txt"' # F1.txt是在收件邮箱中显示用的, 可以任意命名
msg.attach(att1)
# 构造附件2
att2 = MIMEText(open(fileList[1], 'rb').read(), 'base64', 'gb2312')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="123.txt"'
msg.attach(att2)
# 邮件内容
att3 = MIMEText(sendContent, _subtype = 'html', _charset = 'gb2312')
msg.attach(att3)
try:
s = smtplib.SMTP()
s.connect(mailHost)
s.login(mailUser, mailPassword)
s.sendmail(me, sendToList, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
# sendTo列表
sendToList = ["cay22@163.com","cay33@163.com"]
# 标题
sendTitle = "Title subject"
# 邮件内容
sendContent = "<a href='http://blog.csdn.net/bagboy_taobao_com'>How are you.</a>"
# 附件列表
fileList = ["File1.txt", "File2.txt"]
if __name__ == '__main__':
if SendEMail(sendToList, sendTitle, sendContent, fileList):
print "Send Success."
else:
print "Send Failed."
4. 发送图片
可以把图片文件理解为一个文件, 同上发送附件一样. 预览个鬼啊
#!/usr/bin/env python
# coding=utf-8
# Python 2.7.3
# 发送附件和HTML文本内容, 图片
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
def SendEMail(sendToList, sendTitle, sendContent, fileList):
mailHost = "smtp.163.com" # 服务器
mailUser = "MyTest22" # 用户名
mailPassword = "xxxxxxxxx" # 用户密码
mailPostfix = "163.com"
# 注意这里格式
me = mailUser + "<" + mailUser + "@" + mailPostfix + ">"
msg = MIMEMultipart(_subtype = 'html')
msg['Subject'] = sendTitle
msg['From'] = me
msg['To'] = ";".join(sendToList)
# 构造附件1
att1 = MIMEText(open(fileList[0], 'rb').read(), 'base64', 'gb2312')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="F1.txt"' # F1.txt是在收件邮箱中显示用的, 可以任意命名
msg.attach(att1)
# 构造附件2
att2 = MIMEText(open(fileList[1], 'rb').read(), 'base64', 'gb2312')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="123.txt"'
msg.attach(att2)
# 邮件内容
att3 = MIMEText(sendContent, _subtype = 'html', _charset = 'gb2312')
msg.attach(att3)
# 增加一张图片
image = MIMEImage(open("1.jpg",'rb').read())
image.add_header('Content-ID','<image1>')
image["Content-Disposition"] = 'attachment; filename="1.jpg"'
msg.attach(image)
try:
s = smtplib.SMTP()
s.connect(mailHost)
s.login(mailUser, mailPassword)
s.sendmail(me, sendToList, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
# sendTo列表
sendToList = ["cay22@163.com","cay33@163.com"]
# 标题
sendTitle = "Title subject"
# 邮件内容
sendContent = "<a href='http://blog.csdn.net/bagboy_taobao_com'>How are you.</a>"
# 附件列表
fileList = ["File1.txt", "File2.txt"]
if __name__ == '__main__':
if SendEMail(sendToList, sendTitle, sendContent, fileList):
print "Send Success."
else:
print "Send Failed."
转自: http://www.cnblogs.com/xiaowuyi/archive/2012/03/17/2404015.html