开发者

ususual problem with system.net.mail attachment

开发者 https://www.devze.com 2023-02-06 18:53 出处:网络
I have an ususual problem with my attachment memorystream on a VB.net project. I am calling a shared member (SendMail) passing in a customer object and a memorystream of a file to be sent as a开发者_如

I have an ususual problem with my attachment memorystream on a VB.net project. I am calling a shared member (SendMail) passing in a customer object and a memorystream of a file to be sent as a开发者_如何学运维n attachment. Within the "SendMail" it calls two separate functions to prepare one regular email and another for a digitally signed email. The problem I have is I recieve the digitally signed email perfect, however, in the regular email, the attachment is blank and the file name has appended (64 B). If I disable the part of the digitally signing the regular mails works fine. It looks like somewhere the memorystream is changed in the digital signing.

Here his how I make the calls

mailHelper.SendMail(cust, attachment)

withing the 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

If I try the following it works fine, however, is this the proper solution?

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


Assume you already fixed this given the delay in getting an answer but here is my attempt anyhow...

There are quite a few overloads to create an Attachment, none of them match the way you initialised the Attachment.

When creating an Attachment the second parameter needs to be either NULL or identify a valid MIME-type for your attachment.

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号