使用python在Lotus Notes发送邮件

使用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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值