3

I am trying to send emails from a specific account but it always sends from my main no matter how much code I try or what I do. Is there any way to tell it to send it from a particular account? I am writing my code in MS Access, but using Outlook objects.

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

Updated code:

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
John Smith
  • 11,678
  • 17
  • 46
  • 51
  • Are you setting the MailItem.SendUsingAccount property? Please show your code. – Dmitry Streblechenko Jul 31 '13 at 17:31
  • @DmitryStreblechenko I added code. Is there a way to tell it to open Outlook and send, too? Right now I have to open Outlook and THEN run this code from Access which is not ideal – John Smith Jul 31 '13 at 17:34
  • @JohnSmith review this as it references the SendUsingAccount property Dmitry made mention of http://msdn.microsoft.com/en-us/library/office/ff869311.aspx – Sorceri Jul 31 '13 at 17:35
  • @Sorceri That doesn't seem to allow me to directly specify an account. It's just looping through accounts in some open session – John Smith Jul 31 '13 at 17:40
  • I tried using the loop and then setting the account to use with SendUsingAccount later based on when smtpaddress matches, and then send, but it doesn't find the other inbox even though I can clearly see its Mailbox in Outlook – John Smith Jul 31 '13 at 17:51
  • What is your code that sets the SendUsingAccount property? What exactly do you mean by "it doesn't find the other inbox"? Why are you searching for some other inbox and how is it relevant to your question? – Dmitry Streblechenko Jul 31 '13 at 18:09
  • @DmitryStreblechenko I may be using the jargon improperly because I am not super well-versed in outlook or anything (sort of like how I get confused if someone refers to an Excel spreadsheet when they really mean workbook). I updated with code. – John Smith Jul 31 '13 at 18:15
  • Looks perfectly fine to me. After the message is sent, do you see the appropriate MAPI properties in OutlookSpy(http://www.dimastr.com/outspy/) (click IMessage)? How about Item | SendUsingAccount? – Dmitry Streblechenko Jul 31 '13 at 18:50
  • @DmitryStreblechenko The message doesn't get sent because for whatever reason it can't find the account even though it's right there in Outlook. If I change it to my main address, the code works but obviously I don't want to use that one. In the loop I call "Debug.Print olAccountTemp.smtpAddress" and all it does it output my main. – John Smith Jul 31 '13 at 19:04
  • What happens if you step through the code? Why can't your code see the right account? Do you see it in OutlookSpy if you click Namespace, select Accounts, click Browse, go to the IEnumVariant tab? – Dmitry Streblechenko Jul 31 '13 at 19:22
  • I can't install OutlookSpy on that machine due to installation restrictions unfortunately. If I step through the code it does what you'd expect. Just iterates over my main account, doesn't list any others. – John Smith Jul 31 '13 at 19:26
  • So you only have one account? Did you add one? How did you do that? – Dmitry Streblechenko Jul 31 '13 at 22:02
  • @DmitryStreblechenko No, I am saying in my Outlook program I have access to MyName@randomdomain.com and then a second inbox with, say, SomeOtherName@randomdomain.com. I wish to send mail from SomeOtherName but the account loop is only finding MyName. – John Smith Aug 01 '13 at 16:40
  • Is that second account an Exchange mailbox? Or just another SMTP account? – Dmitry Streblechenko Aug 01 '13 at 17:24
  • @DmitryStreblechenko I don't know all the differences. Maybe this will help: http://img818.imageshack.us/img818/6272/yaz.png -- the top address (whited out, ____@____.com) is my "main" and the bottom is the one I wish to send mail from. – John Smith Aug 01 '13 at 17:51
  • That looks like a delegate Exchange mailbox. To send on behalf of that mailbox, just set the MailItem.SentOnBehalfOfName property. – Dmitry Streblechenko Aug 01 '13 at 18:16
  • @DmitryStreblechenko Doesn't send – John Smith Aug 01 '13 at 19:33
  • "Doesn't send" as in "nothing" happens"? "I get an error"? "The sender is still wrong"? – Dmitry Streblechenko Aug 02 '13 at 16:20

2 Answers2

6

Try using

Set oMail.sendusingaccount=olAccount

instead of

oMail.sendusingaccount=olAccount

It worked for me, your code is perfect, just the Set is missing.

MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120
0

It is also much easier when the user can select the email address rather than account number. sendCaller loops through the accounts until it finds this email address. From there on it will call sendFile from where the message will be sent.

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
Robert Longson
  • 118,664
  • 26
  • 252
  • 242
Inet Kemp
  • 39
  • 1
  • 6