K
Kevin Whitson
Greetings:
I have a .Net application that is re-distributing email that arrives in an exchange inbox to multiple recipients by converting the Outlook.MailItem to a System.Net.Mail.MailMessage object. In this process I'm trying to copy the attachments from the outlook message to the mailMessage... I've tried saving the attachment to the local HD via the Outlook SaveAsFile(strFile) method and then using the Attachments.Add(New System.Net.Mail.Attachment(strFile)) to append the attachment to the outgoing mailMessage. However, I was getting random "Cannot Save File" errors.
After a few weeks of tinkering, I shifted gears and decided to try Outlook Redemption because I can convert the attachment to a byte array. Below is the code that I am using. It works great for all kinds of attachments except when the attachment is a .msg file. When someone sends a email via outlook that contains an attachment that is a .msg type I get the following error...
System.ArgumentNullException: Buffer cannot be null. Parameter name: buffer at System.IO.MemoryStream..ctor(Byte[] buffer, Boolean writable) at System.IO.MemoryStream..ctor(Byte[] buffer) at EmailDistribution.Module3.CopyAttachments(MailItem& olSource, MailMessage& mailMessageDest)
Here is the code (note I'm passing the arguments ByRef)...
Sub CopyAttachments(ByRef olSource As Outlook.MailItem, ByRef mailMessageDest As System.Net.Mail.MailMessage)
Dim safeMsg As Redemption.SafeMailItem = New Redemption.SafeMailItem
Dim objAttach As Byte()
Dim ms As System.IO.MemoryStream
Dim streamAttachment As System.Net.Mail.Attachment
Try
safeMsg.Item = olSource
For i As Integer = 1 To safeMsg.Attachments.Count
objAttach = Nothing
objAttach = safeMsg.Attachments(i).AsArray
ms = New System.IO.MemoryStream(objAttach) '>>This is where it fails on .msg attachments.
streamAttachment = New System.Net.Mail.Attachment(ms, safeMsg.Attachments(i).FileName)
mailMessageDest.Attachments.Add(streamAttachment)
Next
Catch ex As System.Exception
Dim strAppendHtml As String = ""
ErrorHandler(ex, strAppendHtml)
End Try
safeMsg = Nothing
End Sub
Has anyone had experience with copying attachments from Outlook to System.Net.Mail.MailMessage without making a round trip to the hard disk and back?
Thanks
Kevin
I have a .Net application that is re-distributing email that arrives in an exchange inbox to multiple recipients by converting the Outlook.MailItem to a System.Net.Mail.MailMessage object. In this process I'm trying to copy the attachments from the outlook message to the mailMessage... I've tried saving the attachment to the local HD via the Outlook SaveAsFile(strFile) method and then using the Attachments.Add(New System.Net.Mail.Attachment(strFile)) to append the attachment to the outgoing mailMessage. However, I was getting random "Cannot Save File" errors.
After a few weeks of tinkering, I shifted gears and decided to try Outlook Redemption because I can convert the attachment to a byte array. Below is the code that I am using. It works great for all kinds of attachments except when the attachment is a .msg file. When someone sends a email via outlook that contains an attachment that is a .msg type I get the following error...
System.ArgumentNullException: Buffer cannot be null. Parameter name: buffer at System.IO.MemoryStream..ctor(Byte[] buffer, Boolean writable) at System.IO.MemoryStream..ctor(Byte[] buffer) at EmailDistribution.Module3.CopyAttachments(MailItem& olSource, MailMessage& mailMessageDest)
Here is the code (note I'm passing the arguments ByRef)...
Sub CopyAttachments(ByRef olSource As Outlook.MailItem, ByRef mailMessageDest As System.Net.Mail.MailMessage)
Dim safeMsg As Redemption.SafeMailItem = New Redemption.SafeMailItem
Dim objAttach As Byte()
Dim ms As System.IO.MemoryStream
Dim streamAttachment As System.Net.Mail.Attachment
Try
safeMsg.Item = olSource
For i As Integer = 1 To safeMsg.Attachments.Count
objAttach = Nothing
objAttach = safeMsg.Attachments(i).AsArray
ms = New System.IO.MemoryStream(objAttach) '>>This is where it fails on .msg attachments.
streamAttachment = New System.Net.Mail.Attachment(ms, safeMsg.Attachments(i).FileName)
mailMessageDest.Attachments.Add(streamAttachment)
Next
Catch ex As System.Exception
Dim strAppendHtml As String = ""
ErrorHandler(ex, strAppendHtml)
End Try
safeMsg = Nothing
End Sub
Has anyone had experience with copying attachments from Outlook to System.Net.Mail.MailMessage without making a round trip to the hard disk and back?
Thanks
Kevin