Click here to Skip to main content
Licence CPOL
First Posted 3 Oct 2005
Views 6,209
Bookmarked 6 times

Use images from an sql server in html emails

By | 3 Oct 2005 | Article
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

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)

About the Author

monillo1a



Costa Rica Costa Rica

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralTags Pinmembermwdiablo16:30 3 Oct '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 3 Oct 2005
Article Copyright 2005 by monillo1a
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid