python 发邮件写例子
#! /usr/bin/env python
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
import sys
from email.mime.text import MIMEText
mailto_list=['[email protected]']
mail_host="smtp.sohu.com" #邮件服务器的域名 如:smtp.sohu.com pop3.sohu.com等
mail_post="25"
mail_user="xxx" #邮件服务用户名
mail_pass="xxx" #和邮件服务登录的密码
mail_postfix="sohu.com"
def send_mail(to_list,sub,content):
me="hello"+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content,_subtype='plain')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
server = smtplib.SMTP()
server.connect(mail_host,mail_post)
server.login(mail_user,mail_pass)
server.sendmail(me, to_list, msg.as_string())
server.close()
return True
except Exception, e:
print str(e)
return False
#for i in range(5):
#if send_mail(mailto_list,"hello","haha!"):
#print "done!"
#else:
#print "failed!"
if __name__=="__main__":
to_list = sys.argv[1]
sub = sys.argv[2]
content = sys.argv[3]
#print to_list
#print sub
#print content
send_mail(to_list,sub,content)
---------------------------------------------------------------------------------------------------------------------------------
运行命令:python sendmail.py [email protected] hi hello
通过cmd传三个参数
python程序外部参数通过sys.argv传入,sys.argv为list。
sys.argv第一项为程序文件的名称,后面的依次为参数列表
参数之间用空格隔开
sys.argv这个参数就好比我们java中的main函数中经常看到的argv[]
-----------------------------------------------------------------------------------------------