因为最近做buildbot,想要使用公司的邮件系统 lotus notes发送邮件,但是网上一直没有比较完整的例子,都是vb的例子比较多,或者例子不完整,后来看到一个用visual foxpro 做的例子,才知道lotus 的com对象可能在版本间有差别,比如网上很多例子都用doc.Form,但是实际上doc对象没有Form属性,应该用ReplaceItemValue才对。这个例子是针对notes 6.5的。
from win32com.client import Dispatch
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")
'''
bodyMime=doc.CreateMIMEEntity()
bodyMime.SetContentFromText(stream,"text/html;charset=iso-8859-1",False)
doc.ReplaceItemValue( "Logo", "StdNotesLtr3" )
doc.ReplaceItemValue( "_ViewIcon", 23 )
doc.ReplaceItemValue( "SenderTag", "Y" )
'''
doc.Send(False)
SendEmail("yourname@yourdomain",None,None,"Title:test for python","body:test for python send mail",
["d:/testtool/teri/pcsim/test.xls","d:/testtool/teri/pcsim/test_email.py"],"password")