5

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:

Ideas?

  • Just posted a similar question there: I am trying to retrieve emails from a defined mailbox when several exist...https://stackoverflow.com/q/52649215/5224236 – gaut Oct 04 '18 at 14:52
  • Where do you assign `outMail` in R? – Parfait Oct 04 '18 at 15:07
  • Try placing the `SendUsingAccount` assignment just before `Send` as done in VBA. – Parfait Oct 04 '18 at 16:47
  • did you ever answer this? – Nova Nov 22 '19 at 20:44
  • What kind of accountant is it? Exchange? POP3/SMTP? – Dmitry Streblechenko Nov 28 '19 at 00:50
  • 1
    Parfait - I tried changing the order, but got the same result (sends from primary inbox). Nova - No, I never answered this. I worked around it by using Python code for this section of my pipeline. Then I triggered the R and py scripts from a bat. @DmitryStreblechenko - it's an Exchange account. – RustlessBroom Dec 17 '19 at 07:06
  • @RustlessBroom, Tried to send the outlook email but the code fails in create item step itself. what is the issue?? code:```import win32com.client outlook = win32com.client.Dispatch("Outlook.Application")mail = outlook.CreateItem(0) mail``` – Pravin Feb 13 '23 at 15:01

1 Answers1

1

Source: 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"

This guy nailed it. :)

Ryan Bradley
  • 627
  • 6
  • 9
  • As I note in the question, I couldn't use 'sentonbehalfofname' because I didn't have the necessary permissions. Even without those permissions, I'm able to send emails from that account in VBA & python. The question was, how do you make that work in R? – RustlessBroom Mar 27 '23 at 17:36