Click here to Skip to main content
13,344,287 members (51,113 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 28 Jun 2007

Convert a normal e-mail to an embedded resource e-mail

, 28 Jun 2007
Rate this:
Please Sign up or sign in to vote.
This article describes, and provides a short (not optimized) function that will convert a normal HTML input to an e-mail with embedded resources.


Hello all, this is my first article ^^. I did this little function that helped me out at the job, and I wanted to share it. The idea is to convert a normal HTML input to a function that will return an AlternateView object that can be used to send an e-mail using the VB/ASP.NET mail objects.

Well, the function is not at all optmized, and uses a little of REGEX. And by the way, I am new to REGEX too, so, any changes/optimizations required, please let me know.

Using the Code

It's a simple function that finds all image/CSS/JS (finds URL, src tags) resources and link them to the e-mail.

Here is the code:

Public Function convertToEmbedResource(ByVal emailHtml$) As AlternateView

    'This is the website where the resources are located
    Dim webSiteUrl$ = ""
    ' The first regex finds all the url/src tags.
    Dim matchesCol As MatchCollection = Regex.Matches(emailHtml, _

    Dim normalRes As Match

    ' I didnt knew how to declare a new LinkedResourceCol so i did this :
    Dim resCol As AlternateView = _
        AlternateView.CreateAlternateViewFromString("", _
        Nothing, "text/html")
    Dim resId% = 0
    ' Between the findings
    For Each normalRes In matchesCol
        Dim resPath$
        ' Replace it for the new content ID that will be embeded
        If Left(normalRes.Value, 3) = "url" Then
            emailHtml = emailHtml.Replace(normalRes.Value, _
                 "url(cid:EmbedRes_" & resId & ")")
            emailHtml = emailHtml.Replace(normalRes.Value, _
                 "src=""cid:EmbedRes_" & resId & """")
        End If
        ' Clean the path

        resPath = Regex.Replace(normalRes.Value, "url\(['|\""]", "")
        resPath = Regex.Replace(resPath, "src=['|\""]", "")

        resPath = Regex.Replace(resPath, "['|\""]\)", _
                  "").Replace(webSiteUrl, "").Replace("""", "")

        ' Map it on the server
        resPath = Server.MapPath(resPath)

        ' Embed the resource
        Dim theResource As LinkedResource = New LinkedResource(resPath)
        theResource.ContentId = "EmbedRes_" & resId
        ' Next resource ID
        resId = resId + 1

    ' Create our final object
    Dim finalEmail As AlternateView = _
        Net.Mail.AlternateView.CreateAlternateViewFromString(emailHtml, _
        Nothing, "text/html")
    Dim transferResource As LinkedResource

    ' And transfer all the added resources to the output object
    For Each transferResource In resCol.LinkedResources
    return finalEmail

End Function

Sending the E-mail

Here is the code to send an email:

Dim mail As New MailMessage()
mail.From = New MailAddress("")
mail.Subject = " Embed Resources "

mail.AlternateViews.Add(convertToEmbedResource(" MY HTML EMAIL "))
Dim smtp As New SmtpClient("")


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


About the Author

Fabricio Miranda
Web Developer
Brazil Brazil
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionNot working for background-image? Pin
MCS_Erik12-Oct-08 14:30
memberMCS_Erik12-Oct-08 14:30 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 28 Jun 2007
Article Copyright 2007 by Fabricio Miranda
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid