Embed programatically created jpeg into HTML email?

Discussion in 'ASP .Net' started by Mark123, Sep 2, 2008.

  1. Mark123

    Mark123 Guest

    I have two pieces of VB.NET code below:

    a) Sends an email
    b) Returns a programmatically created jpeg image to a webpage.

    How can I embed the programmatically created jpeg into an HTML formatted
    email (without saving the jpeg to disk)?

    Code a)
    ======

    Imports Microsoft.VisualBasic
    Imports System.Net.Mail
    Imports System.Net

    Public Class sfSendMail

    Shared Function fSendMail( _
    ByVal strTo As String, _
    ByVal strFrom As String, _
    ByVal strSubject As String, _
    ByVal strBody As String, _
    Optional ByVal strCC As String = "", _
    Optional ByVal strBCC As String = "", _
    Optional ByVal strBodyHTML As String = "") As String

    fSendMail = ""

    'Send mail
    Dim mMailMessage As New MailMessage()
    Dim strMediaType As String
    Dim avPlainText As AlternateView
    Dim avHTMLContent As AlternateView

    mMailMessage.From = New MailAddress(strFrom)
    mMailMessage.To.Add(New MailAddress(strTo))

    If strBCC <> "" Then
    mMailMessage.Bcc.Add(New MailAddress(strBCC))
    End If

    If strCC <> "" Then
    mMailMessage.CC.Add(New MailAddress(strCC))
    End If

    mMailMessage.Subject = strSubject

    strMediaType = "text/plain"
    avPlainText = AlternateView.CreateAlternateViewFromString(strBody,
    Nothing, strMediaType)

    strMediaType = "text/html"
    avHTMLContent =
    AlternateView.CreateAlternateViewFromString(strBodyHTML, Nothing,
    strMediaType)

    mMailMessage.AlternateViews.Add(avPlainText)
    mMailMessage.AlternateViews.Add(avHTMLContent)


    Try
    Dim mSmtpClient As New SmtpClient()
    If Left(SharedFunctions.fGetConnectionString, Len("Data
    Source=MyConnection")) = "Data Source=MyConnection" Then
    mSmtpClient.Host = "smtp.site1.com"
    Dim SMTPUserInfo As New NetworkCredential("me",
    "mypassword")
    mSmtpClient.Credentials = SMTPUserInfo
    mSmtpClient.Port = 587
    Else
    mSmtpClient.Host = "site2.com"
    End If
    mSmtpClient.Send(mMailMessage)
    fSendMail = "True"
    Catch exc As Exception
    fSendMail = "Email send failure: " + exc.ToString()
    End Try

    End Function


    End Class


    Code b)
    ======

    <%@ Page Language="VB" %>
    <%@ import Namespace="System.Drawing" %>
    <%@ import Namespace="System.Drawing.Imaging" %>
    <%@ import Namespace="System.Drawing.Drawing2D" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html>
    <head id="Head1" runat="server" />

    <script runat="server">

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    Response.ContentType = "image/jpeg"
    Response.Clear()
    Response.BufferOutput = True

    ' Create a font style.
    Dim rectangleFont10 As New Font("Arial", 10, FontStyle.Bold)
    Dim rectangleFont32 As New Font("Arial", 32, FontStyle.Bold)
    Dim rectangleFont7 As New Font("Arial", 7, FontStyle.Bold)

    ' Create integer variables.
    Dim height As Integer = 150
    Dim width As Integer = 150

    ' Create a bitmap and use it to create a graphics object.
    Dim bmp As New Bitmap(width, height, PixelFormat.Format24bppRgb)
    Dim g As Graphics = Graphics.FromImage(bmp)

    g.SmoothingMode = SmoothingMode.AntiAlias
    g.Clear(Color.LightGray)


    ' Use the Graphics object to draw three rectangles.
    g.DrawRectangle(Pens.White, 1, 1, width - 3, height - 3)
    g.DrawString("My Text 1", rectangleFont7, SystemBrushes.WindowText,
    New PointF(5, 15))
    g.DrawString("My Text 2", rectangleFont32, SystemBrushes.WindowText,
    New PointF(40, 35))
    g.DrawString("My Text 3", rectangleFont10, SystemBrushes.WindowText,
    New PointF(40, 90))
    g.DrawString("My Text 4", rectangleFont7, SystemBrushes.WindowText,
    New PointF(35, 125))


    ' Save the bitmap to the response stream and
    ' convert it to JPEG format.
    bmp.Save(Response.OutputStream, ImageFormat.Jpeg)

    ' Release memory used by the Graphics object and the bitmap.
    g.Dispose()
    bmp.Dispose()

    ' Send the output to the client.
    Response.Flush()
    End Sub

    </script>

    <body>
    <form id="form1" runat="server">
    </form>
    </body>
    </html>
     
    Mark123, Sep 2, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Helixpoint

    embed an image in an HTML email.

    Helixpoint, Jul 14, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    543
    J. Ptak
    Jul 16, 2003
  2. Olav Tollefsen

    How to embed dynamic generated image into HTML?

    Olav Tollefsen, Mar 1, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,931
    Olav Tollefsen
    Mar 1, 2004
  3. John
    Replies:
    2
    Views:
    1,134
  4. lovaspillando
    Replies:
    0
    Views:
    1,031
    lovaspillando
    Aug 26, 2007
  5. Ivan Alameda Carballo
    Replies:
    0
    Views:
    492
    Ivan Alameda Carballo
    Aug 26, 2007
Loading...

Share This Page