Click here to Skip to main content
Click here to Skip to main content

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

, 28 Jun 2007 CPOL
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.

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:

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:

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)

Share

About the Author

Fabricio Miranda
Web Developer
Brazil Brazil
No Biography provided

Comments and Discussions

 
QuestionNot working for background-image? PinmemberMCS_Erik12-Oct-08 14:30 

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.

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