【问题标题】:How to use RDCOMClient to send Outlook email from a secondary account - translate existing VBA code?如何使用 RDCOMClient 从辅助帐户发送 Outlook 电子邮件 - 翻译现有 VBA 代码?
【发布时间】:2018-09-01 13:49:04
【问题描述】:

我正在尝试使用 RDCOMClient 从辅助电子邮件地址发送电子邮件。我接受了How to retrieve Outlook inbox emails using R RDCOMClient? 的建议并尝试用 VBA 编写并翻译,但无法获得正确的命令。

注意:我不能使用SentOnBehalfOfName,因为我没有必要的权限。

以下 VBA 和 Python 代码均成功从辅助收件箱发送电子邮件。

VBA

Sub SendUsingAccount()

 Dim oAccount As Outlook.Account
 Dim oMail As Outlook.MailItem
 Set oAccount = Application.Session.Accounts.Item(2) 'Index of Mailbox
 Set oMail = Application.CreateItem(olMailItem)
 oMail.Subject = "Sent using MAPI Account"
 oMail.Recipients.Add "email@email.com"
 oMail.Recipients.ResolveAll
 oMail.SendUsingAccount = oAccount
 oMail.Send
End Sub

Python

import win32com.client
o = win32com.client.Dispatch("Outlook.Application")
oacctouse = None
for oacc in o.Session.Accounts:
  if oacc.SmtpAddress == "myemail@email.com":
    oacctouse = oacc
    break

#print oacc   
#dir(oacc)
#oacc.CLSID
#oacc.GetAddressEntryFromID
Msg = o.CreateItem(0)
if oacctouse:
   Msg._oleobj_.Invoke(*(64209, 0, 8, 0, oacctouse))  # Msg.SendUsingAccount = oacctouse
Msg.To="email@email.com"    
Msg.HTMLBody = "test env instance #"
Msg.Send()

R

除了猜测我能想到的[["SMTP"]]$SmtpAddress 等的所有组合之外,我在 R 中尝试过的事情:

OutApp <- COMCreate("Outlook.Application")
outMail <- OutApp$CreateItem(0)
#1 :No Error, but email sends from primary inbox
oa<-OutApp[["Session"]][["Accounts"]]
second_inbox<-oa$Item(2) 
outMail[["SendUsingAccount"]]=second_inbox
#2: Runs, but sends from primary inbox
outMail[["SendUsingAccount"]]="myemail@email.com"
#From what I read emails need to be accessed with a number,not the name
#3 Runs, but sends from primary inbox (the Python index changes every run)
outMail[["SendUsingAccount"]]="oacc_id_from_Python"

#Rest of reproducible code
outMail[["To"]] = "email@email.com"
outMail[["subject"]] = "Alt Acc"
outMail[["body"]] = "test"
outMail$Send()

相关问题:

想法?

【问题讨论】:

  • 刚刚在那里发布了一个类似的问题:我正在尝试从定义的邮箱中检索电子邮件,但存在多个...stackoverflow.com/q/52649215/5224236
  • R 中outMail 分配在哪里?
  • 尝试将SendUsingAccount 赋值放在Send 之前,就像在VBA 中所做的那样。
  • 你回答过这个问题吗?
  • Parfait - 我尝试更改订单,但得到了相同的结果(从主收件箱发送)。 Nova - 不,我从来没有回答过这个问题。我通过在管道的这一部分使用 Python 代码来解决它。然后我从 bat 中触发了 R 和 py 脚本。 @DmitryStreblechenko - 这是一个 Exchange 帐户。

标签: r outlook rdcomclient


【解决方案1】:

来源: http://www.seancarney.ca/2020/10/07/sending-email-from-outlook-in-r/

# Send the message from an alternate account
Email[["sentonbehalfofname"]] = "alternate-sender@test.com"

这家伙成功了。 :)

【讨论】:

    猜你喜欢
    • 2022-08-10
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    相关资源
    最近更新 更多