Sending emails via a group

Posted on 2009-04-07
Last Modified: 2013-11-26
Hello All
I need to be able to send mails from my APP internally via exchange server and i have done this OK. Now the customer has seen that this is possible they want to expand on this a little and send to multiple people. I can set up groups such as sales and purchasing and have send mails like this from my APP, once again tested and all OK. Question is can i add users to exchange groups programatically/on the fly or do i need to do this manually before hand, I.E. add user a and user b to purchasing  or  can i select users from a list and write somthing to add them to a group

Hope that makes sense
Question by:bostonste
LVL 15

Accepted Solution

spprivate earned 500 total points
ID: 24091121
Here is a code .
Private Sub cmdAdd_Click()

On Error Resume Next


    Dim sysPath As String, sSrv As String, sFN As String, sOpenSTR As String

    Dim adoCon As New ADODB.Connection

    Dim adoRS As New ADODB.Recordset


    sysPath = App.Path


    sSrv = "R01" 'SQL production Database


    sFN = "Assoc"

    strFolderPath = "Public Folders\All Public Folders\" & sFN


    arrFolders() = Split(strFolderPath, "\")

    Set objApp = CreateObject("Outlook.Application")

    Set objNS = objApp.GetNamespace("MAPI")

    Set objFolder = objNS.Folders.Item(arrFolders(0))


    If Not objFolder Is Nothing Then

        For I = 1 To UBound(arrFolders)

            Set colFolders = objFolder.Folders

            Set objFolder = Nothing

            Set objFolder = colFolders.Item(arrFolders(I))

            If arrFolders(I) = sFN Then

                objFolder.ShowAsOutlookAB = True


                Set objDL = objFolder.Items.Add(Outlook.OlItemType.olDistributionListItem)

                objDL.DLName = sFN


                adoCon.ConnectionString = "driver={SQL Server};" & _

                               "server=" & sSrv & ";Trusted_Connection=Yes;database=RAS"

                If sFN = "Associates" Then

                    sOpenSTR = "'ASC'"


                    sOpenSTR = "'CR'"

                End If

                adoRS.Open "Exec RES_AddAssocOrCRS " & sOpenSTR, adoCon.ConnectionString, adOpenDynamic, adLockPessimistic

                If Not (adoRS.BOF And adoRS.EOF) Then


                    Do Until adoRS.EOF

                        Set objCI = objFolder.Items.Add(olContactItem)

                        With objCI

                            .FirstName = adoRS("Fname")

                            .LastName = adoRS("Lname")

                            .Email1Address = adoRS("Email_Address")

                            .Email1DisplayName = adoRS("Display_Name")

                            .SelectedMailingAddress = olHome

                            .Categories = sFN


                        End With


                        Set myMailItem = objApp.CreateItem(Outlook.OlItemType.olMailItem)

                        Set oRecipients = myMailItem.Recipients 'Resolve the current user just added by Email Address

                        oRecipients.Add (objCI.Email1Address) 'Add Contact Item as a recipient



                        objDL.AddMembers (oRecipients) ' if count > 109 - it bombs



                        Set objCI = Nothing 'in next Iteration

                        Set oRecipients = Nothing 'Empty Contact Item and Recipient Item




                End If

            End If


            If objFolder Is Nothing Then

                Exit For

            End If

        Next I

    End If


    Set objDL = Nothing


    Set adoRS = Nothing

    Close adoCon

    Set adoCon = Nothing

    Set objFolder = Nothing

    Set colFolders = Nothing

    Set objNS = Nothing

    Set objApp = Nothing

    Unload Me

End Sub

Open in new window

LVL 83

Expert Comment

ID: 24105695
One options is to run a loop and add all the emails to the Recipients collection of the Message rather than first adding to the group.

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now