Click here to Skip to main content
6,635,160 members and growing! (14,093 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Internet     Beginner License: The Code Project Open License (CPOL)

Sending email with an embedded image through ASP.NET

By jebarson

An article to send email from ASP.NET 2.0 by embedding an image in the content
VB, Windows, .NET 2.0, ASP.NET, Visual Studio, WebForms, Dev
Posted:6 Mar 2007
Views:59,112
Bookmarked:32 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
17 votes for this article.
Popularity: 3.09 Rating: 2.52 out of 5
7 votes, 41.2%
1
1 vote, 5.9%
2
2 votes, 11.8%
3
4 votes, 23.5%
4
3 votes, 17.6%
5

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.

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)

About the Author

jebarson


Member
Working as Technical Lead in Photon Infotech Inc., MA, USA. I love programming and that too exploring into new things.

I Started my first program by building a game on GW BASIC when I was 14. The release of the game made me addicted to fame. I fell in love with programming. I will program even on the last day of my life Smile
Occupation: Architect
Location: United States United States

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 38 (Total in Forum: 38) (Refresh)FirstPrevNext
GeneralEmbedding Multiple Images (based on an existing HTML "template" file) PinmemberBrian Rickard15:15 13 Apr '08  
AnswerRe: Embedding Multiple Images Pinmemberjebarson6:11 15 Apr '08  
GeneralRe: Embedding Multiple Images PinmemberBrian Rickard6:41 15 Apr '08  
GeneralI am getting the attachment as .Dat file PinmemberElavarasu2:54 13 Jun '07  
AnswerRe: I am getting the attachment as .Dat file Pinmemberjebarson0:10 16 Jun '07  
AnswerRe: I am getting the attachment as .Dat file PinmemberBrianB15:50 11 Jun '09  
QuestionRegards VB.NET and ASP.NET Pinmemberahmedaaki1:49 21 May '07  
AnswerRe: Regards VB.NET and ASP.NET Pinmemberjebarson2:47 21 May '07  
GeneralHai Tell About AJAX PinmemberSenthil S17:43 3 May '07  
AnswerRe: Hai Tell About AJAX Pinmemberjebarson20:36 6 May '07  
GeneralSuggestion: Example message should include the image Pinmembersimon.proctor0:45 12 Apr '07  
GeneralRe: Suggestion: Example message should include the image Pinmemberjebarson1:25 12 Apr '07  
GeneralRe: Suggestion: Example message should include the image Pinmembersimon.proctor1:29 12 Apr '07  
AnswerRe: Suggestion: Example message should include the image Pinmemberjebarson2:22 16 Apr '07  
GeneralRe: Suggestion: Example message should include the image Pinmembersimon.proctor23:15 17 Apr '07  
AnswerRe: Suggestion: Example message should include the image Pinmemberjebarson19:48 18 Apr '07  
GeneralRe: Suggestion: Example message should include the image Pinmembersimon.proctor0:04 19 Apr '07  
AnswerRe: Suggestion: Example message should include the image Pinmemberjebarson20:26 20 Apr '07  
GeneralRe: Suggestion: Example message should include the image Pinmemberbeebbh6:46 23 Apr '07  
GeneralRe: Suggestion: Example message should include the image PinmemberGlay6:24 6 May '07  
GeneralGetting Exception.. Pinmembercrazy_mads23:18 29 Mar '07  
GeneralRe: Getting Exception.. Pinmemberjebarson23:25 29 Mar '07  
GeneralRe: Getting Exception.. Pinmembercrazy_mads0:06 30 Mar '07  
GeneralRe: Getting Exception.. Pinmemberjebarson22:50 10 Apr '07  
GeneralI am coding in C#. Pinmembercrazy_mads21:14 29 Mar '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Mar 2007
Editor: Chris Maunder
Copyright 2007 by jebarson
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project