65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.67/5 (2 votes)

Jun 28, 2007

CPOL
viewsIcon

22660

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)