如何使用 RDCOMClient 从辅助帐户发送 Outlook 电子邮件 - 翻译现有的 VBA 代码? [英] How to use RDCOMClient to send Outlook email from a secondary account - translate existing VBA code?

查看:90
本文介绍了如何使用 RDCOMClient 从辅助帐户发送 Outlook 电子邮件 - 翻译现有的 VBA 代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 RDCOMClient 从辅助电子邮件地址发送电子邮件.我接受了 如何使用以下方法检索 Outlook 收件箱电子邮件的建议R RDCOMClient? 并尝试用 VBA 编写它并进行翻译,但无法获得正确的命令.

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.

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

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

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

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

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

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

相关问题:

想法?

推荐答案

来源: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"

这家伙搞定了.:)

这篇关于如何使用 RDCOMClient 从辅助帐户发送 Outlook 电子邮件 - 翻译现有的 VBA 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆