system.net.mail 附件的常见问题
我在 VB.net 项目上的附件内存流遇到了常见问题。我正在调用一个共享成员 (SendMail),传入一个客户对象和一个要作为附件发送的文件的内存流。在“SendMail”中,它调用两个单独的函数来准备一封常规电子邮件,另一个用于准备数字签名电子邮件。我遇到的问题是我收到了完美的数字签名电子邮件,但是,在常规电子邮件中,附件为空白,并且已附加文件名 (64 B)。如果我禁用数字签名的部分,则常规邮件可以正常工作。看起来内存流在数字签名中的某个地方发生了变化。
进行调用
mailHelper.SendMail(cust, attachment)
这是我如何使用 mailHelper 类
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())))
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 an 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())))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您已经解决了这个问题,因为得到答案的延迟,但无论如何,这是我的尝试...
有很多重载来创建附件,它们都不符合您初始化附件的方式。
创建附件时,第二个参数必须为 NULL 或标识附件的有效 MIME 类型。
希望这有帮助。
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.