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

Posted

技术标签:

【中文标题】MS Access / Outlook 2010 - 如何选择从哪个帐户发送电子邮件?【英文标题】:MS Access / Outlook 2010 - how to choose which account to send email from? 【发布时间】:2013-07-31 17:28:10 【问题描述】:

我正在尝试从特定帐户发送电子邮件,但无论我尝试多少代码或做什么,它总是从我的主帐户发送。有没有办法告诉它从特定帐户发送?我在 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

【问题讨论】:

您是否设置了 MailItem.SendUsingAccount 属性?请出示您的代码。 @DmitryStreblechenko 我添加了代码。有没有办法告诉它也打开 Outlook 并发送?现在我必须打开 Outlook,然后从 Access 运行这段代码,这并不理想 @JohnSmith 审查此内容,因为它引用了 Dmitry 提到的 SendUsingAccount 属性 msdn.microsoft.com/en-us/library/office/ff869311.aspx @Sorceri 这似乎不允许我直接指定一个帐户。它只是在一些开放会话中循环访问帐户 我尝试使用循环,然后根据 smtpaddress 匹配的时间设置帐户以与 SendUsingAccount 一起使用,然后发送,但即使我可以清楚地看到它的邮箱,它也找不到其他收件箱在 Outlook 中 【参考方案1】:

尝试使用

Set oMail.sendusingaccount=olAccount

而不是

oMail.sendusingaccount=olAccount

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

【讨论】:

【参考方案2】:

当用户可以选择电子邮件地址而不是帐号时,这也容易得多。 sendCaller 循环访问帐户,直到找到此电子邮件地址。从那里它会调用 sendFile 从那里发送消息。

Sub sendCaller()
'creates outlook application
'chooses an email address and finds the corresponding account number

    Dim OutApp As Object
    Dim i As Integer, accNo As Integer

    Set OutApp = CreateObject("Outlook.Application")
    emailToSendTo = "name@domain.com"  'put required email address

'if smtp address=email we want to send to, acc no we are looking for is identified
   For i = 1 To OutApp.Session.Accounts.Count
      'Uncomment the Debug.Print command to see all email addresses that belongs to you
       '''Debug.Print "Acc name: " & OutApp.Session.Accounts.Item(i) & " Acc number: " & i & " email: " & OutApp.Session.Accounts.Item(i).smtpAddress
       If OutApp.Session.Accounts.Item(i).smtpAddress = emailToSendTo Then accNo = i
    Next i

    sendFile accNo

End Sub

Sub sendFile(accountNo As Integer)
    Dim OutApp As Object
    Dim OutMail As Object


    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)


    With OutMail

        .To = "recipient@domain.com"
        .Subject = "Test"
        .Body = "Body"
        Set .SendUsingAccount = OutApp.Session.Accounts.Item(accountNo)
        .Send
    End With
End Sub

【讨论】:

以上是关于MS Access / Outlook 2010 - 如何选择从哪个帐户发送电子邮件?的主要内容,如果未能解决你的问题,请参考以下文章

MS Outlook 干扰 Access vba 程序

MS Access 发送电子邮件(不是来自 Outlook 或用户的电子邮件)

计算机二级ms office和office的区别

访问宏以从 MS Outlook 读取电子邮件

将 accde 文件从 MS Access 2013 保存到 MS Access 2010

使用 c# 和 oledb 查询更新 Ms-Access 2010 中的列值