Click here to Skip to main content
15,885,278 members
Articles
Article
(untagged)

Use images from an sql server in html emails

Rate me:
Please Sign up or sign in to vote.
1.20/5 (4 votes)
3 Oct 2005CPOL 14.9K   6   1
Send emails in HTML format using images directly from a Data Base

Introduction

When I needed to use dynamic images in my web site I followed Gayan Mudalige article on "Displaying multiple dynamic images in a WebForm". Using this very clever method I build a completly dinamic web site for one of my clientes.

When my client came back and asked me to do the same but with HTML Emails, i didn't knew what to do. Actually I was about to to tell him it had to be done trought a third party extension when I remembered gayan tecnic for dinamic images.

Basically, use and <img> block in the html code you send with the email. In the "src" property you put the url of a page in your ASP.NET application.

Ex: src="http://mypage.com/images/dinamicImages.aspx?id=5"

This Page contains a couple of lines of code that loads the image from the data base and puts in an indivudual web page which becomes an image, and the image url.

'Use this Code in the individual blank aspx page

VB
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    Handles MyBase.Load 'Check for Query String
    If Not Request.QueryString.Item("Id") Is Nothing Then
        Dim id As Integer 'Get QueryString value for "Id"
        id = Request.QueryString.Item("Id")
        If id > 0 Then 'Load the Image into the aspx page
            Me.ShowImage(id)
        End If
    End If
'Sub that Load the specific image
    Public sub ShowImage(ByVal id As Integer)
        'Class that holds connections to the Data Base and controls data access
        Dim foto As New ProviderProductosFotografias
        Dim ds As New DataSet
        'Function that gets the image from the database and holds it in a data set
        'Function that holds re-usable methods
        Dim funciones As New Functions
        If funciones.DataSetHasTablesAndRows(ds) Then
            'Read the image stored in the dataset and holds it in an array of bytes
            'Image is induced into the aspx page and serves as a url link.
            Response.BinaryWrite(buffer)
            Dim buffer() As Byte = ds.Tables(0).Rows(0).Item(0)
        End If
        'At last you must direct the image in the HTML document to the aspx page:
        
        <IMG style="WIDTH: 224px; HEIGHT: 160px" height="160" alt=""
        src="http://www.mypage.com/ShopOnLine/UI/Pages/Modulos/ImageMultiDb.aspx?id=5"
        >http://www.mypage.com/ShopOnLine/UI/Pages/Modulos/ImageMultiDb.aspx?id=5"
        'This example assumes images will be used only once in each email, so 
        'there is no need for storing for repetitive use.


     End Sub
     ds = foto.getData("Select imagen from fotografias where fotografiaid=" & id)
End Sub

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Costa Rica Costa Rica
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralTags Pin
KiwiPiet3-Oct-05 16:30
KiwiPiet3-Oct-05 16:30 
Check your tags, the formatting of your article is going to give you low ratings. Sigh | :sigh:

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.