从其他帐户发送电子邮件

Sending email from other account

我使用 Excel VBA 通过 Outlook 发送电子邮件。我正在使用我的工作计算机和我的工作电子邮件作为主要帐户,但想从另一个已登录的帐户发送。

我还没有成功集成在网上找到的任何代码。

下面的代码是我没有尝试修复的。

Sub Test1()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell As Range
    Dim strbody As String
    For Each cell In Range("D2:D2")
        strbody = strbody & cell.Value & vbNewLine
    Next

    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")

    On Error GoTo cleanup
    For Each cell In Columns("A").Cells.SpecialCells(xlCellTypeConstants)
        If cell.Value Like "?*@?*.?*" And _
           LCase(Cells(cell.Row, "C").Value) = "yes" Then

            Set OutMail = OutApp.CreateItem(0)
            On Error Resume Next
            With OutMail
                .To = cell.Value
                .Subject = "A personal message from the founder"
                .Body = "Hi " & Cells(cell.Row, "B").Value & vbNewLine & vbNewLine & strbody
                .Send
            End With
            On Error GoTo 0
            Set OutMail = Nothing
        End If
    Next cell

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub

您的意思是从另一个电子邮件地址发送?如果是这样,只需添加:

.SentOnBehalfOfName = "Email@Other.com" 'Change to the email address you want to send from

例如

With OutMail
            .To = cell.Value
            .SentOnBehalfOfName = "Email@Other.com"
            .Subject = "A personal message from the founder"
            .Body = "Hi " & Cells(cell.Row, "B").Value & vbNewLine & vbNewLine & strbody
            .Send
        End With

Outlook 中有两种可能的方法:

  1. 如果在 Outlook 中配置了另一个帐户,您需要使用 MailItem.SendUsingAccount 属性,其中 returns 或设置一个 Account 对象,代表 MailItem待发
Sub SendUsingAccount() 
 Dim oAccount As Outlook.account 
 For Each oAccount In Application.Session.Accounts 
 If oAccount.AccountType = olPop3 Then 
 Dim oMail As Outlook.MailItem 
 Set oMail = Application.CreateItem(olMailItem) 
     oMail.Subject = "Sent using POP3 Account" 
     oMail.Recipients.Add ("someone@example.com") 
     oMail.Recipients.ResolveAll 
 Set oMail.SendUsingAccount = oAccount 
     oMail.Send 
 End If 
 
 Next  
End Sub
  1. 如果您已获得 Exchange 管理员设置的代表其他人发送的权限,您需要使用 MailItem.SentOnBehalfOfName 属性 其中 returns 表示显示名称的字符串邮件消息的预期发件人。