Python-管理错误消息(电子邮件发送)

发布于 2025-02-10 03:07:44 字数 547 浏览 0 评论 0原文

我试图在电子邮件运输过程中没有错误并在有错误时进行操作时打印一条消息。

我尝试了一下,但没有任何效果,错误消息停止了一切。

如何隐藏错误消息并用我自己的替换并替换该错误消息。

 import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = '[email protected]'
mail.Subject = 'TEST'
mail.Body = 'Hello'


mail.Send()

try :
    print("mail sent")

except BaseException as e:
    print("mail not sent")
    fonction()

I'm trying to print a message when there's no error during the shipping of the email and doing an action if there's an error.

I gave it a try but nothing worked, the error message stop everything.

How hide the error message and replace it with my own and keep with an action.

 import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = '[email protected]'
mail.Subject = 'TEST'
mail.Body = 'Hello'


mail.Send()

try :
    print("mail sent")

except BaseException as e:
    print("mail not sent")
    fonction()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨敛清荷 2025-02-17 03:07:44

您需要编写要在中测试的代码,请尝试块。对于您的消息打印问题,您可以尝试else try-except的块。 else如果在中没有错误 block中没有错误,则块执行。因此,请尝试以下操作:

import win32com.client as win32

try :
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = '[email protected]'
    mail.Subject = 'TEST'
    mail.Body = 'Hello'
    mail.Send()

except BaseException as e:
    print("mail not sent")
    fonction()
else:
    # this part is executed if there is no error
    print("mail sent")

You need write the code you want to test inside try block. And for your message printing problem you can try the else block of try-except. else block executes if there is no error in try block. So try this:

import win32com.client as win32

try :
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = '[email protected]'
    mail.Subject = 'TEST'
    mail.Body = 'Hello'
    mail.Send()

except BaseException as e:
    print("mail not sent")
    fonction()
else:
    # this part is executed if there is no error
    print("mail sent")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文