因為最近做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")