Click here to Skip to main content
15,868,141 members
Articles / Web Development / HTML

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

Rate me:
Please Sign up or sign in to vote.
2.67/5 (2 votes)
28 Jun 2007CPOL 22.4K   21   1
This article describes, and provides a short (not optimized) function that will convert a normal HTML input to an e-mail with embedded resources.

Introduction

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:

VB
Public Function convertToEmbedResource(ByVal emailHtml$) As AlternateView

    'This is the website where the resources are located
    Dim webSiteUrl$ = "http://www.thewebsite.com/myresources/"
    
    ' The first regex finds all the url/src tags.
    Dim matchesCol As MatchCollection = Regex.Matches(emailHtml, _
                   "url\(['|\""]+.*['|\""]\)|src=[""|'][^""']+[""|']")

    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 & ")")
        Else
            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
        resCol.LinkedResources.Add(theResource)
        
        ' Next resource ID
        resId = resId + 1

    Next
    
    ' 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
        finalEmail.LinkedResources.Add(transferResource)
    Next    
    
    return finalEmail

End Function

Sending the E-mail

Here is the code to send an email:

VB
Dim mail As New MailMessage()
mail.From = New MailAddress("me@me.com")
mail.To.Add("me@me.com")
mail.Subject = " Embed Resources "

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

License

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


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

Comments and Discussions

 
QuestionNot working for background-image? Pin
MCS_Erik12-Oct-08 13:30
MCS_Erik12-Oct-08 13: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.