Embed programatically created jpeg into HTML email?



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,


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",
mSmtpClient.Credentials = SMTPUserInfo
mSmtpClient.Port = 587
mSmtpClient.Host = "site2.com"
End If
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"

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

<script runat="server">

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

Response.ContentType = "image/jpeg"
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

' 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.

' Send the output to the client.
End Sub


<form id="form1" runat="server">


Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question