Results 1 to 15 of 15
  1. #1
    talhaparvaiz@yahoo.com is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2020
    Posts
    29

    Sending email through MS access (using non default Outlook email accounts)

    Hi, here is some background on the issue


    • I am using the VB code documented below to send out emails through Outlook.
    • Outlook has 3 email accounts configured and code uses the default to send emails
    • ALL IS WORKING FINE TO THIS POINT
    • Now, I don't want to use the default email address when sending out emails. instead, want to use one of the non-default ones.
    • I am using MS Access 2013 and Outlook 2013





    Code:
    Option Compare Database
    
    Sub Command0_Click()
    
    
    Dim sFileName As String, sEmail As String, BccEmail As String, eBody As String
    Dim rst As DAO.Recordset
    
    
    
    
    Set rst = CurrentDb.OpenRecordset("tbl_Email", dbOpenSnapshot) ' Table Name
    
    
    eBody = "<HTML><BODY>Note</BODY></HTML>"
    
    
    
    
    On Error Resume Next
    rst.MoveFirst
    
    
    
    
    Dim i As Integer
    Dim x As Integer
    
    
    For i = 1 To 500 'sending emails individually
    
    
        sEmail = rst("Email_Address")  'Field Name
        CCEmail = sEmail
        BccEmail = ""
        
        
            For x = 1 To 40 'Adding 40 email addreses to BCC
        If Not rst.EOF Then
        BccEmail = BccEmail & ";" & sEmail
        rst.MoveNext
        sEmail = rst("Email_Address")
        End If
        Next x
        
        If BccEmail <> "" Then
        Call vcSendEmail_Outlook_With_Attachment("Subject", "info@toemail.com", "", BccEmail, "\\fs2\Desktop\test.doc", eBody)
        Call Wait_Time
        End If
    Next i
    
    
    
    
    rst.Close
    Set rst = Nothing
    End Sub


    Thanks in advance

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Explore SentOnBehalfOfName property of email object.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    talhaparvaiz@yahoo.com is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2020
    Posts
    29
    Quote Originally Posted by June7 View Post
    Explore SentOnBehalfOfName property of email object.

    I have modified following code to use SentOnBehalfOfName function but it didn't work... Please advise


    Code:
    Option Compare Database
    
    
    Sub Command0_Click()
    
    
    Dim sFileName As String, sEmail As String, BccEmail As String, eBody As String
    Dim rst As DAO.Recordset
    
    
    
    
    Set rst = CurrentDb.OpenRecordset("tbl_Email", dbOpenSnapshot) ' Table Name
    
    
    eBody = "<HTML><BODY>Note</BODY></HTML>"
    
    
    
    
    On Error Resume Next
    rst.MoveFirst
    
    
    
    
    Dim i As Integer
    Dim x As Integer
    
    
    For i = 1 To 500 'sending emails individually
    
    
        sEmail = rst("Email_Address")  'Field Name
        CCEmail = sEmail
        BccEmail = ""
        
        
        
        For x = 1 To 40 'Adding 40 email addreses to BCC
        If Not rst.EOF Then
        BccEmail = BccEmail & ";" & sEmail
        rst.MoveNext
        sEmail = rst("Email_Address")
        End If
        Next x
        
        If BccEmail <> "" Then
        Call vcSendEmail_Outlook_With_Attachment("Subject", "info@toemail.com", "", BccEmail, "\\fs2\Desktop\test.doc", eBody)
        Call Wait_Time
        End If
    Next i
    
    
    
    
    rst.Close
    Set rst = Nothing
    End Sub
    
    
    
    
    ----------------------------
    ----------------------------
    
    
    
    
    Option Compare Database
    
    
    Sub vcSendEmail_Outlook_With_Attachment(sSubject As String, sTo As String, Optional sCC As String, Optional sBcc As String, Optional sAttachment As String, Optional sBody As String)
        Dim OutApp As Object
        Dim OutMail As Object
     
      
        Set OutApp = CreateObject("Outlook.Application")
        
        
        OutApp.Session.Logon
        
        
        Set OutMail = OutApp.CreateItem(0)
     
         OutMail.SentOnBehalfOfName = "email@From.com"
        OutMail.To = sTo
        If sCC <> "" Then OutMail.CC = sCC
        If sBcc <> "" Then OutMail.BCC = sBcc
        OutMail.Subject = sSubject
        If sBody <> "" Then OutMail.HTMLBody = sBody
    
    
    
    
        OutMail.Attachments.Add (sAttachment)
    
    
        SendKeys "^{ENTER}"
        OutMail.Send 'Send | Display
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    What does "didn't work" mean - error message, wrong result, nothing happens?

    I've never used this and can't really test it so can only refer to web for guidance.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    talhaparvaiz@yahoo.com is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2020
    Posts
    29
    Quote Originally Posted by June7 View Post
    What does "didn't work" mean - error message, wrong result, nothing happens?

    I've never used this and can't really test it so can only refer to web for guidance.

    Nothing happened... Code just ran and no email was generated.

    I have tried a lot of variations but due to my lack of VBA knowledge wasn't able to accomplish much

    Please assist

    thanks

  6. #6
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Try to use SendUsingAccount instead of SendOnBehalfOfName:
    https://www.slipstick.com/developer/...cific-account/

    Note the loop through the Outlook accounts (assumes you have them defined in your Outlook under Accounts).
    Some more info on both:
    https://stackoverflow.com/questions/...ess-in-outlook
    http://www.slipstick.com/developer/c...l-address-vba/
    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  7. #7
    talhaparvaiz@yahoo.com is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2020
    Posts
    29
    Quote Originally Posted by Gicu View Post
    Try to use SendUsingAccount instead of SendOnBehalfOfName:
    https://www.slipstick.com/developer/...cific-account/

    Note the loop through the Outlook accounts (assumes you have them defined in your Outlook under Accounts).
    Some more info on both:
    https://stackoverflow.com/questions/...ess-in-outlook
    http://www.slipstick.com/developer/c...l-address-vba/
    Cheers,
    Vlad



    Here are the modifications that I have made to use SendOnBehalfOfName but it's not working. when I run the code nothing happens


    Code:
    ption Compare Database
    
    
    Sub Command0_Click()
    
    
    Dim sFileName As String, sEmail As String, BccEmail As String, eBody As String
    Dim rst As DAO.Recordset
    
    
    
    
    Set rst = CurrentDb.OpenRecordset("tbl_Email", dbOpenSnapshot) ' Table Name
    
    
    eBody = "<HTML><BODY>Note</BODY></HTML>"
    
    
    
    
    On Error Resume Next
    rst.MoveFirst
    
    
    
    
    Dim i As Integer
    Dim x As Integer
    
    
    For i = 1 To 500 'sending emails individually
    
    
        sEmail = rst("Email_Address")  'Field Name
        CCEmail = sEmail
        BccEmail = ""
        
        
        
        For x = 1 To 40 'Adding 40 email addreses to BCC
        If Not rst.EOF Then
        BccEmail = BccEmail & ";" & sEmail
        rst.MoveNext
        sEmail = rst("Email_Address")
        End If
        Next x
        
        If BccEmail <> "" Then
        Call vcSendEmail_Outlook_With_Attachment("Subject", "info@toemail.com", "", BccEmail, "\\fs2\Desktop\test.doc", eBody)
        Call Wait_Time
        End If
    Next i
    
    
    
    
    rst.Close
    Set rst = Nothing
    End Sub
    
    
    
    
    ----------------------------
    ----------------------------
    
    
    
    
    Option Compare Database
    
    
    Sub vcSendEmail_Outlook_With_Attachment(sSubject As String, sTo As String, Optional sCC As String, Optional sBcc As String, Optional sAttachment As String, Optional sBody As String)
        Dim OutApp As Object
        Dim OutMail As Object
     
      
        Set OutApp = CreateObject("Outlook.Application")
        
        
        OutApp.Session.Logon
        
        
        Set OutMail = OutApp.CreateItem(0)
     
     OutMail.To = sTo
        If sCC <> "" Then OutMail.CC = sCC
        If sBcc <> "" Then OutMail.BCC = sBcc
        OutMail.Subject = sSubject
        If sBody <> "" Then OutMail.HTMLBody = sBody
    
    
    
    
        OutMail.Attachments.Add (sAttachment)
    
        OutMail.SendUsingAccount = "From@Email.com"   
    
        SendKeys "^{ENTER}"
        OutMail.Send
    
    
        
        
    
        Set OutMail = Nothing
        Set OutApp = Nothing End Sub
    


    Please advise

    Thanks

  8. #8
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Have you looked at the links I've included? You need to loop through the accounts until you find the one you want to use, can you try this:
    Code:
    Sub vcSendEmail_Outlook_With_Attachment(sSubject As String, sTo As String, Optional sCC As String, Optional sBcc As String, Optional sAttachment As String, Optional sBody As String)
    Dim OutApp As Object
    Dim OutMail As Object
    Dim oAccount As Object 'Outlook.Account
     
      
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    
    
    For Each oAccount In OutApp.Session.Accounts
       If oAccount = "From@Email.com" Then    'replace with valid email
        	Set OutMail = OutApp.CreateItem(0)
        	outMail.SendUsingAccount = oAccount 
        	OutMail.To = sTo
        	If sCC <> "" Then OutMail.CC = sCC
        	If sBcc <> "" Then OutMail.BCC = sBcc
        	OutMail.Subject = sSubject
        	If sBody <> "" Then OutMail.HTMLBody = sBody
        	OutMail.Attachments.Add (sAttachment)
        	OutMail.Send
       End If
    Next   
    
    
    Set OutMail = Nothing
    Set OutApp = Nothing 
    
    
    End Sub
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  9. #9
    talhaparvaiz@yahoo.com is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2020
    Posts
    29
    Quote Originally Posted by Gicu View Post
    Have you looked at the links I've included? You need to loop through the accounts until you find the one you want to use, can you try this:
    Code:
    Sub vcSendEmail_Outlook_With_Attachment(sSubject As String, sTo As String, Optional sCC As String, Optional sBcc As String, Optional sAttachment As String, Optional sBody As String)
    Dim OutApp As Object
    Dim OutMail As Object
    Dim oAccount As Object 'Outlook.Account
     
      
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    
    
    For Each oAccount In OutApp.Session.Accounts
       If oAccount = "From@Email.com" Then    'replace with valid email
            Set OutMail = OutApp.CreateItem(0)
            outMail.SendUsingAccount = oAccount 
            OutMail.To = sTo
            If sCC <> "" Then OutMail.CC = sCC
            If sBcc <> "" Then OutMail.BCC = sBcc
            OutMail.Subject = sSubject
            If sBody <> "" Then OutMail.HTMLBody = sBody
            OutMail.Attachments.Add (sAttachment)
            OutMail.Send
       End If
    Next   
    
    
    Set OutMail = Nothing
    Set OutApp = Nothing 
    
    
    End Sub
    Cheers,

    Code ran with no results... I have attached my access DB for your review. Thanks



    Send Bulk Email - Test.zip

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    You have not replaced "From@Email.com" with whatever you want as a valid email. However, even with that fixed, I am not getting emails. I have never needed to use Sleep and Do Events with Outlook automation. Use Display instead of Send until you are sure code works.

    I step debugged and as soon as OutMail.SendUsingAccount = oAccount is executed, code exits that If block and goes back to button event. Never even gets to Send method.

    Try using:

    Set OutMail.SendUsingAccount = oAccount


    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    talhaparvaiz@yahoo.com is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2020
    Posts
    29
    Quote Originally Posted by June7 View Post
    You have not replaced "From@Email.com" with whatever you want as a valid email. However, even with that fixed, I am not getting emails. I have never needed to use Sleep and Do Events with Outlook automation. Use Display instead of Send until you are sure code works.

    I step debugged and as soon as OutMail.SendUsingAccount = oAccount is executed, code exits that If block and goes back to button event. Never even gets to Send method.

    Try using:

    Set OutMail.SendUsingAccount = oAccount




    I have made following 3 changes to the code:

    - If oAccount = "From@Email.com" Then 'replaced with valid email
    - Set outMail.SendUsingAccount = oAccount
    - Outmail.Display

    Here are my findings/challenges:


    - When I use Outmail.Display, then the email is created with all required information populated (From email, to email, subject, etc.)
    - When I use Outmail.Send, then nothing happens (no email is created or sen


    Here is the updated code
    Code:
    Sub vcSendEmail_Outlook_With_Attachment(sSubject As String, sTo As String, Optional sCC As String, Optional sBcc As String, Optional sAttachment As String, Optional sBody As String)
    Dim OutApp As Object
    Dim OutMail As Object
    Dim oAccount As Object 'Outlook.Account
     
      
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    
    
    For Each oAccount In OutApp.Session.Accounts
       If oAccount = "From@Email.com" Then    'replace with valid email
            Set OutMail = OutApp.CreateItem(0)
          Set outMail.SendUsingAccount = oAccount 
            OutMail.To = sTo
            If sCC <> "" Then OutMail.CC = sCC
            If sBcc <> "" Then OutMail.BCC = sBcc
            OutMail.Subject = sSubject
            If sBody <> "" Then OutMail.HTMLBody = sBody
            OutMail.Attachments.Add (sAttachment)
            OutMail.Display
       End If
    Next   
    
    
    Set OutMail = Nothing
    Set OutApp = Nothing

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    How do you know it isn't sent? Did you go check the Sent folder of that account?

    I don't have another account I can test with so this is really all I can offer.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Is that a shared maillbox? See https://support.microsoft.com/en-us/...rs=en-us&ad=us
    Exchange server? https://kb.intermedia.net/Article/1633
    You could try the suggestions in this forums (note that the use the SendKeys is not usually recommended):
    https://stackoverflow.com/questions/...ient-when-clos
    https://stackoverflow.com/questions/...ng-word-editor
    https://stackoverflow.com/questions/...d-method-fails
    Code:
    
    Sub vcSendEmail_Outlook_With_Attachment(sSubject As String, sTo As String, Optional sCC As String, Optional sBcc As String, Optional sAttachment As String, Optional sBody As String)
    Dim OutApp As Object
    Dim OutMail As Object
    Dim oAccount As Object 'Outlook.Account
    Dim oNS as Object
    
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set oNS =OutApp.GetNamespace("MAPI")
    oNS.Logon
    
    For Each oAccount In OutApp.Session.Accounts
       If oAccount = "From@Email.com" Then    'replace with valid email
            Set OutMail = OutApp.CreateItem(0)
          Set outMail.SendUsingAccount = oAccount 
            OutMail.To = sTo
            If sCC <> "" Then OutMail.CC = sCC
            If sBcc <> "" Then OutMail.BCC = sBcc
            OutMail.Subject = sSubject
            If sBody <> "" Then OutMail.HTMLBody = sBody
            OutMail.Attachments.Add (sAttachment)
            OutMail.Display
       End If
    Next   
    
    
    
    
    Set OutMail = Nothing
    Set OutApp = Nothing
    [/COLOR]
    I added the NameSpace logon as suggested in one of the links, please give it a try.
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  14. #14
    talhaparvaiz@yahoo.com is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2020
    Posts
    29
    Quote Originally Posted by Gicu View Post
    Is that a shared maillbox? See https://support.microsoft.com/en-us/...rs=en-us&ad=us
    Exchange server? https://kb.intermedia.net/Article/1633
    You could try the suggestions in this forums (note that the use the SendKeys is not usually recommended):
    https://stackoverflow.com/questions/...ient-when-clos
    https://stackoverflow.com/questions/...ng-word-editor
    https://stackoverflow.com/questions/...d-method-fails
    Code:
    
    Sub vcSendEmail_Outlook_With_Attachment(sSubject As String, sTo As String, Optional sCC As String, Optional sBcc As String, Optional sAttachment As String, Optional sBody As String)
    Dim OutApp As Object
    Dim OutMail As Object
    Dim oAccount As Object 'Outlook.Account
    Dim oNS as Object
    
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set oNS =OutApp.GetNamespace("MAPI")
    oNS.Logon
    
    For Each oAccount In OutApp.Session.Accounts
       If oAccount = "From@Email.com" Then    'replace with valid email
            Set OutMail = OutApp.CreateItem(0)
          Set outMail.SendUsingAccount = oAccount 
            OutMail.To = sTo
            If sCC <> "" Then OutMail.CC = sCC
            If sBcc <> "" Then OutMail.BCC = sBcc
            OutMail.Subject = sSubject
            If sBody <> "" Then OutMail.HTMLBody = sBody
            OutMail.Attachments.Add (sAttachment)
            OutMail.Display
       End If
    Next   
    
    
    
    
    Set OutMail = Nothing
    Set OutApp = Nothing
    [/COLOR]
    I added the NameSpace logon as suggested in one of the links, please give it a try.

    It worked... Yahoooooooooooooooooooooooo!

    Thank you so much for your assistance

  15. #15
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You're very welcome, good luck with your project!

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Sending Email from Access using Outlook
    By dccjr3927 in forum Programming
    Replies: 3
    Last Post: 06-26-2019, 12:16 PM
  2. Sending a value from Access to Outlook email
    By George in forum Import/Export Data
    Replies: 8
    Last Post: 09-03-2018, 02:53 AM
  3. Replies: 1
    Last Post: 06-19-2018, 10:38 AM
  4. Replies: 6
    Last Post: 07-28-2017, 09:07 AM
  5. Replies: 3
    Last Post: 09-01-2010, 08:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums