使用python在Lotus Notes发送邮件
环境:
1、python 2.7.9
2、Lotus Notes 8.5
说明:
1、server_name地址查看步骤:
登录邮箱—>文件—>场所—>管理场所—>编辑—>服务器—>宿主/邮件服务器
2、db_name查看步骤:
登录邮箱—>文件—>场所—>管理场所—>编辑—>邮件—>邮件文件
代码如下:
#-*- coding:utf-8 -*-
from __future__ import division, print_function
import os, uuid
import itertools as it
from win32com.client import DispatchEx
import pywintypes # for exception
def send_mail(subject,body_text,sendto,copyto=None,blindcopyto=None,attach=None):
session = DispatchEx('Lotus.NotesSession')
session.Initialize('邮箱密码')
server_name = '服务器地址'
db_name = r'邮件文件名'
db = session.GetDatabase(server_name, db_name)
if not db.IsOpen:
try:
db.Open()
except pywintypes.com_error:
print ('could not open database:{}'.format(db_name))
doc = db.CreateDocument()
#print dir(doc)
doc.ReplaceItemValue("Form", "Memo")
doc.ReplaceItemValue("Subject", subject)
# assign random uid because sometimes Lotus Notes tries to reuse the same one
uid = str(uuid.uuid4().hex)
doc.ReplaceItemValue('UNIVERSALID', uid)
# "SendTo" must be populated otherwise you get this error:
# 'No recipient list foe Send operation'
doc.ReplaceItemValue("SendTo", sendto)
if copyto is not None:
doc.ReplaceItemValue("CoptTo", copyto)
if blindcopyto is not None:
doc.ReplaceItemValue("BlindCopyTo", blindcopyto)
# body
body = doc.CreateRichTextItem("Body")
body.AppendText(body_text)
# attachment
if attach is not None:
attachment = doc.CreateRichTextItem("Attachment")
for att in attach:
attachment.EmbedObject(1454, "", att, "Attachment")
# save in 'Sent' view;defaule is False
doc.SaveMessageOnSend = True
doc.Send(False)
print ("succeed")
if __name__ == '__main__':
subject = "test subject"
body = "test body"
sendto = ['收件人邮箱地址']
#files = ['/path/to/a/file.txt', '/path/to/another/file.txt']
#attachment = it.takewhile(lambda x: os.path.exists(x), files)
send_mail(subject, body, sendto)
亲测可用。
备注:代码从互联网搬运过来的,只做学习使用
参考:
https://www.cnpython.com/qa/100597
https://www.cnpython.com/qa/91938