Kar
Kar

Reputation: 321

Image is not shown on live server

I have a logo that I need to show inside a mail. On my local machine, the logo is shown, but on the server where the live environment is, the logo is not shown.

I'm using the path like this:

imagePath = "~/Images/email-logo2.png";

The email-logo2.png exists in the same folder on the local machine and on the server, too.

enter image description here

I have tried to add permissions to read in the server folder where the png exists, but it did not resolve the problem. Can you advise?

The image is added in the email like this:

HTML:

<img class="auto-style4" src="{PictureSrc}" /><br />

C# code:

switch (property)
{
    case "PictureSrc":
        string imagePath = "";

        if (User.Identity.GetUserId<int>() == 3140 || 
            User.Identity.GetUserId<int>() == 3142)
        {
            imagePath = "~/Images/email-logo2.png";
        }
        else
        {
            imagePath = "~/Images/email-logo.png";
        }

        content = content.Replace("{" + property + "}", HttpContext.Server.MapPath(imagePath));

Upvotes: 1

Views: 2354

Answers (1)

ADyson
ADyson

Reputation: 61839

Server.MapPath returns a path on disk (e.g. C:\images\image.png), not a URL. See https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath(v=vs.110).aspx.

So a user viewing the email from another machine will obviously not be able to resolve a path on the server's disk, it has no access to that disk.

The image path you provide has to be a fully qualified URL e.g. http://www.example.com/images/image.png.

It worked locally because you're on the same machine as where the image is located, so it happens to have access to that path, but that's not true for everyone else using it.

Alternatively, if the image is not too large you can convert it to base64 and embed it into the HTML in the email.

Upvotes: 1

Related Questions