How to retrive Image from DB Using and handler and Embed that image in Email, so that in future if image in DB changes so it should not affect to image which has been Mailed earlier

Recommended Answers

All 2 Replies

I have embedded images in a mail message using this technique. Feel free to adapt this example as you see fit. You can use this code in its own class, create a Subroutine and pass it variables so that you can send messages from different pages in your web application.

 Try
            Dim mail As New MailMessage()
            Dim mailSender As New SmtpClient("smtp.mailserver.com")

            Dim mailFromDisplay As String

            mailFromDisplay = "some variable that contains nice display format"       
            mail.From = New MailAddress(mailFrom, mailFromDisplay)
            mail.To.Add(mailTo-variable)
            mail.CC.Add(mailCC-varible)
            mail.Bcc.Add(mailBcc-varible)

            'Do you have an attachment?  check your mailFile variable
            If mailFile <> "none" Then
              Dim objFile As String = "" & HttpContext.Current.Server.MapPath(mailFile)
              mail.Attachments.Add(New System.Net.Mail.Attachment(objFile))
            End If

            mail.Subject = subject-variable
            mail.IsBodyHtml = True

            Dim body As New StringBuilder

            body.Append("<img src=""cid:image1""/>")
            body.Append(message)

            Dim contentId As String = "image1"
            Dim filename As String = HttpContext.Current.Server.MapPath("\images\myImage.gif")

            Dim msgAlternateView As AlternateView = AlternateView.CreateAlternateViewFromString(body.ToString, Nothing, MediaTypeNames.Text.Html)
            Dim linkedResource As LinkedResource = New LinkedResource(filename)
            linkedResource.ContentId = contentId
            msgAlternateView.LinkedResources.Add(linkedResource)
            mail.AlternateViews.Add(msgAlternateView)

            mailSender.Send(mail)
        Catch ex As Exception
            ' Do something about the error!
        End Try

Hi JorgeM,

Your code is perfect but i want that image should come from image Handler i.e .ashx file..
Specifying an image name will always send the same image.

I am using this for the internal mails i.e outlook. The main issue is that if i send image using .ashx file than on receiving the email user has to download the image in order to view the image in this case if server is closed image wont be visible and if i directly send the image as you specified than it will be display directly but not different everytime.

So in short i want differnt image everytime using imageHandler.ashx and it should visible directly to the user without downloading..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.