Click here to Skip to main content
12,757,627 members (36,807 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET image
Hi, this my ASPX page, i want to send mail via asp and embed some images to it, but doesn't embed images

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendMail.aspx.cs" Inherits="SendMail" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Button Text="Click" runat="server" OnClick="Send"/>

using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Web.Services;
using System.Globalization;
using System.Net.Mail;
using System.Net.Mime;
using System.Net;

public partial class SendMail : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)


    protected void Send(object sender, EventArgs e)
        string subject = "CNet";
        string body = "";
        string from = "from";
        string password = "pwd";
        string server = "";
        string to = "";

        MailMessage Message = new MailMessage();
        AlternateView View;
        LinkedResource resource;
        SmtpClient client;
        StringBuilder msgText = new StringBuilder();

        msgText.Append("Hi there,");
        msgText.Append("Welcome to the new world programming.");
        msgText.Append("With regards,");
        msgText.Append("<img src=\"cid:pic\" />");

        View = AlternateView.CreateAlternateViewFromString(msgText.ToString(), Encoding.UTF8, MediaTypeNames.Text.Html);

        resource = new LinkedResource((Server.MapPath(".")) + "\\pic.gif", "image/gif");

        resource.ContentId = "Image1";


        Message.From = new MailAddress(from, subject);

        client = new SmtpClient();
        client.Port = 25;
        client.Host = server;
        client.Credentials = new NetworkCredential(from, password);
Posted 18-Feb-13 10:53am
Updated 18-Feb-13 11:36am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Here is the background: the images in HTML e-mail should be referenced using the special URI scheme "cid:" (Content-id). Please see:[^],[^].

The e-mail should be a multipart e-mail. Some parts could be HTML documents and some — base64-encoded images. To reference an image in one part in HTML part, HTML code <img> tag should reference the image as cid:<content-id></content-id>, where content-id should be the same as LinkedResource.ContentId of the image part to be referenced and thus rendered as inserted in HTML document.

This example illustrates using this technique for creation of e-mail with C# and .NET:[^].

(This is a pretty dumb code; it uses a placeholder "@@IMAGE@@" in the HTML code to be replaced with actual value for content id using string.Replace, instead of, say, using string.Format. It has nothing to do with the idea of embedding the images.)

I found this example using this query:[^].

If you need more advanced example, try to find some more; it returned just about 227,000 results.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170217.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100