CODEDIGEST
Home Articles CodeDigest Tutorials FAQs
Skip Navigation LinksHome » Article » ASP.Net Article » Email with Embedded image in ASP.Net 2.0   You are not logged in.
Search
 

Technologies
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Email with Embedded image in ASP.Net 2.0

By Satheesh babu
Posted On Feb 17,2008
Article Rating: (Login)
Be first to rate
this article.
No of Comments: 6
Print this article.
Category: ASP.Net

Subscribe to our feed!

Email with Embedded image in ASP.Net 2.0

We can embed image in email body using LinkedResource class and AlternateView class that is packed with System.Net.Mail namespace. We can achieve this by making the message as HTML.

How to achieve this?

 We have to include a <IMG> with a contentid specified in src attribute of <IMG> tag in the message body i.e. the HTML body should contain <img src="cid:imageId" />.

 

Example will be,

<b>View my Pic</b><br>

<img src="cid:Anyid" />

 

After doing this we need to give the same contentid to the ContentId attribute of LinkedResource to work.

 

Implememtation:

 

try

        {

            MailMessage mail = new MailMessage();

            mail.To.Add("xxx@gmail.com");

            mail.From = new MailAddress("xxxx@gmail.com");

            mail.Subject = "Test with Image";

            string Body = "<b>Welcome to popfly!!</b><br><img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 >";

 

            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");

            LinkedResource imagelink = new LinkedResource(Server.MapPath(".") + @"\popfly.JPG", "image/jpeg");

            imagelink.ContentId = "imageId";

            imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

            htmlView.LinkedResources.Add(imagelink);

 

            mail.AlternateViews.Add(htmlView);

            SmtpClient smtp = new SmtpClient();

            smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

            smtp.Send(mail);

        }

        catch (Exception ex)

        {

            Response.Write(ex.Message);

        }

 

OUTPUT:

Happy Coding!!!

Similar Articles
  • You can contribute to CodeDigest.Com:
    Article Feedback
    Title  
    Submitted By  
    Comment  
    Enter the verification number
     
    Comments
    Feeback
    Hi,

    This piece of code was really helpful.

    Thanks,
    Sheeja
    Attaching image
    Hello,

    I tried using above code but it is sending image as an attachment.

    Thanks
    Vinod Suri
    Thanks
    U r article solved my problem thanks .Easy and simple code
    Thanks!
    Thanks for the example. It helped me get where I need to be in just moments.
    Trying to embed more than 1 image
    Thanks for your blog. My requirement is to embed more than one image in a mail. Please see the code i am using
    Dim htmlBody As String = "<html><body><table border='3' width='720' height='480' ALIGN='center' ><tr><td><table background='cid:backgroundImage' border='0' width='715' height='475' ALIGN='center' ><tr><th colspan='3' height='40'></th></tr><tr ><td width='200'></td><td width='350' align='center'><font color='#808000' face='Mistral' size='8' ><b>" + dataReader.GetValue(1).ToString() + "</b><br/> </font><b><font color='#808000' face='Comic Sans MS' size='5'> Best Wishes from </font></b></td><td width='170'><img src='cid:employeeImage' align='absbottom ' height='150' width='150' align='absbottom ' ></td></tr></table></td></tr></table></body></html>"

    'Dim htmlBody As String = "<html><body><table border='3' width='720' height='480' ALIGN='center' ><tr><td><table background='cid:backgroundImage' border='0' width='715' height='475' ALIGN='center' ><tr><th colspan='3' height='40'></th></tr><tr ><td width='200'></td><td width='350' align='center'><font color='#808000' face='Mistral' size='8' ><b>" + dataReader.GetValue(1).ToString() + "</b><br/> </font><b><font color='#808000' face='Comic Sans MS' size='5'> Best Wishes from </font></b></td><td width='170'><img src='C:\Birthday and Anniversary Project\testImage.jpeg' align='absbottom ' height='150' width='150' align='absbottom ' ></td></tr></table></td></tr></table></body></html>"
    Dim hmtlView As AlternateView = AlternateView.CreateAlternateViewFromString(htmlBody, Nothing, "text/html")
    Dim background As New LinkedResource("C:\BirthdayandAnniversaryProject\Images\template.JPG")
    background.ContentId = "backgroundImage"
    'hmtlView.LinkedResources.Add(background)
    'mailMessage.AlternateViews.Add(hmtlView)
    Dim EmpImage As New LinkedResource("C:\BirthdayandAnniversaryProject\testImage1.jpeg")
    EmpImage.ContentId = "employeeImage"
    EmpImage.TransferEncoding = Net.Mime.TransferEncoding.Base64
    Dim collection As LinkedResourceCollection = hmtlView.LinkedResources()
    collection.Insert(0, background)
    collection.Insert(1, EmpImage)
    hmtlView.LinkedResources.Concat(collection)
    mailMessage.AlternateViews.Add(hmtlView)

    I however get an exception on server saying - System.runtime.interopservices.ext exc : A generic error occurred in GDI+

    Please help.. .THanks
    Email with Embedded image in ASP.Net 2.0
    i tried to send bulk new letter where mail ids are generated by database and inside loop i coded for sending mail, application runs well but it is taking 3 minutes to send 2 mails with inline image embed as you coded. i do no why taking so much of time. could you please help me out.