当前访客身份:游客 [ 登录 | 加入 OSCHINA ]

代码分享

当前位置:
代码分享 » Python  » 网络编程
分享到: 
收藏 +0
2
若发送到139邮箱可通知手机,自动给手机发送短信
标签: <无>

代码片段(1) [全屏查看所有代码]

1. [代码]简易发送邮件     跳至 [1] [全屏预览]

import smtplib
#easy send email, only can send plain text

from email.mime.text import MIMEText
from smtplib import SMTPHeloError, SMTPAuthenticationError, SMTPHeloError, SMTPSenderRefused

#easy send email, only can send plain text
def sendmail(target, subject, content) :
	msg  = MIMEText(content)
	from_addr = "orighost@qq.com"
	to_addr   = target

	msg['Subject'] = subject
	msg['From'] = from_addr
	msg['To'] = to_addr

	try:
		s = smtplib.SMTP('smtp.qq.com')
		s.login('orighost', '××××××') 
		s.sendmail(from_addr, to_addr, msg.as_string())
	except SMTPHeloError:
		print 'Error: Can not connect server.'
		return 1
	except SMTPAuthenticationError:
		print 'Error: Username or password is not correct.'
		return 1
	except SMTPHeloError:
		print 'Error: Can not deliver to target host.'
		return 1
	except SMTPSenderRefused:
		print 'Error: The target server don\'t accept you from_addr'
		return 1
	except:
		print "Send failed"
		return 1
		pass
	finally:
		s.quit()

	print "Send success."
	return 0

if __name__ == "__main__":
	sendmail("orighost@qq.com", "hello", "welcome to use the email.")

	


开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

发表评论 回到顶部 网友评论(1)

  • 1楼:hehe小白 发表于 2013-10-26 20:02 回复此评论
    不错,学习了一下。
开源从代码分享开始 分享代码
pengyouya123的其它代码 全部(6)...