我正在尝试附加 Kentico 页面(版本 7(中包含的文档,但不是所有文档,仅在 asp 复选框列表中选择的文档。看起来我一切正常,除了当我尝试实际发送电子邮件时,我收到以下错误:
CMS 中发生类型为"System.NullReferenceException"的异常。IO.dll但未在用户代码中处理
其他信息:对象引用未设置为对象的实例。
奇怪的是,当我调试它时,附件集合会显示正确的附件。内容在那里,哑剧类型设置正确。据我所知,附件都设置正确,所有需要值的东西都有一个值。
编辑:应该注意的是,当没有附件添加到电子邮件对象时,电子邮件发送没有问题。
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
有谁知道编译器在抱怨什么?
我刚刚注意到,当我查看附件的内容是否完整时,如果附件没有名称,它可能想抱怨附件。
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)
错误消失了。