【发布时间】:2022-01-21 13:03:33
【问题描述】:
所以我可以打开 Outlook 并插入所有需要的值来创建电子邮件草稿,但是,当我重新打开窗口并尝试发送另一封电子邮件时,它会遇到此错误。
self.mailItem = olApp.CreateItem(0)
File "<COMObject Outlook.Application>", line 2, in CreateItem
pywintypes.com_error: (-2147023174, 'The RPC server is unavailable.', None, None)
是我调用它的方式还是这个 pywin32 的工作方式,我将如何解决这个问题,使我可以使用相同的代码发送尽可能多的电子邮件?用 self.mailItem.Send() 替换 self.mailItem.Display(),允许我发送我想要的多个,但是我希望用户在发送之前编辑任何撰写的电子邮件。因此这不是一个选择。
我的电子邮件课程
import win32com.client as win32
from PyQt5.QtWidgets import QMessageBox
olApp = win32.Dispatch('Outlook.Application')
olNS = olApp.GetNameSpace('MAPI')
class emailComposition():
def __init__(self):
self.mailItem = olApp.CreateItem(0)
self.mailItem.BodyFormat = 1
self.mailItem.Sensitivity = 2
attachment = self.mailItem.Attachments.Add("C:\\Users\\----\Desktop\\Krypt\\Images\\Logos\\Logo1.png")
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1")
def setSubject(self, subj):
self.mailItem.Subject = subj
def setSender(self, senderEmail):
self.sender = senderEmail
def setRecipient(self, recipientEmail, recipientName):
self.mailItem.To = recipientEmail
self.rName = recipientName
def setText(self, ItemList):
items = "<br>• xyz <br>• xyz"
# still need to do something with the ItemList, removing it has no effect on the error outcome!
self.mailItem.HTMLBody = "<p>Good Day " + self.rName + " <br>Could you please send me a quote on the following: " + items + " </p> <p>Thanks</p><img src=""cid:MyId1"" width=""100"" height=""100"" >"
def displayEmail(self):
try:
self.mailItem._oleobj_.Invoke(*(64209, 0, 8, 0, olNS.Accounts.Item(self.sender)))
except:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText("Unregistered email")
msg.setInformativeText("""The email provided isn't a registered email within outlook.\nA default email will be used.""")
msg.setWindowTitle("Krypt")
msg.exec_()
self.mailItem.Display()
我用来调用这个类的代码,所有的都是一个按钮按下连接
def createEmail(self):
input = self.ui.tblQuoteItems.item(0, 0)
if input is not None:
if self.ui.edtSupplier.text() != "" and self.ui.edtSupplierRep.text() != "":
x = emailComposition()
x.setRecipient(self.ui.edtSupplierEmail.text(), self.ui.edtSupplierRep.text())
x.setSubject("Official Quote")
x.setSender(self.ui.edtUserEmail.text())
self.read() #Loads self.itemList with needed items
x.setText(self.itemList)
x.displayEmail()
【问题讨论】: