?
Solved

Send email with image from aspx page with c# code

Posted on 2007-12-03
5
Medium Priority
?
2,269 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 1000 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 1000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

777 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