New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending image in non asp.net app. #44

Open
calumb opened this Issue Dec 6, 2012 · 9 comments

Comments

Projects
None yet
9 participants
@calumb
Copy link

calumb commented Dec 6, 2012

I have a class library where I need to send emails with images.

I get the following error when using @Html.EmbedImage. I'm using the MultiPart example but in a class library.

Unable to compile template. The name 'Html' does not exist in the current context.

Is this possible?

@hellfirehd

This comment has been minimized.

Copy link

hellfirehd commented Apr 17, 2013

I hate to simply say "Me too" but I have exactly the same problem.

@hellfirehd

This comment has been minimized.

Copy link

hellfirehd commented May 2, 2013

I've been looking into this. and isn't anything to do with postal. The fact is that RazorEngine (upon which postal relies) has no notion of the HtmlHelper which all the extension methods hang off of. When it renders a template, the template inherits from an base class provided by RazorEngine which does not have a Html member.

The workaround is to simply create an new class that inherits from Postal.Email and then implement your helpers on it or as extension methods to it.

@NightOwl888

This comment has been minimized.

Copy link

NightOwl888 commented Jun 22, 2013

Actually, this sounds like the same problem I had when trying to create database driven views. I managed to work it out though - it is because when compiling the view outside of the context of the Views folder, the main Web.config is used (as opposed to the one in the Views folder) and the namespaces required for the @html and other objects are not included in the main Web.config.

To fix the problem, you need to add the following config sections to your ROOT web.config:

<configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
  <appSettings>
    <add key="webpages:Enabled" value="true" />
  </appSettings>

See this link for more details if you need them: http://forums.asp.net/t/1705855.aspx/1

@JerryDoubleU

This comment has been minimized.

Copy link

JerryDoubleU commented Dec 7, 2015

I have also spent some hours trying to get around this issue.

Mine solution was as follows (pseudo C# code):
Attachment someImageAttachment = new Attachment(imagePath);
emailModel.SomeImageContentId = someImageAttachment.ContentId;
email.Attach(someImageAttachment);

in Email.cshtml:
<img src="cid:@Model.SomeImageContentId" alt="SomeImage" />

Works perfectly in every e-mail client I have tested and the image is not visible as an attachment, which makes this solution even more awesome ;)

Hope it helps you and saves you lots of time :)

@mattkoch614

This comment has been minimized.

Copy link

mattkoch614 commented Dec 31, 2015

@JerryDoubleU Thank you! This worked for me :)

@vip32

This comment has been minimized.

Copy link

vip32 commented Feb 23, 2016

@JerryDoubleU Wow, that is a nice workaround!

@tanis2000

This comment has been minimized.

Copy link

tanis2000 commented Jul 13, 2016

@JerryDoubleU that worked fine. Awesome workaround! Thanks!

@jrz22

This comment has been minimized.

Copy link

jrz22 commented Jul 21, 2016

@JerryDoubleU Thanks for posting!

@jeniferdeveloper

This comment has been minimized.

Copy link

jeniferdeveloper commented Jan 11, 2018

@JerryDoubleU Thanks, is Work ...

have also spent some hours trying to get around this issue.

Mine solution was as follows (pseudo C# code):
Attachment someImageAttachment = new Attachment(imagePath);
emailModel.SomeImageContentId = someImageAttachment.ContentId;
email.Attach(someImageAttachment);

in Email.cshtml:
SomeImage

Works perfectly in every e-mail client I have tested and the image is not visible as an attachment, which makes this solution even more awesome ;)

Hope it helps you and saves you lots of time :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment