【发布时间】:2011-06-30 00:08:51
【问题描述】:
我正在尝试创建一个将 html 转换为 pdf 文档的应用程序,然后将其作为附件通过电子邮件发送。
我收到此错误The underlying connection was closed: The connection was closed unexpectedly.
尝试了谷歌搜索,但运气不佳;为什么会出现这种情况的任何提示?
编辑:
错误出现在第 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 = "test@test.com";
string sendTo = "test@test.com";
//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>
这是整个 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
我还应该说电子邮件发送和附件工作正常(已经测试)。
【问题讨论】:
-
哪一行会抛出这个异常? html2pdf.Run()? SmtpMail.Send()?
-
您需要隔离问题。该错误表明这是在网络使用时发生的,无需进一步分析,这似乎是通过电子邮件发送时发生的。另外,对未来发展的说明:处理Disposable对象。
-
@Mr.失望我忘了包括跟踪。它发生在 pdf 运行命令中。