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?
CMS.EmailEngine.EmailSender
?CMS
AndAlsoEmailEngine
AndAlsoEmailSender
appear to be objects, if any one of them is Nothing, you'll get the exception. You cant reference an element downstream from Nothing