【问题标题】:Unable to send 2 pywin32 emails within one program runtime无法在一个程序运行时发送 2 封 pywin32 电子邮件
【发布时间】: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>&bull; xyz <br>&bull; 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()

【问题讨论】:

    标签: python email pywin32


    【解决方案1】:

    在代码中创建新的 Outlook 应用程序实例后,尝试获取 Namespace 和任何标准文件夹引用:

    import win32com.client as win32
    
    olApp = win32.Dispatch('Outlook.Application')
    olNS = olApp.GetNameSpace('MAPI')
    objFolder = objNS.GetDefaultFolder(olFolderInbox)
    

    Automating Outlook from a Visual Basic Applications 文章中了解更多信息。

    您也可能会发现 Application Shutdown Changes in Outlook 文章很有帮助。

    【讨论】:

      猜你喜欢
      • 2012-04-09
      • 2017-10-27
      • 2012-11-13
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多