python发送notes邮件_使用python在Lotus Notes中发送邮件

i need help for send a mail in the Lotus Notes using python, appear that the win32com can do it, but i don't found any complete example or tutorial. My idea is a simple function like it:

from win32com.client import Dispatch

import smtplib

def SendMail(subject, text, user):

session = Dispatch('Lotus.NotesSession')

session.Initialize('???')

db = session.getDatabase("", "")

db.OpenMail();

Some suggestion? Thanks!

解决方案

Below is some code that I have used for this purpose for several years:

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)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值