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

3
我想要从一个特定的账户发送电子邮件,不管我尝试写多少代码或者怎么做,它总是从我的主账户发送。有没有办法让它从一个特定的账户发送呢?我在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属性?请展示你的代码。 - Dmitry Streblechenko
@JohnSmith请审核此内容,因为它涉及到Dmitry提到的SendUsingAccount属性。链接:http://msdn.microsoft.com/en-us/library/office/ff869311.aspx - Sorceri
@Sorceri,这似乎不允许我直接指定一个账户。它只是在某个开放的会话中循环遍历账户。 - John Smith
所以你只有一个账户吗?你添加了一个吗?你是怎么做到的? - Dmitry Streblechenko
@DmitryStreblechenko,不是的,我的Outlook程序中有MyName@randomdomain.com和第二个收件箱,比如SomeOtherName@randomdomain.com。我希望从SomeOtherName发送邮件,但账户循环只找到MyName。 - John Smith
显示剩余13条评论
2个回答

6

尝试使用

Set oMail.sendusingaccount=olAccount

代替
oMail.sendusingaccount=olAccount

这对我有用,你的代码完美无缺,只是缺少了Set


0

如果用户可以选择电子邮件地址而不是账号,那么这也会更容易。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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接