Asp.NET Tutorials
Home > Email专题 > Sending email with an embedded image through ASP.NET
Sending email with an embedded image through ASP.NET

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.

Jebarson Vetha Rajin. J


Click here to view Jebarson Vetha Rajin. J's online profile.

Add by : Huobazi (2007-3-08:04:46)