How to use RDCOMClient to send Outlook email from a secondary account - translate existing VBA code?

∥☆過路亽.° 提交于 2020-01-29 05:32:30

问题


I am trying to send an email from a secondary email address using RDCOMClient. I took the advice from How to retrieve Outlook inbox emails using R RDCOMClient? and tried writing it in VBA and translating, but could not get the right commands.

Note: I can't use SentOnBehalfOfName because I don't have the necessary permission.

The below VBA and Python code both successfully send email from the secondary inbox.

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

Things I have tried in R in addition to guessing all combinations I can think of for [["SMTP"]], $SmtpAddress, etc:

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()

Related questions:

  • https://social.msdn.microsoft.com/Forums/windows/en-US/7afc9e42-ca4f-491b-8c50-19556fb4e1cf/sendusingaccount-does-not-work-in-outlook-2010-possible-bug?forum=outlookdev
  • Sending email in R via outlook,

Ideas?

来源:https://stackoverflow.com/questions/49440262/how-to-use-rdcomclient-to-send-outlook-email-from-a-secondary-account-translat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!