python - 使用 python 在 Lotus Notes 中发送邮件

标签 python email lotus-notes

我需要帮助使用 python 在 Lotus Notes 中发送邮件,似乎 win32com 可以做到,但我没有找到任何完整的示例或教程。我的想法是像这样一个简单的函数:

from win32com.client import Dispatch
import smtplib

def SendMail(subject, text, user):
    session = Dispatch('Lotus.NotesSession')
    session.Initialize('???')
    db = session.getDatabase("", "")
    db.OpenMail();

一些建议?谢谢!

最佳答案

下面是我多年来为此目的使用的一些代码:

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('your_password')

    server_name = 'your/server'
    db_name = 'your/database.nsf'

    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()
    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 for Send operation'
    doc.ReplaceItemValue("SendTo", sendto)

    if copyto is not None:
        doc.ReplaceItemValue("CopyTo", 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; default is False
    doc.SaveMessageOnSend = True
    doc.Send(False)

if __name__ == '__main__':
    subject = "test subject"
    body = "test body"
    sendto = ['abc@def.com',]
    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, attach=attachment)

关于python - 使用 python 在 Lotus Notes 中发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26065191/

相关文章:

excel - 如何从 Outlook 宏发送 Excel 图表

php - SMTP 错误 : Could not authenticate

ruby-on-rails - 动态配置 SMTP 设置时 ActionMailer 不发送邮件

go - 在 Golang 中通过 COM 与 Lotus Notes 交互

python - 数据框按最大值排序并显示行名称

python - 将 cURL 命令转换为 Python 请求命令

python - Scrapy redirect_urls exception.KeyError

python - 交换两个数字的替代方法

java - 从Java删除目录时出现奇怪的错误: 0 bytes,访问被拒绝

java - 如何使用 Java 复制笔记项目