tags:

views:

809

answers:

3

sending mail along with embedded image using asp.net

I have already used following but it can't work

Dim EM As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage(txtFrom.Text, txtTo.Text) Dim A As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(txtImagePath.Text) Dim RGen As Random = New Random() A.ContentId = RGen.Next(100000, 9999999).ToString() EM.Attachments.Add(A) EM.Subject = txtSubject.Text EM.Body = "" + txtBody.Text + "
" EM.IsBodyHtml = True Dim SC As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(txtSMTPServer.Text) SC.Send(EM)

A: 

I don't think it's possible without third-party components,unless you do all the hard work yourself: you have to create a multi-part message and have the HTML refer to the proper cid: (embedded content) elements.

The EasyMail.NET component made by Quiksoft allows you to send out HTML mails with embedded images. I've used it for years and it works beautifully.

Philippe Leybaert
how is that hard work! takes like a minute
Simon_Weaver
So why don't you put your money where your mouth is, and show us the code?
Philippe Leybaert
The code is above in the winning answer. :)
Kyberias
+4  A: 

If you are using .NET 2 or above you can use the AlternateView and LinkedResource classes like this:

string html = @"<html><body><img src=""cid:YourPictureId""></body></html>";
AlternateView altView = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html);

LinkedResource yourPictureRes = new LinkedResource("yourPicture.jpg", MediaTypeNames.Image.Jpeg);
yourPictureRes.ContentId = "YourPictureId";
altView.LinkedResources.Add(yourPicture);

MailMessage mail = new MailMessage();
mail.AlternateViews.Add(altView);

Hopefully you can deduce the VB equivalent.

Alex Peck
Beaten by a man with the same name.
Alex Peck
Sometimes it happens ;)
Alex
getting beaten is better than getting your eyes 'peck'ed out i would guess
Simon_Weaver