【问题标题】:Python - Send Email From Shared Mailbox without using "On Behalf Of"Python - 从共享邮箱发送电子邮件而不使用“代表”
【发布时间】:2019-08-09 22:19:44
【问题描述】:

我正在尝试不使用“代表”从“共享邮箱”发送电子邮件。

我可以从 Outlook 进入该邮箱并手动发送电子邮件,当我这样做时,它不会放置“代表”标语。

但是我不能用编程语言做同样的事情。

到目前为止,我已经尝试了这些并学到了以下课程。

import win32com.client as win32

def send_email(sender,recipient):
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = recipient
    mail.Subject = "Test Emails"
    mail.HTMLBody = "Test Content"
    mail.SendUsingAccount = sender
    mail.Send()

recipient = "abc@company.com"
sender= "Mailbox@company.com"

send_email(sender,recipient)

这段代码不是从 Mailbox@company.com 发送的

而是从我的个人 Outlook 帐户发送。

然后我继续寻找,如果我可以看到我拥有的每个帐户,它就不会显示那里的邮箱帐户。

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
for acc in outlook.Session.Accounts:
    print (acc)

但我确实可以访问。我在我的观点上看到了它。我可以通过使用 Outlook 选择要发送电子邮件的帐户来手动发送电子邮件。

我在这里错过了什么。请帮我解决这个问题。

【问题讨论】:

    标签: python-3.x outlook


    【解决方案1】:

    MailItem.SendUsingAccount 属性返回或设置一个Account 对象,该对象表示将在其下发送MailItem 的帐户。因此,您需要获取正确的帐户对象,然后将其分配给属性。它不接受字符串值。

    这是一个 VBA 示例:

    Sub SendUsingAccount()  
     Dim oAccount As Outlook.account  
     For Each oAccount In Application.Session.Accounts  
     If oAccount.AccountType = olPop3 Then  
     Dim oMail As Outlook.MailItem  
     Set oMail = Application.CreateItem(olMailItem)  
         oMail.Subject = "Sent using POP3 Account"  
         oMail.Recipients.Add ("someone@example.com")  
         oMail.Recipients.ResolveAll  
     Set oMail.SendUsingAccount = oAccount  
         oMail.Send  
     End If  
     Next  
    End Sub
    

    或 Python:

    import win32com.client
    
    o = win32com.client.Dispatch("Outlook.Application")
    oacctouse = None
    for oacc in o.Session.Accounts:
        if oacc.SmtpAddress == "sender@mail.com":
            oacctouse = oacc
            break
    Msg = o.CreateItem(0)
    if oacctouse:
        Msg._oleobj_.Invoke(*(64209, 0, 8, 0, oacctouse))  # Msg.SendUsingAccount = oacctouse
    
    if to:
        Msg.To = ";".join(to)
    if cc:
        Msg.CC = ";".join(cc)
    if bcc:
        Msg.BCC = ";".join(bcc)
    
    Msg.HTMLBody = ""
    
    Msg.Send()
    

    【讨论】:

    • 尝试了python代码,它仍然是从我的个人帐户发送的,而不是从共享邮箱发送的。
    • 它不起作用,因为我在 o.Session.Accounts 中看不到我的邮箱。
    • 您的个人资料中似乎没有配置该帐户。如果是这样,您可以尝试改用SentOnBehalfOfName 属性。
    【解决方案2】:

    解决方案是,回到管理团队并要求他们从邮箱设置中禁用“代表”,然后开始在代码中使用 Sentonbehalfof。

    【讨论】:

      猜你喜欢
      • 2020-04-17
      • 1970-01-01
      • 2015-01-03
      • 2022-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      相关资源
      最近更新 更多