0

I'm attempting to attach documents contained within a Kentico page (version 7) but not all documents, only those selected in an asp checkboxlist. It would appear I have everything working except when I try to actually send the email, I get the following error:

An exception of type 'System.NullReferenceException' occurred in CMS.IO.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

The odd thing here is that when I debug it, the attachment collection shows the correct attachments. The content is there, the mime-type is set correctly. As far as I can tell, the attachments are all set correctly and everything that needs a value has a value.

Edit: It should be noted that the email sends without problem when there are no attachments added to the email object.

 Dim emailMsg As New CMS.EmailEngine.EmailMessage
    With emailMsg
        .From = WebConfigurationManager.AppSettings("SomeEmailAddress")
        .ReplyTo = .From
        .Recipients = txtTo.Text.Trim()
        .Subject = txtSubject.Text.Trim()
        .EmailFormat = CMS.EmailEngine.EmailFormatEnum.PlainText
        .PlainTextBody= txtMessage.Text.Trim()

        Dim rows As System.Data.DataRowCollection = GetAttachmentsList()

        For Each item As WebControls.ListItem In cblMyCheckboxList.Items
            If Not item.Selected Then
                Continue For
            End If
            'each of these items were selected. We now need to find that attachment by name in the GetAttachmentsList()
            'Difficulty level: the row collection is not enumerable...
            'in fact, nothing here is enumerable and none of it works with linq
            For Each rowItem As System.Data.DataRow In rows
                'filename is in item(1)
                If rowItem(1) = item.Value Then
                    'rowItem(5) is a byte[] array
                    'rowItem(4) is the mime-type
                    Dim attachmentStream As IO.MemoryStream = New IO.MemoryStream(rowItem(5), False)
                    Dim attachment As System.Net.Mail.Attachment = New Net.Mail.Attachment(attachmentStream, New System.Net.Mime.ContentType(rowItem(4)))
                    .Attachments.Add(attachment)
                End If
            Next
        Next
    End With
    CMS.EmailEngine.EmailSender.SendEmail(emailMsg) 'error happens here

And GetAttachmentList() is defined as follows:

Private Function GetAttachmentsList() As System.Data.DataRowCollection
    'inspired from this help doc for Kentico 9
    'https://docs.kentico.com/display/API9/Attachments

    Dim tree As CMS.DocumentEngine.TreeProvider = New CMS.DocumentEngine.TreeProvider()
    'Dim page As CMS.DocumentEngine.TreeNode = tree.SelectSingleNode(0000, "en-us", "PageClassAsListed")
    Dim params As CMS.DocumentEngine.NodeSelectionParameters = New CMS.DocumentEngine.NodeSelectionParameters()
    With params
        .AliasPath = "/MyAliasPath"
        .CultureCode = "en-us"
        .ClassNames = "MyClassName"
        .CombineWithDefaultCulture = False
    End With
    Dim page As CMS.DocumentEngine.TreeNode = tree.SelectSingleNode(params)
    Return CMS.DocumentEngine.DocumentHelper.GetAttachments(page, "", "", True, tree).Tables(0).Rows
End Function

Does anyone have any idea what the compiler is complaining about?

6
  • Did you check all three parts of CMS.EmailEngine.EmailSender? Feb 3, 2016 at 16:37
  • It would appear I'm missing some knowledge. All three parts? Feb 3, 2016 at 16:38
  • Each of CMS AndAlso EmailEngine AndAlso EmailSender appear to be objects, if any one of them is Nothing, you'll get the exception. You cant reference an element downstream from Nothing Feb 3, 2016 at 16:40
  • That's a namespace. It is properly registered and the email correctly sends when there are no attachments. Feb 3, 2016 at 16:41
  • You shouldn't index columns by numbers, the order of returned columns is not under your control and this code may easily fail later. Use indexing by column names instead.Can you also provide some information what particular data is in that DataRow at the time it fails? Feb 4, 2016 at 9:13

1 Answer 1

0

I just noticed that while I was looking to see that the content of the attachment was complete, it probably wants to complain about the attachment if it has no name.

Dim attachment As System.Net.Mail.Attachment = New Net.Mail.Attachment(attachmentStream, New System.Net.Mime.ContentType(rowItem(4)))
attachment.Name = rowItem(1)
.Attachments.Add(attachment)

And the error goes away.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.