2

I am embedding an image in the body of an email and that works fine but the image is full size and takes up most of the email so I want to resize the image.

<div runat="server" id="dvEmailLogo">
   <asp:Image runat="server" ID="EmailLogo" />
</div>

Code behind:

    EmailLogo.ImageUrl = ConfigurationManager.AppSettings["LogoFileFolder"] + Company.Current.CompCode + "\\" + Company.Current.Logo + "";

    Attachment inlineLogo = new Attachment(EmailLogo.ImageUrl);
    mailMsg.Attachments.Add(inlineLogo);
    string contentID = "Image";
    inlineLogo.ContentId = contentID;

   //To make the image display as inline and not as attachment
   inlineLogo.ContentDisposition.Inline = true;
   inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;

   //To embed image in email
   mailMsg.Body = "<htm><body> <img src=\"cid:" + contentID + "\"> </body></html>";

I tried adding the width and height to the url:

EmailLogo.ImageUrl = ConfigurationManager.AppSettings["LogoFileFolder"] + Company.Current.CompCode + "\\" + Company.Current.Logo + @"&width=200&height=200";

but this causes an error: System.IO.FileNotFoundException: Could not find file

I also tried adding the width the height to the div dvEmailLogo but that didn't change the size

user123456789
  • 1,914
  • 7
  • 44
  • 100
  • you can use css to stretch image in div http://stackoverflow.com/questions/1891857/how-do-you-stretch-an-image-to-fill-a-div-while-keeping-the-images-aspect-rat – Alexey Obukhov Dec 22 '16 at 12:07
  • 1
    @Alexey CSS won't work with many email clients. OP use `Path.Combine` to formulate paths and to solve a *FileNotFound* use a tool: Process Monitor. This will show where the file is trying to be found so you can see what you have to change. – Jeremy Thompson Dec 22 '16 at 12:15
  • You can set the width of the image in the html ``. That is after you found the image on disk. – VDWWD Dec 22 '16 at 12:19
  • or set size of image with javascript after page rendered (or window size changed) – Alexey Obukhov Dec 22 '16 at 12:23

1 Answers1

4

Why don't you just resize the image on the html code?

mailMsg.Body = "<htm><body> <img height="200" width="200" src=\"cid:" + contentID + "\"> </body></html>";
Anestis Kivranoglou
  • 7,728
  • 5
  • 44
  • 47