Solved

Send email with image from aspx page with c# code

Posted on 2007-12-03
5
2,267 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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