Solved

Send email with image from aspx page with c# code

Posted on 2007-12-03
5
2,260 Views
Last Modified: 2013-12-16
I have used the following C# code behind to send a test email, it seems to work for what I need, but I would like to send an image embedded in the body of the email. How can I do this? Thanks in advance.  

        MailMessage m = new MailMessage();
        m.From = new MailAddress("Test@Test.com");
        m.Subject = "Test Email";
        m.Body = "none";
        m.To.Add(new MailAddress(EmailInputBox.Text));
        SmtpClient client = new SmtpClient("relay-hosting.secureserver.net");
        try
        {
            client.Send(m);
            EmailConfirmationLabel.Text = "Email successfully sent.";
        }
        catch (Exception ex)
        {
            EmailConfirmationLabel.Text = "Sending Email Failed." + ex.Message;
        }
0
Comment
Question by:cbrick04
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20401720
       MailMessage m = new MailMessage();
        m.From = new MailAddress("Test@Test.com");
        m.Subject = "Test Email";
        m.Body = "none";
        m.To.Add(new MailAddress(EmailInputBox.Text));

MailAttachment attachment = new MailAttachment( Server.MapPath( "image.jpg" ) ); //create the attachment
m.Attachments.Add( attachment );      //add the attachment

        SmtpClient client = new SmtpClient("relay-hosting.secureserver.net");
        try
        {
            client.Send(m);
            EmailConfirmationLabel.Text = "Email successfully sent.";
        }
        catch (Exception ex)
        {
            EmailConfirmationLabel.Text = "Sending Email Failed." + ex.Message;
        }
0
 
LVL 25

Accepted Solution

by:
imitchie earned 250 total points
ID: 20401724
To EMBED it properly, you will need to send HTML formatted email, like in
http://ouadiebakisenhaji.blogspot.com/2007/05/send-mail-with-embedded-image-c-20.htm
0
 
LVL 8

Assisted Solution

by:sjturner2
sjturner2 earned 250 total points
ID: 20401761
Hi,

For each image you need to create a Linked Resource i.e:

 LinkedResource linkedResource = new LinkedResource(
                        new MemoryStream(embeddedImage.Buffer),
                        "image/jpeg");

 linkedResource.ContentId = "ImageIdentifier";
 linkedResource.TransferEncoding = TransferEncoding.Base64;
 linkedResources.Add(linkedResource);

Then in the html of the email you need to put a link to the image putting cid: in front of the image id:
// <img 'src=cid:ImageIdentifier'  ...

Then you need to create an alternativeview from the html and add the images:

 AlternateView alternativeView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
 foreach (LinkedResource linkedResource in linkedResources)
 {
      alternativeView.LinkedResources.Add(linkedResource);
 }

The finally add the alternative view to the mail message i.e:

mailMessage.AlternateViews.Add(alternativeView);

and send the email. The mail client will display the Alternative view if the user can view html emails.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20591132
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question