Send e-mail with attachment from database image column (sql server 2k5)

Soooo...I needed to get files out of my SQL Server table (which has the files in an image column) and attach them to an email to send out.  I honestly thought this would be a no-brainer but turned out it was pretty finicky.  Anyway...the code to so is below just in case anyone is wondering.


     Partial Class _Default

         Inherits System.Web.UI.Page

     

         Function SendEmail()

     

             'Get your data (filename, content type, image column) from the db

     

             Dim dt As DataTable = GetYourStuff()

            If dt.Rows.Count <= 0 Then Return Nothing

    

            Dim msg As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()

            msg.Subject = "Sample Message"

            msg.To.Add(New System.Net.Mail.MailAddress("Receiver@yourDomain.com"))

    

           Dim fromadx As New System.Net.Mail.MailAddress("Sender@yourDomain.com", "Sender Name")
           msg.From = fromadx

            msg.IsBodyHtml = False

            msg.Body = "Sample body..."

    

            Dim strMem As System.IO.MemoryStream = New System.IO.MemoryStream(CType(dt.Rows(0)("ImageColumn"), Byte()))

            Dim strWriter As System.IO.StreamWriter = New System.IO.StreamWriter(strMem)

    

            strWriter.Flush()

            'this is very important..wont work without it

            strMem.Position = 0

            'Filename and content type are hardcoded here, but I assume you get them from GetYourStuff() above

    

            Dim attachment As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(strMem, "myFile.txt", "text/plain")

            msg.Attachments.Add(attachment)

    

            Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(ConfigurationManager.AppSettings.Item("smtpserver"), 25)

            smtpClient.Send(msg)

   

        End Function

    End Class

 

kick it on DotNetKicks.com

Print | posted @ Thursday, January 31, 2008 4:44 PM

Comments on this entry:

Gravatar # re: Send e-mail with attachment from database image column (sql server 2k5)
by Kevin at 1/20/2009 1:07 PM

Great Stuff!
Gravatar # re: Send e-mail with attachment from database image column (sql server 2k5)
by sandeep at 2/12/2009 7:55 PM

Great stuff but how to send multiple attachments .I mean if I want to send 5 attachments out of 10 and if i get those 5 from data base how should I loop????Is there any way I can an array of attachments??
Gravatar # re: Send e-mail with attachment from database image column (sql server 2k5)
by SanjayU at 2/13/2009 12:52 PM

Sandeep,
I have responded with code via e-mail. Please let me know if that does the trick.

For others, in order to attach multiple files you must loop through the files you want to attach you must instantiate a new object of type System.Net.Mail.Attachment. Then call System.Net.Mail.MailMessage.Attach for each file you need to attach.

If you need code, feel free to contact me and I'll e-mail it over...I'll post it up if I see more than a couple of requests...

HTH.

Cheers,
Sanjay
Gravatar # re: Send e-mail with attachment from database image column (sql server 2k5)
by Jose-Miguel Colon at 6/15/2009 2:03 PM

I am a asp.net beginner. I will like to see the code to attach multiple files in a database.
My Database has the info:
FileID, FileName, FileType,FileData,Category

I want to attach all the files that belong to same category. like: select * from FileList where category=1


Thanks
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 
Twitter