首页 > 代码库 > python简单的发送邮件
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 = ‘test@sina.com‘ receivers = [‘test@sina.com‘,‘test@qq.com‘] 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简单的发送邮件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。