Python创建自己的邮箱应用(1)
2017-01-09 13:42
295 查看
在python中使用smtplib进行电子邮件的发送.
发送纯文本邮件
# -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText from email.header import Header class sendEmailController: def __init__(self, mail_host, mail_user, mail_pass): self.mail_host = mail_host self.mail_user = mail_user self.mail_pass = mail_pass def senTextEmail(self, title, sender, receivers, content): # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码 meesage = MIMEText(content, 'plain', 'utf-8') froms = u'%s <%s>' % (sender, self.mail_user) meesage['From'] = froms meesage['To'] = receivers meesage['Subject'] = Header(title, 'utf-8') self.send(self.mail_user, receivers, meesage) def send(self, sender, receivers, message): try: smtpObj = smtplib.SMTP() smtpObj.connect(self.mail_host, 25) smtpObj.set_debuglevel(1) smtpObj.login(self.mail_user, self.mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) smtpObj.quit() print("发送成功") except smtplib.SMTPException: print("发送失败") if __name__ == '__main__': sendEC = sendEmailController('smtp.163.com', '发送人邮箱', '邮箱密码') sendEC.senTextEmail('会议记录', '163邮箱', '接收人邮箱', '把今天的会议记录发我')
发送Html邮件
# -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText from email.header import Header class sendEmailController: def __init__(self, mail_host, mail_user, mail_pass): self.mail_host = mail_host self.mail_user = mail_user self.mail_pass = mail_pass def senTextEmail(self, title, sender, receivers, content): # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码 self.send(self.mail_user, receivers, self.message('plian',title,sender,receivers,content)) def sendHtmlEmail(self, title, sender, receivers, content): self.send(self.mail_user, receivers, self.message('html',title,sender,receivers,content)) def message(self, subtype, title, sender, receivers, content): # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码 meesage = MIMEText(content, subtype, 'utf-8') froms = u'%s <%s>' % (sender, self.mail_user) meesage['From'] = froms meesage['To'] = receivers meesage['Subject'] = Header(title, 'utf-8') return meesage def send(self, sender, receivers, message): try: smtpObj = smtplib.SMTP() smtpObj.connect(self.mail_host, 25) smtpObj.set_debuglevel(1) smtpObj.login(self.mail_user, self.mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) smtpObj.quit() print("发送成功") except smtplib.SMTPException: print("发送失败") if __name__ == '__main__': msg = """ <html><body> <p><a href="http://www.baidu.com">百度链接</a></p> </body></html> """ sendEC = sendEmailController('smtp.163.com','发送人邮箱', '邮箱密码') sendEC.sendHtmlEmail('会议记录', '163邮箱', '接收人邮箱', msg)
相关文章推荐
- Python创建自己的邮箱应用(2)
- 使用Python调用新浪微盘接口,创建自己的云盘应用
- Equinox OSGi系列之 创建自己的OSGi应用项目
- 地磅称量系统之(51)应用自己创建的Icon文件作为位图
- 快速使用 Thinkphp 之二: 创建自己的应用项目
- Yii 框架创建自己的 web 应用
- cPanel如何创建自己个性邮箱账号
- Equinox OSGi系列之 创建自己的OSGi应用项目
- SSIS从理论到实战,再到应用(1)----创建自己的第一个包
- fileupload应用 上传数据自己选择收件人选择上传的附件点发送后会创建一个文件夹存入你上传的附件
- fileupload应用 上传数据自己选择收件人选择上传的附件点发送后会创建一个文件夹存入你上传的附件
- Zigbee学习之创建自己的简单应用
- Equinox OSGi系列之四 创建自己的OSGi应用项目
- Weblogic 8.1的配置-创建自己的应用目录
- 如何把企业邮箱的登陆框应用的自己网站的主页
- fileupload应用 上传数据自己选择收件人选择上传的附件点发送后会创建一个文件夹存入你上传的附件
- Equinox OSGi系列之 创建自己的OSGi应用项目
- 企业库应用实践系列一:创建第一个自己的Application Block _转http://www.cnblogs.com/lvwuhuijun/articles/1366094.html
- Equinox OSGi系列之四 创建自己的OSGi应用项目
- python进阶ing——创建第一个Tornado应用