Click here to Skip to main content
11,641,971 members (58,408 online)
Rate this: bad
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 9:53am
Edited 18-Feb-13 10: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
0 DamithSL 245
1 Afzaal Ahmad Zeeshan 170
2 Sergey Alexandrovich Kryukov 159
3 OriginalGriff 110
4 Mika Wendelius 90
0 Mika Wendelius 460
1 DamithSL 353
2 OriginalGriff 315
3 Sergey Alexandrovich Kryukov 179
4 Afzaal Ahmad Zeeshan 169

Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2015
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