0

I am trying to create an app that converts html to a pdf doc then emails it as an attachment.

I am getting this error The underlying connection was closed: The connection was closed unexpectedly.

Tried Googling but didn't have much luck; any hints to why this might be coming up?

Edit: The error occurs at line 28: html2pdf.Run(Convert);

<%@ Import Namespace="System.Web.Mail" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Pdfizer" %>
<script language="c#" runat="server">        
    protected override void OnLoad(EventArgs e)
    {    
        sendApplication();    
    }

    private void sendApplication()
    {    
        string template = "X:\\path\\to\\file\\temp.htm"; //HTML Form to send    
        Stream steam = new FileStream("X:\\path\\to\\file\\app.pdf", FileMode.Create, FileAccess.Write, FileShare.Write); //Stream to write to file.
        string fileToSend = "X:\\path\\to\\file\\app.pdf"; //PDF to send(after creation)

        string smtpServer = "spamfilter1.com";
        string sendFrom = "[email protected]";
        string sendTo = "[email protected]";

        //Read template
        StreamReader templateFile = new StreamReader(template);
        string Convert = templateFile.ReadToEnd();
        templateFile.Close();

        //Convert to PDF
        HtmlToPdfConverter html2pdf = new HtmlToPdfConverter();
        html2pdf.Open(steam);
        html2pdf.AddChapter(@"_");
        html2pdf.Run(Convert);
        html2pdf.Close();

        //Send email and attachment
        MailMessage msg = new MailMessage();
        msg.To = sendTo;
        msg.From = sendFrom;
        msg.Subject = "New Application";
        msg.Body = Convert;
        MailAttachment attach = new MailAttachment(fileToSend);
        msg.Attachments.Add(attach);
        SmtpMail.SmtpServer = smtpServer;
        SmtpMail.Send(msg);
        lblStatus.Text = "Application Submitted.";
    }    
</script>   

Here is the entire Trace

Server Error in '/WebSite1' Application.
The underlying connection was closed: The connection was closed unexpectedly.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.

Source Error:

Line 26:         html2pdf.Open(steam);
Line 27:         html2pdf.AddChapter(@"_");
Line 28:         html2pdf.Run(Convert);
Line 29:         html2pdf.Close();
Line 30:         


Source File: c:\Users\Jeff\Documents\Visual Studio 2010\WebSites\WebSite1\sstnS.aspx    Line: 28

Stack Trace:

[WebException: The underlying connection was closed: The connection was closed unexpectedly.]
   System.Net.HttpWebRequest.GetResponse() +6111059
   System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy) +107
   System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy) +4012381
   System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) +69
   System.Xml.XmlTextReaderImpl.OpenAndPush(Uri uri) +171
   System.Xml.XmlTextReaderImpl.PushExternalEntityOrSubset(String publicId, String systemId, String baseUriStr, Uri& baseUri, String entityName) +252
   System.Xml.XmlTextReaderImpl.DtdParserProxy_PushExternalSubset(String systemId, String publicId) +46
   System.Xml.DtdParserProxy.System.Xml.IDtdParserAdapter.PushExternalSubset(String systemId, String publicId) +16
   System.Xml.DtdParser.ParseExternalSubset() +21
   System.Xml.DtdParser.ParseInDocumentDtd(Boolean saveInternalSubset) +4133593
   System.Xml.DtdParser.Parse(Boolean saveInternalSubset) +54
   System.Xml.DtdParser.System.Xml.IDtdParser.ParseInternalDtd(IDtdParserAdapter adapter, Boolean saveInternalSubset) +31
   System.Xml.XmlTextReaderImpl.ParseDtd() +72
   System.Xml.XmlTextReaderImpl.ParseDoctypeDecl() +219
   System.Xml.XmlTextReaderImpl.ParseDocumentContent() +453
   System.Xml.XmlTextReaderImpl.Read() +145
   System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) +114
   System.Xml.XmlDocument.Load(XmlReader reader) +114
   System.Xml.XmlDocument.LoadXml(String xml) +168
   Pdfizer.HtmlToPdfConverter.Run(String html) +134
   ASP.sstns_aspx.sendApplication() in c:\Users\Jeff\Documents\Visual Studio 2010\WebSites\WebSite1\sstnS.aspx:28
   ASP.sstns_aspx.OnLoad(EventArgs e) in c:\Users\Jeff\Documents\Visual Studio 2010\WebSites\WebSite1\sstnS.aspx:9
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1 

I should also say the Email sending and attachment works properly(already tested).

3
  • What line throws this exception? The html2pdf.Run()? The SmtpMail.Send()? Commented Jun 29, 2011 at 15:56
  • You need to isolate the problem. The error suggests this is happening at the point of network utilisation, without further analysis, that would seem to be when sending via email. Also, a note for future development: dispose of Disposable objects. Commented Jun 29, 2011 at 15:57
  • @Mr. Disappointment I forgot to include the trace. It happens at the pdf run command. Commented Jun 29, 2011 at 16:01

3 Answers 3

1

It looks like you're expecting the Run() method to put the data into the "steam" location, but it's instead trying to open a URI. Either the Open() method isn't telling it "here's where to save the data", or the Run() method isn't properly saving to that location. I would start by combing through the documentation for the library, since I'm not familiar with it myself.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you made me think of something. My template had: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="w3.org/1999/xhtml"> This was pulling from the site breaks it. Just changed it to <html>
0

Is html2pdf calling a webservice at all? or is it self contained?

Comments

0

Can you post the entire error message including the stack trace? I suspect that the error is happening when you are trying to send the email. The stack trace would verify that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.