none
CRM 2011 SDK Email to multiple cc or bcc parties

    Question

  • I originally accidentally posted this in the generic CRM forum.  Sorry.

    I have a function that will generate and send an email with attachments (either new or re-used).  Based on the situation there may be one or more cc and/or bcc recipients.  They are sent to the routine in collections: ccAdds and bccAdds., under the emRec object.

    The data extracts correctly and successfully creates new activity party records, and if there's only one in each group, all is well.

    But, there may be multiple cc or bcc addresses to be sent to.

    The number might range from none to 20 in the collection, so I am trying to loop through it to add the cc's.

    As you might expect, my code only successfully adds the last one.

    How can I add a variable number of activty party records to the email?

     

    Here's the routine in question:

     Public Function CreateEmailwithAtts(ByVal serverConfig As ServerConnection.Configuration, ByVal emRec As emailData) As String
            Dim retString As String = ""
            Dim boo As Boolean = False
            Dim emTempID As String = ""
            Dim eType As String = ""
            Dim _emailId As Guid
            Dim sendEmailResult As String = ""
    
            Try
                Using _serviceProxy
                    _service = _serviceProxy
                    Dim context As New OrganizationServiceContext(_service)
                    context.MergeOption = MergeOption.NoTracking
    
                    Dim usrGuid As Guid = New Guid(myUserID)
                    Dim toGuid As Guid = New Guid(emRec.toID)           'acctID or contactID
                    Dim toLName As String = LCase(emRec.toIDType)       'Account or contact
                    Dim regID As Guid = New Guid(emRec.regardingID)     'acctID or contactID
    
                    Dim fromParty As ActivityParty = New ActivityParty With {.PartyId = New EntityReference(SystemUser.EntityLogicalName, usrGuid)}
    
                    Dim toParty As ActivityParty = New ActivityParty With {.PartyId = New EntityReference(toLName, toGuid)}
    
                    Dim email As Email = New Email With {
                                            .To = New ActivityParty() {toParty},
                                            .From = New ActivityParty() {fromParty},
                                            .Subject = emRec.subject,
                                            .Description = emRec.description,
                                            .DirectionCode = emRec.directionCode}
    
                    Dim emAdd As SelectedCC
                    For Each emAdd In emRec.ccAdds
                        Select Case emAdd.entType
                            Case "acct"
                                eType = "account"
                            Case "cont", "tech"
                                eType = "contact"
                        End Select
                        Dim ccParty = New ActivityParty With {
                        .PartyId = New EntityReference(eType, New Guid(emAdd.entID))
                        }
                        email.Cc = New ActivityParty() {ccParty}
                    Next
    
                    Dim bccAdd As SelectedBCC
                    For Each bccAdd In emRec.bccAdds
                        Select Case bccAdd.entType
                            Case "acct"
                                eType = "account"
                            Case "cont", "tech"
                                eType = "contact"
                        End Select
                        Dim bccParty = New ActivityParty With {
                        .PartyId = New EntityReference(eType, New Guid(bccAdd.entID))
                        }
                        email.Bcc = New ActivityParty() {bccParty}
                    Next
    
                    _emailId = _serviceProxy.Create(email)
    
                    'add any attachments here
                    If emRec.attachments.Count > 0 Then
                        Dim attachmentCount As Integer = 0
    
                        For Each emAtt As emailAttach In emRec.attachments
                            attachmentCount += 1
                            If emAtt.attID <> "" Then  'reuse an attachment
                                Dim retrievedAttachment As ActivityMimeAttachment = DirectCast(_serviceProxy.Retrieve(ActivityMimeAttachment.EntityLogicalName, New Guid(emAtt.attID), New ColumnSet(True)), ActivityMimeAttachment)
                                Dim reUseAttachment As ActivityMimeAttachment = New ActivityMimeAttachment
    
                                With reUseAttachment
                                    .ObjectId = New EntityReference(email.EntityLogicalName, _emailId)
                                    .ObjectTypeCode = email.EntityLogicalName
                                    .Subject = emAtt.subject
                                    .AttachmentId = retrievedAttachment.AttachmentId
                                    .AttachmentNumber = attachmentCount
                                End With
                                Try
                                    _service.Create(reUseAttachment)
    
                                Catch ex As Exception 'SoapException
                                    Dim errormsg As String = [String].Format("Write Error for Email Attachment {0} error {1}", emAtt.pathname, ex.InnerException)
                                    'WriteErrorFile(errormsg)
                                End Try
                            Else
    
                                Dim emailAttachment = New ActivityMimeAttachment
                                With emailAttachment
                                    .ActivityId = New EntityReference("email", _emailId)
                                    .FileName = emAtt.pathname.Substring(emAtt.pathname.LastIndexOf("\"c) + 1)
                                    .Subject = emAtt.subject
                                    .MimeType = emAtt.type
                                    .AttachmentNumber = attachmentCount '- 1
                                End With
    
                                If File.Exists(emAtt.pathname) Then
                                    Dim fileContents As Byte() = File.ReadAllBytes(emAtt.pathname)
                                    emailAttachment.Body = Convert.ToBase64String(fileContents)
                                Else
                                    ' Handle condition where file doesn't exist
                                    emailAttachment.Body = "Attachment File Not Found."
                                End If
    
                                Try
                                    _service.Create(emailAttachment)
    
                                Catch ex As Exception 'SoapException
                                    Dim errormsg As String = [String].Format("Write Error for Email Attachment {0} error {1}", emAtt.pathname, ex.InnerException)
                                End Try
                            End If
                        Next
                    End If
    
                    If Not _emailId.Equals(Guid.Empty) Then
                        Dim sendEmailreq As SendEmailRequest = New SendEmailRequest With {.EmailId = _emailId, .TrackingToken = "", .IssueSend = True}
    
                        Dim sendEmailresp As SendEmailResponse = CType(_serviceProxy.Execute(sendEmailreq), SendEmailResponse)
                    End If
    
                    '' Verify that the e-mail has been created
                    If Not _emailId.Equals(Guid.Empty) Then
                        Return _emailId.ToString
                    End If
    
                End Using
                ' Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            Catch fe As FaultException(Of Global.Microsoft.Xrm.Sdk.OrganizationServiceFault)
                Return ""
            End Try
            Return retString
        End Function
    

     

    Thanks for any ideas.

    Lon Weekly

    Thursday, February 02, 2012 11:35 PM

Answers

  • I actually replied to your thread in the General Forum.  The CC/BCC is an array/collection of Activity Party records.  The way your loop is written the very last record is the one that get's assigned.  You need to setup an EntityCollection for the Cc Activity Parties before your loop starts.  At the end of your loop add the ActivityParty object you create to your collection.  Then when you're loop is done set the CC value to the EntityCollection.

    Jeremy

    • Marked as answer by lweekly Friday, February 03, 2012 6:52 AM
    Friday, February 03, 2012 12:52 AM

All replies

  • I actually replied to your thread in the General Forum.  The CC/BCC is an array/collection of Activity Party records.  The way your loop is written the very last record is the one that get's assigned.  You need to setup an EntityCollection for the Cc Activity Parties before your loop starts.  At the end of your loop add the ActivityParty object you create to your collection.  Then when you're loop is done set the CC value to the EntityCollection.

    Jeremy

    • Marked as answer by lweekly Friday, February 03, 2012 6:52 AM
    Friday, February 03, 2012 12:52 AM
  • Thanks. I posted my final working code in the CRM General forum under this similar topic.
    Friday, February 03, 2012 3:43 PM