笔记:阿里云服务器下python 配置邮箱服务
2018-01-03 16:52
363 查看
linux下发送邮件,我用的服务器是阿里云,向网易邮箱发邮件,因为阿里云ECS专有网络服务器禁用了25端口,所以改用了465
第一种方法:
test1.py
第二种方法:
test2.py
这两种方法大同小异,# python test1.py 运行此文件
第一种方法:
test1.py
# coding:utf-8 import smtplib import sys from email.mime.text import MIMEText mailto_list=['XXX@163.com'] #收件人邮箱列表 mail_user="XXX@163.com" #用户名 mail_passwd="XXXXX" #用户登录密码(第三方登录授权码) mail_host="smtp.163.com" #邮箱服务器 mail_postfix="163.com" #邮箱后缀名 def send_mail(to_list,sub,content): #定义函数,参数为收件人,邮件主题,邮件内容 print content me="<"+mail_user+">" msg=MIMEText(content,'plain') msg['Subject']=sub msg['From']=me msg['To']=';'.join(to_list) #将收件人列表以“;” 形式隔开 try: server = smtplib.SMTP_SSL() #用的是SSL协议的邮箱smtp server.connect(mail_host,465) #smtp 的端口号465 print server.login(mail_user,mail_passwd) server.sendmail(me,to_list,msg.as_string()) #SMTP对象使用sendmail 方法发送邮件 #SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options] server.close() except Exception, e: print str(e) return False send_mail(mailto_list,"Long time no see",'happy new year,beautiful girl') #调用函数
第二种方法:
test2.py
#coding:utf-8 import sys import smtplib #import email.mime.multipart from email.mime.text import MIMEText server = 'smtp.163.com' port = '465' def send_mail(server,port,usr,pwd,msg): smtp = smtplib.SMTP_SSL() smtp.connect(server,port) smtp.login(usr,pwd) smtp.sendmail(msg['from'],msg['to'],msg.as_string()) smtp.quit() print('email has send out successfully !') if __name__ == '__main__': # msg = email.mime.multipart.MIMEMutipart() msg = MIMEText('beautiful girl','plain') msg['subject'] = 'HELLO PYTHON,ITS MY FIRST MAIL TO YOU' msg['From'] = '发件人邮箱@163.com' msg['To'] = '收件人邮箱@163.com' usr = '用户名' pwd = '密码' (第三方授权码) content=' '.join(sys.argv[4:]) send_mail(server,port,usr,pwd,msg)
这两种方法大同小异,# python test1.py 运行此文件
相关文章推荐
- 【笔记】配置Python脚本自启动服务
- 阿里云服务器的一些初始配置,安装mysql,安装ftp服务
- Python笔记——网络服务器之HTTPS服务
- 阿里云apache服务器外网无法访问(配置安全组,添加80服务)
- 阿里云服务器Linux下配置web服务环境
- 阿里云服务器配置-->在服务器端安装IIS服务
- Python--阿里云服务器CentOS 配置流程(一)
- 在阿里云服务器上配置CentOS+Nginx+Python+Flask环境
- [20180310智慧推荐餐厅系统01]配置阿里云服务器Python3和MySql环境
- 阿里云 ECS Linux 服务器 SSH 服务的安全配置优化
- Python笔记——网络服务器之HTTP服务
- 从零开始配置阿里云服务器Centos7.4(一):安装python3
- 阿里云服务器centos7.4操作系统Apache配置https服务
- 阿里云服务器新用户优惠了 99一年,服务配置2G内存。很费力啊!
- 配置搭建阿里云服务器nginx+uwsgi (python)
- 在阿里云服务器上配置CentOS+Nginx+Python+Flask环境
- 阿里云服务器部署笔记一(python3、Flask、uWSGI、Nginx)
- 如何在阿里云服务器里配置iis 搭建web服务
- Django学习笔记四:阿里云服务器购买和配置nginx
- 阿里云服务器配置JDK+Tomcat+Mysql+WEB服务部署