system.net.mail附件的异常问题

时间:2011-01-25 01:07:44

标签: memorystream system.net.mail

我在VB.net项目中的附件内存流有一个常见的问题。我正在调用一个共享成员(SendMail)传入一个客户对象和一个文件的内存流作为附件发送。在“SendMail”中,它调用两个单独的函数来准备一个常规电子邮件,另一个函数用于数字签名的电子邮件。我遇到的问题是我收到完美的数字签名电子邮件,但是,在常规电子邮件中,附件是空白的,文件名已附加(64 B)。如果我禁用数字签名的部分常规邮件工作正常。它似乎在数字签名中改变了内存流。

这是他如何拨打电话

mailHelper.SendMail(cust, attachment)

使用mailHelper CLASS

Public Shared Sub SendEmail(ByVal cust As Customer, ByVal attachment As MemoryStream)

  Dim messages As New List(Of MailMessage)
  messages.Add(CreateUnSignedMail(cust,attachment)
  messages.Add(CreateSignedMail(cust,attachment)
  SendSMTPMail(messages)
End Sub

Private Shared Function CreateUnSignedMail(ByVal cust As Customer, ByVal attachment As MemoryStream) As MailMessage

Dim eMail As New MailMessage()
        With eMail
            .//Normal properties set (like to, from etc)

            .Attachments.Add(New Attachment(attachment, "someFilename.doc")
        End With
        Return eMail
End Function

Private Shared Function CreateSignedMail(ByVal cust As Customer, ByVal attachment As MemoryStream) As MailMessage

Dim eMail As New SecureMailMessage()
        With eMail
            .//Normal properties set (like to, from etc)

            .Attachments.Add(New  SecureAttachment(attachment, "someFilename.doc")
        End With
        Return eMail
End Function


Private Shared Sub SendSMTPMail(ByVal messages As List(Of System.Net.Mail.MailMessage))
        Dim smtp As New SmtpClient("myServer")
        Try
            With smtp
                .//Additional properties set

                For Each email In messages
                    .Send(email)
                Next

            End With

        Catch ex As Exception
            //Log error to file.
            Logger.Log.Error("SMTP Error", ex)
            Throw
        End Try
End Sub

如果我尝试以下方法,它可以正常工作,但是,这是正确的解决方案吗?

messages.Add(SendUnSignedMail(cust, New MemoryStream(attachment.ToArray())))
messages.Add(SendSignedMail(cust, New MemoryStream(attachment.ToArray())))

1 个答案:

答案 0 :(得分:0)

假设你已经解决了这个问题,因为得到了答案的延迟,但无论如何这是我的尝试......

创建附件有很多重载,它们都没有与初始化附件的方式相匹配。

创建附件时,第二个参数必须为NULL或标识附件的有效MIME类型。

希望这有帮助。