Solved

Send email with image from aspx page with c# code

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

786 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