Solved

Send email with image from aspx page with c# code

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Contant UI messages in .NET 1 40
Select all rows in datagridview 15 21
asp.net mvc5 3 37
Separate a number and letter from a string 25 26
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now