Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending mail along with embedded image using asp.net

Tags:

c#

asp.net

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 = "<body>" + txtBody.Text + "<br><img src='cid:" + A.ContentId +"'></body>"
        EM.IsBodyHtml = True
        Dim SC As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(txtSMTPServer.Text)
        SC.Send(EM)
like image 532
Tushar Maru Avatar asked Jul 11 '09 09:07

Tushar Maru


People also ask

How do I send an email with HTML content including images?

To attach an image, you need to have the encoding scheme of the image you want to attach. This is the base64 string of the picture. You can get this by right-clicking on the image you want to attach, copy the image address, and paste it into the HTML text. The recipient will have a preview of when they open the email.

How do you embed an image into an email?

Insert a picture into the body of an email messagePosition your cursor where you want the image in your message. Select Insert > Pictures. Browse your computer or online file locations for the picture you want to insert. Select the picture, then select Insert.

How can you send an email message from an asp net web page?

First: Edit Your AppStart Page SmtpPort: The port the server will use to send SMTP transactions (emails). EnableSsl: True, if the server should use SSL (Secure Socket Layer) encryption. UserName: The name of the SMTP email account used to send the email. Password: The password of the SMTP email account.


1 Answers

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(yourPictureRes);

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

Hopefully you can deduce the VB equivalent.

like image 160
Alex Peck Avatar answered Nov 07 '22 22:11

Alex Peck