Python实现的简单发送邮件脚本分享,python发送邮件脚本
Python实现的简单发送邮件脚本分享,python发送邮件脚本
近来有些东西需要监控报警发邮件,然后在网上找了点材料,自己写了一个简单发送邮件的脚本,主要就是运用python的smtplib模块,分享给大家看一下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#导入smtplib和MIMEText
import smtplib,sys
from email.mime.text import MIMEText
def send_mail(sub,content):
#############
#要发给谁,这里发给1个人
mailto_list=["wangwei03@jb51.net"]
#####################
#设置服务器,用户名、口令以及邮箱的后缀
mail_host="mail.gyyx.cn"
mail_user="wangwei03@jb51.net"
mail_pass="123456677890"
mail_postfix="gyyx.cn"
######################
'''''
to_list:发给谁
sub:主题
content:内容
send_mail("aaa@126.com","sub","content")
'''
me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content,_charset='gbk')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(mailto_list)
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, mailto_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == '__main__':
if send_mail(u'这是python测试邮件',u'python发送邮件'):
print u'发送成功'
else:
print u'发送失败'
基于smtplib的简单的
server = smtplib.SMTP('10.30.17.99')
server.set_debuglevel(1)
server.ehlo('localhost')
server.starttls()
server.ehlo('localhost')
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
如果是基于notes COM接口的
def SendEmail(SendTo,CC,BCC,Subject,Body,Attachment=None,Pass=None):
if SendTo==None:
return
session=Dispatch("Lotus.NotesSession")
if Pass:
session.Initialize(Pass)
Server=session.GetEnvironmentString( "MailServer",True)
MaildbName=session.GetEnvironmentString( "MailFile",True)
db=session.GetDatabase(Server,MaildbName)
doc=db.CreateDocument()
doc.ReplaceItemValue("Form","Memo")
if SendTo:
doc.ReplaceItemValue("SendTo",SendTo)
if CC:
doc.ReplaceItemValue("CopyTo",SendTo)
if BCC:
doc.ReplaceItemValue("BlindCopyTo",SendTo)
if Subject:
doc.ReplaceItemValue("Subject",Subject)
stream=session.CreateStream()
stream.WriteText(Body)
bodyMime=doc.CreateMIMEEntity()
bodyMime.SetContentFromText(stream,"text/html;charset=iso-8859-1",False)
if Attachment:
RichTextItem = doc.CreateRichTextItem("Attachment")
for fn in Attachment:
RichTextItem.EmbedObject(1454, "", fn ,"Attachment")
doc.Send(False)...余下全文>>
如果已经安装了python解释器,且.py类型文件与解释器程序相关联了的,则简单的方法就是:
string pyFile=@"c:\test.py";
System.Diagnostics.Process.Start(pyFile);
相关内容
- Python实现的监测服务器硬盘使用率脚本分享,python脚本
- Python的subprocess模块总结,pythonsubprocess
- 数据挖掘之Apriori算法详解和Python实现代码分享,aprio
- Python set集合类型操作总结,pythonset集合类型
- 零基础写python爬虫之抓取百度贴吧代码分享,python爬虫
- 零基础写python爬虫之神器正则表达式,python爬虫
- 零基础写python爬虫之抓取糗事百科代码分享,python爬虫
- 零基础写python爬虫之抓取百度贴吧并存储到本地txt文件
- 零基础写python爬虫之打包生成exe文件,
- 零基础写python爬虫之爬虫编写全记录,python爬虫
评论关闭