i know the mail attachment doesn't allow stream as an attachment, i found a work around...
Imports System.IO
Imports System.Text
Imports System.Web.Mail
Public Class IAMailAttachment
Inherits MailAttachment
''' i had to provide the original set of constructors,
''' because chances were someone might want to use file names
Public Sub New(ByVal fileName As String)
MyBase.New(fileName)
End Sub
Public Sub New(ByVal fileName As String, ByVal encoding As MailEncoding)
MyBase.New(fileName, encoding)
End Sub
Public Sub New(ByVal fileData As Byte(), ByVal extension As String)
MyBase.New(createMailAttachmentFromBytes(fileData, extension))
End Sub
Public Sub New(ByVal ioStream As Stream, ByVal extension As String)
MyBase.New(createMailAttachmentFromStream(ioStream, extension))
End Sub
''' <summary>
''' shared method expects a stream, which it reads into a bytes, and then calls the createMailAttachment method to
''' actually create a temporary file
''' <summary>
Private Shared Function createMailAttachmentFromStream(ByVal ioStream As Stream, ByVal extension As String) As String
Dim bytesToRead(ioStream.Length - 1) As Byte
ioStream.Read(bytesToRead, 0, bytesToRead.Length)
ioStream.Close()
Return createMailAttachmentFromBytes(bytesToRead, extension)
End Function
''' <summary>
''' shared method expects byte array, and the extension of the file expected by the mail
''' it then creates a file under temporary folder of the user. [we can't use any other folder because we might not have access to it]
''' it then creates file, writes the bytes to it, and then make sure its close.
''' finally it returns the file name that was jus created.
''' <summary>
Private Shared Function createMailAttachmentFromBytes(ByVal fileData As Byte(), ByVal extension As String)
Dim fileToCreate = New StringBuilder(System.Environment.GetEnvironmentVariable("TEMP")). _
Append("\attachment.").Append(extension).ToString()
Dim streamToWrite As FileStream = File.Create(fileToCreate, fileData.Length)
streamToWrite.Write(fileData, 0, fileData.Length)
streamToWrite.Close()
Return fileToCreate
End Function
End Class
Sai aka Dj Vibe [ djvibe_alb ( at ) hotmail dot com ],
2005-05-19 10:47:11
#