Sending email through Access from secondary email address

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kjhyder
    New Member
    • May 2022
    • 15

    Sending email through Access from secondary email address

    I have an Access database that I send out emails to our customers. When the command button is selected it opens Microsoft Office and generates the emails for me to send. We have now created a general email address (like info@abccompany .com) that is shared among my team. It is possible to change the from address in Outlook for the emails generated from Access? So instead of being from my email address, they are from the general email address?

    Thank you
  • isladogs
    Recognized Expert Moderator Contributor
    • Jul 2007
    • 479

    #2
    I have moved your thread from General to the Access/VBA forum which will hopefully get a more focused response.

    There are several methods for sending emails from Access. Which method are you using?

    For example, I use CDO to send emails and that allows me to specify the send address. Any valid email address can be used.
    I have an example database illustrating this approach. See CDO EMail Tester

    Comment

    • kjhyder
      New Member
      • May 2022
      • 15

      #3
      I am using VB code to send out the emails from Outlook with Access. Below is a bit of my code regarding the emails and attachments.

      Code:
      Do While Not rs.EOF  '
              temp = rs("AGENT")
              MyFileName = rs("AGENT")
       
              attach = mypath & MyFileName & ".pdf"
              strEMail = rs![Email] & ";"
      '
              DoCmd.OpenReport "R1099", acViewReport, , "[AGENT]='" & temp & "'"
              DoCmd.OutputTo acOutputReport, "", acFormatPDF, attach
              DoCmd.Close acReport, "R1099"
              DoEvents
      '
                With oMail
                   .To = Left$(strEMail, Len(strEMail) - 1)
                   .Body = "With the end of 2022 approaching, we are looking to get a jump on year end 1099 preparation. Please review the attached information and let us know if any corrections are required." & vbCrLf & "Thanks," & vbCrLf & "Accounting"
                   .Subject = MyFileName & " Tax Information"
                   .Attachments.Add attach
                   .Display
                End With
      I have permissions to a shared email address with others in the office. I would like the emails I am sending to come from that email address instead of my email address.

      Thanks,

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        Good morning kjhyder;
        There is an obj.SendUsingAc count function - I've not used it myself in the Access environment; however, I have used it in the Outlook environment ( Microsoft Function: outlook.mailite m.sendusingacco unt ) I'm sure there's a way to use this in the Access VBA - might have to either late-bind or add the reference to the Microsoft Outlook object library.
        I'll see if I can bodge something together in a little bit - may be Monday as the code I've used is on my work-account-desktop so I don't have access (punny :) ) to it right now.

        >> keep in mind however, that the original sender information is pegged to the email; thus, while it may appear that it was sent directly from the secondary account (the uninformed user will not know/see any difference), the account that is associated with the primary outlook client is still in the email headers - this is an anti-spam/malware feature and there is no easy way around this within the Outlook/Office-VBA environment.
        Last edited by zmbd; Oct 15 '22, 04:25 PM.

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          Good morning kjhyder;
          Any progress on your end using the information I provided earlier?
          Mind posting any progress you've made?

          I do have my code:

          1) You must have the desktop client installed. The code will not work with the cloud-based-outlook web-application.
          Several of my remote labs have Outlook thru our company's Office365 tenant subscription; however, they do not have the desktop Outlook application installed on their local PCs which has created an issue or two for us (seems you have to pay for more "seats" and the company doesn't want to do that right now 😢 )

          2) The account you want to use must be added to the desktop client
          (Link to instructions: Add an email account to Outlook ) there is simply no other way to do this unless you have the account already setup

          3) I can verify that the resulting email headers have your user account information included in one of the lines; thus, while it will appear for all intents and purposes to come from the chosen account, with the replyto set for that account, anyone with the knowledge how to read the message header information can trace the email right back to your exact PC if needed... Caveat emptor

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            If the General EMaIL Account (info@abccompany .com) exists in your Accounts Collection, then the following should work (basic Code):
            Code:
            Dim OutApp As Object
            Dim objAccount As Object
            Dim objMail As Object
            Dim intCtr As Integer
            
            Set OutApp = CreateObject("Outlook.Application")
            
            For intCtr = 1 To OutApp.Session.Accounts.Count
              If OutApp.Session.Accounts.Item(intCtr) = "info@abccompany.com" Then
                Set objAccount = OutApp.Session.Accounts.Item(intCtr)
                Set objMail = OutApp.CreateItem(0)
                  With objMail
                    .Subject = "Some Subject"
                    .Body = "Body Text here"
                    .To = "someone@somewhere.com"
                    .SendUsingAccount objAccount
                      .Send
                  End With
              End If
            Next

            Comment

            • zmbd
              Recognized Expert Moderator Expert
              • Mar 2012
              • 5501

              #7
              Hello ADezii
              Remarkably similar to the code I have except I look for the SMTP address directly.

              Code:
              Option Compare Database
              Option Explicit
              
              Sub SendUsing_LateBindMethod_Click()
                  'Use following Dim statements for Late Binding
                  'NOTE: Additional Const declaration
                  Const olMailItem As Long = 0    'For Late Binding
                  Const olFolderInbox As Long = 6 'For Late Binding
                  Const olFormatHTML As Long = 2  'For Late Binding
                  Dim objOutlook As Object    'Outlook.Application  '(Note dimensioned as Object)
                  Dim objEmail As Object      'Outlook.MailItem     '(Note dimensioned as Object)
                  Dim objNameSpace As Object  'Outlook.NameSpace    '(Note dimensioned as Object)
                  Dim objFindAccount As Object 'Outlook.Account Temporary object to find the correct account object
                  Dim strFromSmtpAddress As String
                  Dim strSubject As String
                  Dim strToAddress As String
                  Dim strBodyText As String
                  
                  On Error GoTo zErrorTrap
              '
              ' Just a place to put this information
                  strSubject = "My Test Message - testing sendusingaccount property"
                  strToAddress = "BogusToEmail@TrashMail.com"
                  strFromSmtpAddress = "BogusSMTPEmail@TrashMail.com"
                  strBodyText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
              '
              ' Late bind to the outlook application - may not work for Outlook365 - must have desktop version
                  Set objOutlook = GetObject(, "Outlook.Application")
              '
              ' Hook to the currently running outlook application session or create a new session
                  If objOutlook Is Nothing Then
                      Set objOutlook = CreateObject("Outlook.Application")
                      Set objNameSpace = objOutlook.GetNamespace("MAPI")
                      objNameSpace.GetDefaultFolder(olFolderInbox).Display
                  End If
              '
              ' Create a new mail item
                  Set objEmail = objOutlook.CreateItem(olMailItem)
              '
              ' Find the SMPT account in the curent session that you want to send from if not the default account
                If Len(strFromSmtpAddress) > 0 Then
                  For Each objFindAccount In objOutlook.Session.Accounts
                    If objFindAccount.SmtpAddress = strFromSmtpAddress Then
                      Set objEmail.SendUsingAccount = objFindAccount
                    End If
                  Next
                End If
              '
              ' Start the email and send (note there are alot of options here - this are the basics)
                  With objEmail
                      .To = strToAddress
                      .Subject = strSubject
                      .BodyFormat = olFormatHTML
                      .Body = strBodyText
                      '
                      'uncomment to show the email and allow the user to edit before sending.
                      .Display
                      '
                      'comment out display to send without visual
                      '.Send
                      '
                  End With
                  '
              'clean up the memory
              zRecover:
                  If Not objFindAccount Is Nothing Then Set objFindAccount = Nothing
                  If Not objOutlook Is Nothing Then Set objOutlook = Nothing
                  If Not objEmail Is Nothing Then Set objOutlook = Nothing
                  If Not objNameSpace Is Nothing Then Set objOutlook = Nothing
              Exit Sub
              zErrorTrap:
              'Stop
                Select Case Err.Number
                  Case 429
                    'no current outlook object resume the code and create an object
                    Resume Next
                  Case Else
                  MsgBox "ErrSource: " & Err.Source & vbCrLf & "ErrNum: " & Err.Number & vbCrLf & "ErrDescription: " & vbCrLf & Err.Description
                End Select
              Resume zRecover
              End Sub

              Comment

              • kjhyder
                New Member
                • May 2022
                • 15

                #8
                Thank you both for the assistance. I will input the code changes and test.

                Comment

                • ylimejoy27zv
                  Banned
                  New Member
                  • Oct 2022
                  • 1

                  #9
                  How do I send an email from a different email address?
                  Step 1: Add an address you own,online payment gateway in uae,
                  On your computer, open Gmail.
                  In the top right, click Settings. ...
                  Click the Accounts and import or Accounts tab.
                  In the "Send mail as" section, click Add another email address.
                  Enter your name and the address you want to send from.
                  Click Next Step.

                  Comment

                  Working...