There are a few different ways that you can send images via email. The easiest way to do this is to simply make the body of the email html and add in an <img> tag that links directly to the image on the internet. However, because this has been abused by email spammers an image sent like that will most likely not be viewed depending on the recipients email settings. In this tutorial we will demonstrate how to send an email with an attached image displayed in the messages body.
We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud servers!
Adding the Default.aspx PageTo demonstrate this I am going to create a simple web site with a button in it that I will use to send the email. At this point in the tutorial I have created a new ASP.NET Empty Web Site. To begin:
  1. Right click the project in your solution explorer.
  2. Select add new item…
  3. Select a web form.
  4. Name it ‘Default.aspx’.
  5. Click add.
  6. Open Default.aspx up to design mode.
  7. Drag and drop a button onto the web form.
Adding the ImageFor this example, I will be adding an image to the project so that we can simplify the code here, however you can use whatever image you want. To do this:
  1. Right click the project in your solution explorer.
  2. Select add existing item…
  3. Select the image you want to add.
  4. Click add.
Sending the EmailNext, we need to add in the code to send the email. It is important to understand that you will need access to your own email account to which you know the SMTP settings in order to send an email. For this code sample I wll be configuring the SMTP settings to send an email using Gmail. However, you can use whatever email you want as long as it is configured properly. To begin:
  1. Open Default.aspx to design mode.
  2. Double click the button we added earlier to generate the click event method for it.
  3. Add the following using statements at the top of the Default.aspx.cs class:
    Code Block
    Default.aspx.cs
    The using statements we need to send email.
    using System.Net.Mail;
    using System.Text;
    using System.Net;
  4. Add the following code to the Button1_Click event method to send the email:
    Code Block
    Default.aspx.cs
    The Button1_Click event method.
    protected void Button1_Click(object sender, EventArgs e)
    {
        //create the message
        MailMessage mail = new MailMessage();
        //add the email address we will be sending the message to
        mail.To.Add("Recipient@gmail.com");
        //add our email here
        mail.From = new MailAddress("Sender@Gmail.com");
        //email's subject
        mail.Subject = "In line image test";
        //email's body, this is going to be html. note that we attach the image as using cid
        mail.Body = "Hello email. <br/> <img src=\"cid:tmpImage.gif\">";
        //set email's body to html
        mail.IsBodyHtml = true;
            
        //add our attachment
        Attachment imgAtt = new Attachment(Server.MapPath("tmpImage.gif"));
        //give it a content id that corresponds to the src we added in the body img tag
        imgAtt.ContentId = "tmpImage.gif";
        //add the attachment to the email
        mail.Attachments.Add(imgAtt);
     
        //setup our smtp client, these are Gmail specific settings
        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
        client.EnableSsl = true//ssl must be enabled for Gmail
        //our Gmail account credentials
        NetworkCredential credentials = new NetworkCredential("Sender@Gmail.com""Password");
        //add credentials to our smtp client
        client.Credentials = credentials;
     
        try
        {
            //try to send the mail message
            client.Send(mail);
        }
        catch
        {
            //some feedback if it does not work
            Button1.Text = "Fail";
        }
    }
I just signed up at Server Intellect and couldn’t be more pleased with my fully scalable & redundant cloud hosting! Check it out and see for yourself.
What this is doing is creating a new email message and attaching an image to it. Then, in the body we add an <img> tag that links to the image in the attachment of the email. This way, your image is more likely to show up to the recipient based on their email settings.
TestingTo test this out, modify the Button1_Click event method to work with your corresponding email address and provider. Then, change the recipient email to one of your email accounts. Once this is done, load up the web site and click the button to send the email. Then, verify that the email is sent and the proper image is attached and viewable within your email. 
email with img asp4 csharp