vba - MS Access/Outlook 2010 - 如何选择从哪个帐户发送电子邮件?

标签 vba ms-access outlook

我正在尝试从特定帐户发送电子邮件,但无论我尝试多少代码或做什么,它总是从我的主帐户发送。有没有办法告诉它从特定帐户发送?我在 MS Access 中编写代码,但使用的是 Outlook 对象。

Sub testEmail()
    On Error Resume Next
    Set outapp = GetObject(, "Outlook.Application")

    If outapp Is Nothing Then
        Set outapp = CreateObject("Outlook.Application")
    End If


    Set oMail = outapp.CreateItem(olMailItem)

    With oMail
        .To = "randomaddress@randomdomain.com"
        .Subject = "test2"

        .Send
    End With

    Set outapp = Nothing
    Set oMail = Nothing

End Sub

更新代码:

Option Compare Database

Sub testEmail()
    On Error Resume Next
    Set oApp = CreateObject("Outlook.Application")
    Set oMail = oApp.CreateItem(olMailItem)
    Set olAccount = oApp.Account
    Set olAccountTemp = oApp.Account
    Dim foundAccount As Boolean
    Dim strFrom As String
    strFrom = "FROMADDY@randomaddress.com"    

    foundAccount = False
    Set olAccounts = oApp.Application.Session.Accounts
    For Each olAccountTemp In olAccounts
        Debug.Print olAccountTemp.smtpAddress
        If (olAccountTemp.smtpAddress = strFrom) Then
            Set olAccount = olAccountTemp
            foundAccount = True
            Exit For
        End If
    Next

    If foundAccount Then
        Debug.Print "ACCT FOUND!"
        With oMail
            .To = "randomaddress@random.com"
            .Body = "Message!"
            .Subject = "test3"
            .sendusingaccount = olAccount
        End With
    Else
        Debug.Print "No acct found"
    End If

    Set oApp = Nothing
    Set oMail = Nothing
    Set olAccounts = Nothing
    Set olAccount = Nothing
    Set olAccountTemp = Nothing
End Sub

最佳答案

尝试使用

Set oMail.sendusingaccount=olAccount

代替

oMail.sendusingaccount=olAccount

它对我有用,您的代码是完美的,只是缺少 Set

关于vba - MS Access/Outlook 2010 - 如何选择从哪个帐户发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17977035/

相关文章:

ms-access - 从当前数据库的表中选择一个值并在 Access VBA 中使用变量

ms-access - 如何使用 Microsoft Access 创建自动管理的 "last update"字段

.net-core - .NET Core 3.0 中的 Outlook 互操作?

css - 边框半径在 Windows Outlook 上的电子邮件模板中不起作用

vba - 使用 VBA 在 Outlook 2003 中循环 PST

html - 如果条件满足,点击 HTMLelement

sql - 如何在 SQL MS Access 中对同一字段使用 "like"和 "not like"?

sql - 使用选择查询从连接表中获取值

VBA 无法正确处理 find 方法返回的范围

string - 在 Excel 的 VBA 中,当字符串包含 '&' 时如何操作页眉/页脚