python简单的发送邮件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python简单的发送邮件相关的知识,希望对你有一定的参考价值。
python 利用smtplib来发送邮件,具体的代码如下
一、 编辑smtp_v2.py
vim /home/python/smtp_v2.py
#!/usr/bin/env python # -*- encoding:utf-8 -*- ‘‘‘欢迎使用python smtp‘‘‘ import smtplib from email.mime.text import MIMEText from email.utils import formataddr from email.header import Header smtp_host = ‘smtp.sina.com‘ smtp_port = 25 mail_user = ‘test@sina.com‘ mail_pwd = ‘这里输入发送邮箱的登录密码‘ sender = ‘[email protected]‘ receivers = [‘[email protected]‘,‘[email protected]‘] message = MIMEText(‘Python邮件发送测试...‘,‘plain‘,‘utf-8‘) message[‘From‘] = formataddr([‘小灰哥‘,sender]) message[‘To‘] = formataddr([‘测试‘,receivers[0]]) subject = Header(‘python 测试一二三‘,‘utf-8‘) message[‘Subject‘] = subject try: smtpObj = smtplib.SMTP(smtp_host,smtp_port) #smtpObj.connect(mail_host,smtp_port) smtpObj.login(mail_user,mail_pwd) smtpObj.sendmail(sender,receivers,message.as_string()) print ‘邮件发送成功‘ smtpObj.quit() except smtplib.SMTPException ,e: print ‘Error:无法发送邮件‘,e
二、执行脚本
python smtp_v2.py
以上是关于python简单的发送邮件的主要内容,如果未能解决你的问题,请参考以下文章