Click here to Skip to main content
15,884,388 members
Articles / Web Development / ASP.NET
Article

Sending email with an embedded image through ASP.NET

Rate me:
Please Sign up or sign in to vote.
2.63/5 (19 votes)
6 Mar 2007CPOL 146.7K   263   40   38
An article to send email from ASP.NET 2.0 by embedding an image in the content

Introduction

This article will help beginners to get a basic idea of mailing with an embedded image or object from ASP.NET 2.0 using SMTP Client.

Background

I wrote this code when one of my colleagues came up with a similar requirement of sending email from an application which will have an embedded image signature (not as an attachment).

Using the code

The code is very simple and pretty straightforward. As a prerequisite, you should have access to a mail server. Now create a MailMessage object and specify the sender and recipient address. Message Text is created as an AlternateView and a LinkedResource is pointed to the image or object you want to embed. Once you are done with this, just add the LinkedResource to the AlternateView and finally add the AlternateView to the MailMessage object text.

VB.NET
Dim msg As New MailMessage("sender mail address", "recipient mail address")
msg.Subject = "This is my first email through program"
msg.IsBodyHtml = True

Dim View As AlternateView
Dim resource As LinkedResource
Dim client As SmtpClient
Dim msgText As New StringBuilder

msgText.Append("Hi there,")
msgText.Append("Welcome to the new world programming.")
msgText.Append("Thanks")
msgText.Append("With regards,")
msgText.Append("")

'create an alternate view for your mail
View = AlternateView.CreateAlternateViewFromString(msgText.ToString(),
                                                Nothing, "text/html")

'link the resource to embed
resource = New LinkedResource((Server.MapPath("Images\007jvr.gif")))

'name the resource
resource.ContentId = "Image1"

'add the resource to the alternate view
View.LinkedResources.Add(resource)

'add the view to the message
msg.AlternateViews.Add(View)

client = New SmtpClient()
client.Host = "smtp.gmail.com" 'specify your smtp server name/ip here
'client.EnableSsl = True 
'enable this if your smtp server needs SSL to communicate
client.Credentials = New Net.NetworkCredential("username", "pwd")
client.Send(msg) 

Please rate the article and don't hesitate to write to me in case you have any doubts or concerns.

Please do check out my other articles which may help you out.

License

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


Written By
Software Developer (Senior) Microsoft Corporation
India India
I work for Microsoft on MS technologies for application development. My interests include .net, WCF, Azure, Windows Phone, ASP.net, SL, WCF, WPF and many more.

You can visit my site at http://www.jebarson.info

Follow me on twitter @jebarson007

Comments and Discussions

 
GeneralRe: I am coding in C#. Pin
jebarson29-Mar-07 20:54
jebarson29-Mar-07 20:54 
QuestionImage receive as .DAT file???? Pin
Sujagr821-Mar-07 0:48
Sujagr821-Mar-07 0:48 
AnswerRe: Image receive as .DAT file???? Pin
jebarson21-Mar-07 0:56
jebarson21-Mar-07 0:56 
GeneralRe: Image receive as .DAT file???? Pin
Sujagr821-Mar-07 1:37
Sujagr821-Mar-07 1:37 
GeneralRe: Image receive as .DAT file???? Pin
Sujagr821-Mar-07 21:02
Sujagr821-Mar-07 21:02 
GeneralRe: Image receive as .DAT file???? Pin
jebarson21-Mar-07 23:16
jebarson21-Mar-07 23:16 
GeneralRe: Image receive as .DAT file???? Pin
jebarson21-Mar-07 23:17
jebarson21-Mar-07 23:17 
GeneralSending Mail Pin
pratap55720-Mar-07 3:47
pratap55720-Mar-07 3:47 
Dear Sir,

i tried to connect to smtp.gmail.com through ping and IP address 64.233.183.109 as i got from net . But it was unable to connect . Could u pls help me in this regard?


pratap Das
SE(India)
GeneralRe: Sending Mail Pin
jebarson20-Mar-07 4:29
jebarson20-Mar-07 4:29 
GeneralSending Mail Pin
pratap55719-Mar-07 22:47
pratap55719-Mar-07 22:47 
GeneralRe: Sending Mail Pin
jebarson19-Mar-07 23:01
jebarson19-Mar-07 23:01 
QuestionLinked or embedded Pin
sides_dale16-Mar-07 19:37
sides_dale16-Mar-07 19:37 
AnswerRe: Linked or embedded Pin
jebarson16-Mar-07 23:12
jebarson16-Mar-07 23:12 

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.