使用Python发送邮件

一. 邮件的发送


这里使用邮箱MyTest22@163.com发送数据到cay22@163.comcay33@163.com

发送邮件主要用到了smtplibemail两个模块


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值