加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

使用python在Lotus Notes中发送邮件

发布时间:2020-12-20 11:38:02 所属栏目:Python 来源:网络整理
导读:我需要帮助使用 python在Lotus Notes中发送邮件,看起来win32com可以做到,但我没有找到任何完整的示例或教程.我的想法是一个简单的功能: from win32com.client import Dispatchimport smtplibdef SendMail(subject,text,user): session = Dispatch('Lotus.No
我需要帮助使用 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,attach=attachment)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读