Click here to Skip to main content
Rate this: bad
good
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="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Button Text="Click" runat="server" OnClick="Send"/>
    </form>
</body>
</html>
 
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 = "mail.npgnet.ir";
        string to = "r.ghafari@npg.ir";
 
        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("Thanks");
        msgText.Append("With regards,");
        msgText.Append("");
        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";
 
        View.LinkedResources.Add(resource);
 
        Message.From = new MailAddress(from, subject);
        Message.AlternateViews.Add(View);
        Message.To.Add(to);
 
        client = new SmtpClient();
        client.Port = 25;
        client.Host = server;
        client.Credentials = new NetworkCredential(from, password);
        client.Send(Message);
    }
}
Posted 18-Feb-13 9:53am
Edited 18-Feb-13 10:36am
v2

1 solution

Rate this: bad
good
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:
http://en.wikipedia.org/wiki/URI_scheme[^],
http://tools.ietf.org/html/rfc2392[^].
 
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:
http://kosiara87.blogspot.com/2011/04/c-sending-mail-with-embedded-image.html[^].
 
(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: http://bit.ly/zpvER5[^].
 
If you need more advanced example, try to find some more; it returned just about 227,000 results.
 
—SA
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 405
1 Peter Leow 225
2 Sergey Alexandrovich Kryukov 210
3 Maciej Los 195
4 DamithSL 174


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