When python operates Outlook to send mail, it appears "The attachment "xx" of the mail "xx" is opened or is being used by another application.

Use python to operate outlook to send emails, the code is as follows:

import win32com.client as win32

receiver = "[email protected]"   #收件人
outlook = win32.Dispatch("Outlook.Application")
mail = outlook.CreateItem(0)  # 0: creat mail
print("收件人是:" + receiver)
mail.To = receiver  #收件人
mail.Subject = "aa"  #邮件主题
mail.Attachments.Add(r"D:\aa.docx") #添加附件
mail.Body= "test"      #邮件正文
mail.Send()
print("邮件发送成功。")

The following warning pop-up window will always appear when the email is sent:
Warning popup when sending email
My personal guess is that it takes time to load the attachment when sending the email, so add a delay before send():

import win32com.client as win32
import time

receiver = "[email protected]"   #收件人
outlook = win32.Dispatch("Outlook.Application")
mail = outlook.CreateItem(0)  # 0: creat mail
print("收件人是:" + receiver)
mail.To = receiver  #收件人
mail.Subject = "aa"  #邮件主题
mail.Attachments.Add(r"D:\aa.docx") #添加附件
mail.Body= "test"      #邮件正文
time.sleep(1)
mail.Send()
print("邮件发送成功。")

Guess you like

Origin blog.csdn.net/liu_liu_123/article/details/119393315