如何从特定帐户发送邮件? [英] How to send mail from a specific account?

查看:58
本文介绍了如何从特定帐户发送邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Office 365(Outlook 版本 1909 内部版本 12026.20264).

I am using Office 365 (Outlook Version 1909 Build 12026.20264).

我配置了两个电子邮件帐户(a@a.com 和 b@b.com)和一个共享邮箱(shared@b.com).

I configured two email accounts (a@a.com and b@b.com) and one shared mailbox (shared@b.com).

使用我的 b@b.com 帐户,我有权从 shared@b.com 发送电子邮件.

With my b@b.com account I have permission to send email from shared@b.com.

我可以从所有帐户(a@a.com、b@b.com、shared@b.com)手动发送电子邮件.

I can manually send email from all accounts (a@a.com, b@b.com, shared@b.com).

我有使用 SentOnBehalfOfName 属性从 shared@b.com 发送电子邮件的 VBA 代码.

I have VBA code to send email from shared@b.com using the SentOnBehalfOfName property.

Sub TEST()
  Dim origEmail As MailItem
  Dim replyEmail As MailItem

  Set origEmail = Application.ActiveWindow.Selection.Item(1)
  Set replyEmail = Application.CreateItemFromTemplate("C:\...\Template.oft")

  replyEmail.SentOnBehalfOfName = "shared@b.com"
  replyEmail.HTMLBody = replyEmail.HTMLBody & origEmail.Reply.HTMLBody
  replyEmail.Subject = "RE:" + origEmail.Subject

  replyEmail.Display
End Sub

通常这会正确发送电子邮件.我可以看到他们转到 b@b.com 帐户中的已发送文件夹.

Usually this sends emails correctly. I can see they go to the sent folder in the b@b.com account.

有时它会尝试使用 a@a.com 帐户发送电子邮件(该帐户无权从 shared@b.com 发送电子邮件).我在 a@a.com 中收到一封错误电子邮件,但该电子邮件未发送.

Sometimes it tries to send emails using the a@a.com account (which has no permission to send emails from shared@b.com). I receive an error email in a@a.com and the email is not sent.

如何更改我的代码,以便每次从 shared@b.com 发送电子邮件时,它都会使用我的 b@b.com 帐户?

How can I change my code so that everytime I am sending email from shared@b.com it will use my b@b.com account?

注意:我将 b@b.com 帐户设置为我的默认帐户.

Note: I set my b@b.com account as my default account.

推荐答案

需要将SendUsingAccount属性设置为b@b.com对应的Account对象邮箱.

You need to set the SendUsingAccount property to the Account object corresponding to the b@b.com mailbox.

Sub TEST()
  Dim origEmail As MailItem
  Dim replyEmail As MailItem

  Set origEmail = Application.ActiveWindow.Selection.Item(1)
  Set replyEmail = Application.CreateItemFromTemplate("C:\...\Template.oft")

  for each acc in Application.Session.Accounts
    if acc.DisplayName = "b@b.com" Then 
      replyEmail.SendUsingAccount = acc
      Exit for
    End If
  next 

  replyEmail.SentOnBehalfOfName = "shared@b.com"
  replyEmail.HTMLBody = replyEmail.HTMLBody & origEmail.Reply.HTMLBody
  replyEmail.Subject = "RE:" + origEmail.Subject

  replyEmail.Display
End Sub

这篇关于如何从特定帐户发送邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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