0

I am trying to create an img tag in code-behind and send it in email (in the body of the email not as attachment).

body += "<p><img src='" + imageUrl.Remove(0,2) + "' alt='Product Image' width='250px' height='250px' runat='server' /></p>";

NB: I have declared body as string and I am adding other HTML controls to it (e.g p,h1,li etc).

The imageUrl variable returns "~/Images/bag/name_of_image.jpg" hence I am removing the first two characters which are ~/.

It seems like it is creating the image but its not displaying in the email body.

Uğur Aldanmaz
  • 1,018
  • 1
  • 11
  • 16
Linda
  • 201
  • 1
  • 7
  • 19
  • 3
    To link anything in an email, you need to use absolute path including protocol (`http(s)`,...) and of course your domain name. How would an email client know which URL your relative path are actually relative to ? – Laurent S. Sep 24 '14 at 11:45
  • 2
    FYI you can't "create" elements with `runat='server'` like that. – MikeSmithDev Sep 24 '14 at 13:10
  • What you mean with create? Yes it can be create, but does not works... – Uğur Aldanmaz Sep 24 '14 at 13:13

4 Answers4

1

You need include the absolute path to the image in the e-mail.

Images embedded in an email should be like:

<img src="http://www.example.com/image_link.jpg" alt="Some Image" />

You however in your email you would be embedding:

<img src="image_link.jpg" alt="Some Image" />

As the e-mail application the user has won't have a copy of image_link.jpg it can't show it.

So in your process you may need to upload the image to an externally accessible webserver (if being access outside your network) or at least an internally accessible web server.

Ryan McDonough
  • 9,732
  • 3
  • 55
  • 76
  • Thanks a lot Ryan. it works when I use absolute path like `http://cdn-careers.sstatic.net/careers/Img/logos/careers-logo-gray.png`. However I wonder why I would NOT work when I use an internal absolute path like **C:\inetpub\wwwroot\Websitename\Images\image_name.jpg** – Linda Sep 24 '14 at 12:07
  • It's because the user you are sending it to doesn't have access to your C:/ drive (rightly so) - however the websever will provide that access. If you have found the answer useful please mark it as answered :) – Ryan McDonough Sep 24 '14 at 13:48
  • Remeber when the e-mail is shown on the users system it will try to find : C:\inetpub\wwwroot\Websitename\Images\image_name.jpg - which isn't a correct path for the file on their system - and even if it did exist (i.e testing locally on your machine) the e-mail client likely wouldn't be allowed access to the C:/ drive. – Ryan McDonough Sep 24 '14 at 13:50
0

I would suggest to use embedded image in your email body. Have a look at below URLs:

  1. http://www.emailarchitect.net/easendmail/kb/csharp.aspx?cat=8
  2. Send inline image in email
Community
  • 1
  • 1
SpiderCode
  • 10,062
  • 2
  • 22
  • 42
0

Please do it as below. just remove "~" from SRC.

 string mainUrl = "http://testURL.com/"; //its just example, you can put this in web config as well and than retrive it

 body += "<p><img src='" + mainUrl + imageUrl.Remove(0,1) + "' alt='Product Image' width='250px' height='250px' runat='server' /></p>";

Hope this will help you,

Thanks

JJRS
  • 47
  • 5
0

If you embed an image with its local path to an e-mail, this image cannot be accessible from remote receviers.

It is basic; user opens your e-mail using with any e-mail client program (outlook, thunderbird or webmail etc.), and e-mail client program searches this image using with its path. If path is a local, how can e-mail client program access this image?

You have to give a common accessible path that e-mail clients can access your image file and load then show it to user.

So upload your image to a server, then use the server address to specify image path.

An example:

string domainName = @"http://lindaLinda.com/images/";
string imageFileName = "myFirstEmailImg.jpg";
body += @"<p><img src='" + domainName + imageFileName +"' alt='Product Image' width='250px' height='250px' runat='server' /></p>";

It gives this output:

<p><img src='http://lindaLinda.com/images/myFirstEmailImg.jpg' alt='Product Image' width='250px' height='250px' runat='server' /></p>
Uğur Aldanmaz
  • 1,018
  • 1
  • 11
  • 16