How to generate HTML email content with asp.net - asp.net

I want to send emails in HTML format. How can I use asp.net to generate HTML content for emails.
Using output of .aspx page(Tried there was nothing in email body. Must be something in page that can't be used for email)
Using Webcontrol and get web control output and use it as email body.
Using custom http handler so can call handler to get email body.
Can you please suggest what would be the best solution?
Some guide or sample reference would be great if know one.
Thanks for all answers:
I am implementing code below:
string lcUrl = "http://localhost:50771/webform1.aspx";
// *** Establish the request
HttpWebRequest loHttp =
(HttpWebRequest)WebRequest.Create(lcUrl);
// *** Set properties
//loHttp.Timeout = 10000; // 10 secs
loHttp.UserAgent = "Code Web Client";
// *** Retrieve request info headers
HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
Encoding enc;
try
{
enc = Encoding.GetEncoding(loWebResponse.ContentEncoding);
}
catch
{
enc = Encoding.GetEncoding(1252);
}
StreamReader loResponseStream =
new StreamReader(loWebResponse.GetResponseStream(), enc);
string lcHtml = loResponseStream.ReadToEnd();
loWebResponse.Close();
loResponseStream.Close();

I think the ASPX file will make the most easy to edit mechanism. You can use
var stream = new MemoryStream();
var textWriter = new StreamWriter(stream);
Server.Execute("EmailGenerator.aspx", textWriter);
to capture the output of that page.
I personally don't like any of your options. I would probably do it by using an HTML file (or a template stored in a database) and substituting something like {{name}} with the appropriate parameter.

I did this, essentially we had a page that the user could view, and then they could click a button and send the html on the page in an email. Basically my page was responsible for generating the email. I did this by overriding the Render method, and providing my own stream or using the one passed to us. We did this dpeneding on if we were rendering what the user would see or emailing it.
protected override void Render(HtmlTextWriter writer)
{
if (_altPrintMethod)
{
System.Net.Mail.MailMessage....
mailMsg.Body=RenderHtml(baseUrl);
}
}
protected virtual string RenderHtml(string baseUrl)
{
RemapImageUrl(baseUrl);
StreamWriter sw = new StreamWriter(new MemoryStream());
HtmlTextWriter writer = new HtmlTextWriter(sw);
base.Render(writer);
writer.Flush();
StreamReader sr = new StreamReader(sw.BaseStream);
sr.BaseStream.Position = 0;
return sr.ReadToEnd();
}
One thing to note is you have to make sure all links are fully qualified. You might be able to use the base functionality. this is what I was doing in the RemapImageUrl basically I appended an absolute path on all my image files.

Related

ASP.NET - How to redirect a response with a PDF to a new tab?

I have a method which I call directly from the Page_Load method, to create a PDF and print it, using PdfSharp, which is called like this:}
if(IsPostBack)
PdfSharpConvert(eventArgument, Response);
And the method looks like this:
public static void PdfSharpConvert(String html, System.Web.HttpResponse response)
{
Bitmap bitmap = new Bitmap(790, 1800);
Graphics g = Graphics.FromImage(bitmap);
XGraphics xg = XGraphics.FromGraphics(g, new XSize(bitmap.Width, bitmap.Height));
TheArtOfDev.HtmlRenderer.PdfSharp.HtmlContainer c = new TheArtOfDev.HtmlRenderer.PdfSharp.HtmlContainer();
c.SetHtml(html);
PdfDocument pdf = new PdfDocument();
PdfPage page1 = new PdfPage();
... code to paint the page ...
MemoryStream stream = new MemoryStream();
pdf.Save(stream, false);
response.Clear();
response.ContentType = "application/pdf";
response.AddHeader("content-length", stream.Length.ToString());
response.BinaryWrite(stream.ToArray());
response.Flush();
stream.Close();
response.End();
}
This works fine, I get the PDF viewer on the browser, but I'm trying to show the PDF in a different tab in the browser, not on the same one, is there a way to do this without rewriting the views I use or doing something more complicated? I tried using response.Redirect, and it does redirect, but I don't know what to give it as a parameter so it properly shows the PDF in a different page.
You require a client side javascript button.
You need to set session, or even pass the file name as part of the url.
so, say like this:
window.open('MyPdfViewer.aspsx', '_blank;);
or maybe:
window.open('MyPdfViewer.aspsx?FileName=' + MyFileName, '_blank;);
Then in the on-page load of MyPdfViewer, in the is-post back = false code stub, you place your code to pump out the pdf.
So, that 2nd new tab and web page? The on-load event of that page will have to fetch the pdf file, and push it out as you are doing now.
So pass the file name in the URL, or assume session[] has the file name to display.
You can also consider display on the current page - inside of a iFrame.

Reading posted JSON data in ASP.NET

I subscribe to a mass email service which, when an error occurs at their end, posts to a page on my website as an endpoint to notify me that an email has bounced.
They describe it as follows:
The event data is sent in the POST request body using a JSON object.
What I need to know is how can I capture the info posted to me?
Currently I'm pointing them to a generic handler, a .ashx page, this can be changed to whatever as long as it's in .NET.
In 10 years working with first classic ASP and now .NET I've never done this before and I must admit I don't even know where to start.
This is the code I used to achieve a similar thing - not sure where I got it originally.
C#
var strJSON = String.Empty;
context.Request.InputStream.Position = 0;
using (var inputStream = new StreamReader(context.Request.InputStream))
{
strJSON = inputStream.ReadToEnd();
}
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
object serJsonDetails = javaScriptSerializer.Deserialize(strJSON, typeof(object));
// go and process the serJsonDetails object
or VB
Dim strJSON = [String].Empty
context.Request.InputStream.Position = 0
Using inputStream = New StreamReader(context.Request.InputStream)
strJSON = inputStream.ReadToEnd()
End Using
Dim javaScriptSerializer As New JavaScriptSerializer()
Dim serJsonDetails As Object = javaScriptSerializer.Deserialize(strJSON, GetType(Object))
' go and process the serJsonDetails object
You could just read the Request stream (Request.GetRequestStream) and use Json.NET to deserialize to an object.
You could use MVC4 and the built in object mapping.
There's many options. Perhaps you should read up on them more so that you have an idea of their capabilities and drawbacks.
Probably Request.Form (here) will help you get the JSON, if you know the content of the post, and then you need something like json.net library to get the object, or you can simply search the string using regex or keywords
Or if you can direct the post to a web service(asmx) instead of a web page, those services will parse the json for you
If you need to read raw post data twice or many times, i advice you to use this code.
string postBodyJson = null;
MemoryStream ms = new MemoryStream();
Request.InputStream.Position = 0;
Request.InputStream.Seek(0, SeekOrigin.Begin);
Request.InputStream.CopyTo(ms);
ms.Position = 0;
ms.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(ms))
{
postBodyJson = reader.ReadToEnd();
}

Multiline textbox rendering issue in asp.net

I have developed a client page for wcf service. basically my page has one button and two textboxes one is for loading request xml from xml file, and another one is displaying response xml.
Problem is that after any error got from wcf service my request xml textbox ignores all xml nodes it just displays node values with even spaces.
this is working in one machine it is not working in another machine.
two machines are windows xp os, ie 7.
<TextBox ID="requesttextbox" runat="server" TextMode="MultiLine" Width="470px"
Height="300px" Wrap="false/>
button click code something like this
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.XmlResolver = null;
xmlDoc.LoadXml(requesttextbox.Text);
HttpWebRequest objHttpWebRequest = null;
HttpWebResponse objHttpWebResponse = null;
string strFinalRequestXML = xmlDoc.OuterXml;
objHttpWebRequest = (HttpWebRequest)WebRequest.Create("RequestURL");
objHttpWebRequest.Method = "POST";
objHttpWebRequest.Accept = "xml";
objHttpWebRequest.ContentType = "application/xml; charset=utf-8";
objHttpWebRequest.Timeout = 300000;
objHttpWebRequest.ContentLength = strFinalRequestXML.Length;
System.IO.StreamWriter sw = new System.IO.StreamWriter (objHttpWebRequest.GetRequestStream());
sw.Write(strFinalRequestXML);
sw.Close();
try
{
objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();
Stream streamResponseText = objHttpWebResponse.GetResponseStream();
StreamReader srFinalResponseText = new StreamReader(streamResponseText, Encoding.UTF8);
txtResponse.Text = string.Empty;
// formattin xml string to as xml nodes to display in textbox
System.Xml.Linq.XElement element = System.Xml.Linq.XElement.Parse(srFinalResponseText.ReadToEnd());
txtResponse.Text = element.ToString();
strStatusCode = objHttpWebResponse.StatusCode.GetHashCode().ToString();
}
catch (WebException objWebException)
{
}
For example:
request xml <node>test</node> <node1>test;</node1> inside request xml text box.
after error from wcf display as " test test".
i have no clue for this problem.
If you are truly using WCF, you shouldn't have to manually call the URL using HttpWebRequest and HttpWebResponse, nor should you have to parse the XML by hand. You should add the service to the Service References, see:
http://msdn.microsoft.com/en-us/library/bb628652.aspx
If it is not WCF, but instead a regular SOAP web service, you can still add it as a Web Reference, see:
http://msdn.microsoft.com/en-us/library/bb628649.aspx
Then you can write code that is a little easier:
using (var client = new MyService.MyServiceClient())
{
string foo;
foo = client.MyMethod();
}
As for the text display, there is nothing in your catch handler, so I'm not sure where the textbox would be getting any sort of value. You must be setting it somewhere outside the code snippet you have provided.

Export a data set to Excel and raise a file download dialog from an asp.net web method

I am using the following code to export a data set to an Excel sheet.
[WebMethod]
public static void ExporttoExcel()
{
DataSet ds;
productfactory pf=new productfactory();
ds = pf.getproducts();
HttpResponse response = HttpContext.Current.Response;
// first let's clean up the response.object
response.Clear();
response.Charset = "";
response.ContentEncoding = System.Text.Encoding.Default;
// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"products.xls\"");
// create a string writer
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\products.xls";
response.Write(sw.ToString());
// response.End();
}
}
}
The problem is that it's not raising file download and hence no export is taking place. The same code works fine in a normal method. But with the web method it's not working.
I suggest to make an HttpHandler ending in ashx, and place inside him your code that create the excel file.
then call it from your javascript code like that.
document.location.href = "ExporttoExcel.ashx";
The problem is that WebMethods are not designed to allow you to interact with the Response object (evident in that it wasn't available and you had to use HttpContext.Current.Response to get to it). WebMethods are designed to be blackbox to the user. They will perform and action and/or return a value.
Perhaps you can give us a better idea of what you are trying to accomplish and we can suggest an alternate solution.
u can use to create a dynamic iframe with URL set to the Web Handler to generate the Excel this will raise the file download with out posting the current page.

Hijack output from aspx

I want to convert an aspx page to PDF using a component that can convert Html to PDF. Is it possible to, during post back, redirect the output from the aspx-page and send it as a stream or string to a HtmlToPdf method?
protected override void Render(HtmlTextWriter writer)
{
// setup a TextWriter to capture the page markup
TextWriter tw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(tw);
// render the page into our surrogate TextWriter
base.Render(htw);
// convert the TextWriter markup to a string
string pageSource = tw.ToString();
if (convertToPDF)
{
// convert the page markup to a pdf
// eg, byte[] pdfBytes = HtmlToPdf(pageSource);
}
// write the page markup into the output stream
writer.Write(pageSource);
}
Have you tried to send the value returned from "HttpContext.Current.Response.OutputStream;" in the postback ?
Hi I think that the way to do this would be to use the Reponse.Filter property to intercept and alter the HTML being sent to a page.
There's a tutorial video and sample code in both VB.net and C# on this page on the ASP.net website:
http://www.asp.net/learn/videos/video-450.aspx
You would write an HttpFilter that is attached to the request. This is code that can change the output after it has been written by the ASP.NET Page's Render step.
This article shows how to do this (they change the output from HTML to valid XHTML, but the idea is the same).

Resources