I have a HTML email template in the App_Data folder of my MVC application. In my code, I use this template to send HTML emails to users. This template references a few images in a folder in my project. The issue is that these images don't appear at all when the user receives the email. I have tried to reference the images using ~/path to image/image.gif. I have tried using ../../path to image/image.gif and I have copied the images to the App_Data folder and just referenced the images thus image.gif. Nothing is working. Does anyone have any suggestions?
the images either need to be stored in a publicly accessible location with a full reference to the image <img src="http://my.domain.com/images/filename.ext" /> or the images need to be embedded into the email.
The reason behind the image is not show in email because email client cannot get path you
specified.
For the solution you can do below
First replace the simple image URL to publically access url
[means if you copy the image URL in browser you can access the browser], make sure your image folder is out of authentication
As Jason said you have to put whole url, but if you put your image in App_Data they won't be accessible from outsite because this is a protected folder. You have another option to put images in mail, IMO this is a prefered way to put images in mail, e-mail client won't complain and ask for permission to display images. Here is a example how to do this
http://www.codeproject.com/KB/aspnet/EmbedImage.aspx
you can also use google picasa.
upload the images to picasa, set the visibility to public, get in the album and on the right
side you will have link to this photo link.
press this link and chose the size you want it to be displayed, mark the image only
check box and you will have a link ready to embed in to the mailer.
put the link in the src and that is it.
Related
I am able to send email via smtp with c#, whereby inserting images with LinkedResource.
The problem now is that the emails received contain ATT0000X.bin files as attachment. How do I make sure these bin files are not added in the email?
Thanks in advance!
I just had this issue while trying to embed an image to the e-mail. I had two things wrong on my code, maybe one of them is happening to you:
First I was embedding an image but didn't define any tag for it to be displayed in my Body. This was fixed by adding into my HTML the tag cid:yourContentId and then assigning that tag as the image's ContentId. That fixed the issue in outlook, but it was still showing a clip like if the e-mail had an attachment, but no place to actually see the attachment. This looks super spammy. In Outlook web you could still see the ATT00001.bin attachment.
My second mistake was not defining the ContentType of my embedded image when creating the LinkedResource. This fixed the clip being displayed in the desktop client and in the web client no attachment could be seen anymore. The final code is something like this
LinkedResource embeddedImage = new LinkedResource(imageStream, new System.Net.Mime.ContentType("image/jpeg"));
embeddedImage.ContentId = "ContentId";
altview.LinkedResources.Add(embeddedImage);
mailMessage.AlternateViews.Add(altview);
I have the following structure:
AppName
Content
img
file.png
I can navigate directly to mysite.com/Content/img/file.png without any issues.
However when I add test.txt to img and set to Copy Always and type Content (same as file.png), I get No webpage was found for the web address when visiting the link, when I want to be able to enable folks to download the publicly accessible file (no auth needed), just like the image. Why isn't this working?
I can see test.txt in the bin folder of the app. Does something else need to be configured to enable direct file downloads?
Make sure your IIS site settings or web.config have mime type associated with .txt extension.
https://www.iis.net/configreference/system.webserver/staticcontent/mimemap
I am trying to use a HTML file as mail body template. HTML file has few inline images like LOGO. Everything is fine and I am getting all contents in the HTML file upon receiving the mail, except the images.
An example below src="~/Images/logo.png" but after getting the mail, if I view source in mail client I see like below (using Google SMTP server to send mail).
Reason is pretty clear as why images are not getting displayed (due to the fact that src part is getting changed) but not sure how to avoid this.
<img title="MyCompany" class="xxxx" alt="MyCompany"
src="https://ci4.googleusercontent.com/proxy/VigKoQr9DXTS3UveuswmpxB6iEAO5rcVwSrrVUptfWGAeYQFugjcKDZxDZbB6g=s0-d-e1-ft#http://~/Images/logo.png">
Any solution for this? How can I get the actual images being displayed?
Thank You.
Here the solution!
For email template you should provide absolute url for the image.
Ex: <img src="http://ngm.nationalgeographic.com/2015/11/climate-change/which-species-will-thrive/img/12-bengal-tiger-1024.jpg" >
There is an image in Images folder in my solution and I have an image controller on my page. and on a button click I set the URL of the image like this,
imgDisp.ImageUrl = #"~/Images/American.jpg";
But the image is not showing up on the browser when I run the code. The image is present in the specified folder and I didnt mention any URL in tag either. What am I missing ? The image URL should be mentioned the above way when running in local host too or should it be done differently ? Thanks in advance for the reply.
I actually have a C# winform application which load images from my computer C://images/... with the Image object and the function Fromfile.
Image.FromFile(Path);
but in my web application (ASP)
<asp:Image ID="viewPhoto" runat="server" Width="550px" Height="400px"/>
I use the attribute ImageURL.
viewPhoto.ImageURL = Path
But the problem is that it doesn't find the correct path because with this way. The path will be http://localhost:3656/C://images....
I would like to load an image directly from my server to have the correct path for both of my applications.(web ASP and winform)
Image.FromFile(/images/myimage.jpg)
This actually doesn't work because the program doesn't find any photo in this path.
First of all i think that images you are trying to show are not in web application folder / virtual directory. Move images folder to your web application folder and then use:
Page.ResolveClientUrl("images/test.jpg");
or for server side:
Server.MapPath("images/test.jpg");
If you dont want to move images to your web folder then your only choice is to write HttpHandler which will read images from C:\images folder and transmit it to the client. This will also require some specific permissions for your web app IIS user to access some folder outside the web app scope.
You can see the sample of HttpHandler here: Thumbnailer HTTP Handler
ASP.Net image simply renders the ImageURL to the client browser, this will then try to load that image from the location specified, this typically needs to be a resource available via your website.
Try moving your images inside your web root then you can access them like :
viewPhoto.ImageUrl = "~/images/yourimage.jpg";
As im sure you know your <asp:Image> element is actually creating an html <img> element that will instruct the browser to request the image from the browser.
Image.FromFile will give you an image object on the server side but it won't be much use for your clients browser.
As #HABJAN said, you need to map the path from your local file to a relative web URI and to do that you can use Server.MapPath. However your images will need to be inside your asp.net project folder which is asccessible by the web server in order for it to serve the file.
If you really want to share the paths in some sort of shared constant assembly I suggest you make your windows application work with relative paths and duplicate the images folder structure for both the windows app and the web app.
I found the quickest way to resolve this was to assign the image path as a css attribute. The only downside of this is that if you are looking to display a large amount of images in a repeater you would not want to use this, but if you have one or two images you can use this.
<style type="text/css">
#errorImage{
background: url(/path/path/images/image.jpg);
background-size: contain;
height: 100px;
width: 100px;
}
</style>
<div id="errorImage"><div>
Using the path you are using now (http://localhost:3656/C://images....), the application will look for a /C://images... folder in your app directory on the web server. I believe you should create an images directory in your application folder and server the images from there so that you could reference the images using a relative path.