Results 1 to 3 of 3

Thread: [RESOLVED] Sending email to mutiple addresses

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    767

    Resolved [RESOLVED] Sending email to mutiple addresses

    Hello vbforums
    I searched the site and I found this code for sending email from outlook.
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Const SW_SHOWNORMAL = 1

    Code:
    'the sub:
    Sub My_Mailto(pr_Recipients_s As String, _
                  Optional pr_Subject_s As String = "", _
                  Optional pr_Body_s As String = "", _
                  Optional pr_CC_s As String = "", _
                  Optional pr_BCC_s As String = "")
    '---------------------------------------------------------------------------------------
    ' Purpose   :  Create an email (not sent automatically)
    ' Returns   :  (none)
    ' Parameters:  (various email fields)
    ' Author    :  Si_the_geek (vbForums.com)
    ' Date      :  Fri 14 Jan 2005
    '---------------------------------------------------------------------------------------
     
    Dim vl_Execute_s As String      'Format the parameters
      vl_Execute_s = "mailto:" & pr_Recipients_s
     
      If (pr_Subject_s <> "") Then
        vl_Execute_s = vl_Execute_s & "&subject=" & pr_Subject_s
      End If
      If (pr_CC_s <> "") Then
        vl_Execute_s = vl_Execute_s & "&cc=" & pr_CC_s
      End If
      If (pr_BCC_s <> "") Then
        vl_Execute_s = vl_Execute_s & "&bcc=" & pr_BCC_s
      End If
      If (pr_Body_s <> "") Then
        vl_Execute_s = vl_Execute_s & "&body=" & pr_Body_s
      End If
     
                                    'Convert CR's to HTML CR's
        '(note - this works well for Outlook, not tested on other clients)
      vl_Execute_s = Replace(vl_Execute_s, vbCr, "%0A")
     
                                    'Create the mail
      ShellExecute 0, vbNullString, vl_Execute_s, vbNullString, "C:\", SW_SHOWNORMAL
     
    End Sub
    Code:
    Private Sub Command1_Click()
    Dim sBodyText As String
    sBodyText = "body text here" & vbCr & "testing {#1#} testing {#2#}*"
    sBodyText = Replace(sBodyText, "*#1#*", txtFirstName.Text)
    sBodyText = Replace(sBodyText, "*#1#*", txtSurname.Text)
    Call My_Mailto("si_the_geek@example.com", "subject here", sBodyText)
    End Sub
    I was able to sznd emails to single address. And even to multiples address by seperating the recipients' addresses with a semi-colon.
    However in my case the addresses are stored in access database under email field.
    Can anyone help to send emails to multiple addresses stored in database.
    Thank you in advance.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Sending email to mutiple addresses

    loop through the recordset and add the email addresses to a string of ; separated addresses, then pass that to your my_mailto

    like
    Code:
    do until rs.eof
      strmails = strmails & rs("email") & ";"
      rs.movenext
    loop
    my_mailto strmails, "subject here", sbodytext
    where rs is a recordset from your database table
    i did not test this code

    for sending email from outlook
    that code is not specific for outlook and should send using whatever is the default email client
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    767

    Re: [RESOLVED] Sending email to mutiple addresses

    thanks a lot sir
    that's solved my issue

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width