我试图附加Kentico页面(版本7)中包含的文档,但不是所有文档,只包括在asp复选框列表中选择的文档。看起来我有一切正常工作,除非我尝试实际发送电子邮件,我收到以下错误:
类型' System.NullReferenceException'的例外情况发生在CMS.IO.dll但未在用户代码中处理
附加信息:未将对象引用设置为对象的实例。
这里奇怪的是,当我调试它时,附件集合显示正确的附件。内容在那里,mime-type设置正确。据我所知,附件都设置正确,所有需要值的附件都有值。
编辑:应该注意的是,当电子邮件对象中没有添加附件时,电子邮件会毫无问题地发送。
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
GetAttachmentList()
的定义如下:
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
有没有人知道编译器在抱怨什么?
答案 0 :(得分:0)
我刚注意到,虽然我希望看到附件的内容已完成,但如果附件没有名称,可能会抱怨附件。
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)
错误就消失了。