简体   繁体   English

我还能如何使用ASP.NET通过电子邮件发送文件?

[英]How else can I email a file using ASP.NET?

I'm using the code below in a ASP.NET page to send a file via email from our users home computer to a mailbox that is used for receiving work that needs photocopying. 我正在ASP.NET页中使用以下代码通过电子邮件将文件从用户家用计算机发送到邮箱,该邮箱用于接收需要复印的工作。 The code below works fine when sending a file within our network but fails when our users are at home and connected via our SSL VPN, there appears to be a bug in our VPN where it doesn't allow the file to be temporarily saved on the webserver before being sent via email. 当我们在网络中发送文件时,以下代码可以正常工作,但当用户在家中并通过SSL VPN连接时,以下代码将失败,我们的VPN中似乎存在一个错误,该错误不允许将该文件临时保存在网络服务器,然后再通过电子邮件发送。 Can anyone offer any other suggestions on how to attach a file to a ASP.NET page and have the file sent via email without storing it on the web server? 谁能提供关于将文件附加到ASP.NET页并通过电子邮件发送文件而不将其存储在Web服务器上的其他建议? Many thanks Jane. 非常感谢简。

MailMessage mail = new MailMessage();
        mail.From = txtFrom.Text;
        mail.To = txtTo.Text;
        mail.Cc = txtFrom.Text;
        mail.Subject = txtSubject.Text;
        mail.Body = "test"
        mail.BodyFormat = MailFormat.Html;

        string strdir = "E:\\TEMPforReprographics\\"; //<-------PROBLEM AREA
        string strfilename = Path.GetFileName(txtFile.PostedFile.FileName);

        try
        {
            txtFile.PostedFile.SaveAs(strdir + strfilename);
            string strAttachment = strdir + strfilename;

            mail.Attachments.Add(new MailAttachment(strdir + strfilename));
            SmtpMail.SmtpServer = "172.16.0.88";
            SmtpMail.Send(mail);
            Response.Redirect("Thanks.aspx", true);
        }
        catch
        {
           Response.Write("An error has occured sending the email or uplocading the file.");
        }
        finally
        {

        }

If you use the classes in the System.Net.Mail namespace, the Attachment class in there supports streams, so assuming you can read it in to memory as a stream first you can then add that to the attachment, that way you never have to store any files. 如果您使用System.Net.Mail命名空间中的类,则其中的Attachment类支持流,因此假设您可以首先将其作为流读入内存,然后可以将其添加到附件中,这样就不必存储任何文件。

More information (and a sample) here: 更多信息(和示例)在这里:

http://msdn.microsoft.com/en-us/library/6sdktyws.aspx http://msdn.microsoft.com/en-us/library/6sdktyws.aspx

Can anyone offer any other suggestions on how to attach a file to a ASP.NET page and have the file sent via email without storing it on the web server? 谁能提供关于将文件附加到ASP.NET页并通过电子邮件发送文件而不将其存储在Web服务器上的其他建议? Many thanks Jane. 非常感谢简。

That's impossible. 这不可能。 A web server hosting an ASPX page has to receive the file from the client before processing it any further. 承载ASPX页面的Web服务器必须先从客户端接收文件,然后再进行进一步处理。

Use string strdir = Path.GetTempPath(); 使用string strdir = Path.GetTempPath(); ?

Off the top of my head, create the attachment like: 在我的头顶上,创建附件,如下所示:

txtFile.PostedFile.InputStream.Position = 0  
mail.Attachments.Add(new MailAttachment(txtFile.PostedFile.InputStream, strfilename  ));

That should allow you to create the attachment, without saving it to disk. 那应该允许您创建附件,而无需将其保存到磁盘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在ex.html c#中使用extern html文件作为附件并将其发送到电子邮件地址? - How I can use a extern html file for a attachment in asp.net c# and send it to a email address? 如何从Mutliple CheckBoxList选择检索输出并将其放置在ASP.Net的电子邮件文本文件中 - How Can I Retrieve the Output from Mutliple CheckBoxList Selections & Place in Email Text File in ASP.Net 我如何在asp.net mvc中发送确认电子邮件 - How can I send a Confirmation Email in asp.net mvc 如何在ASP.NET成员资格中还原电子邮件确认 - How I can revert email confirmation in ASP.NET membership 我怎么知道我的电子邮件是否是使用Asp.net收到和阅读的 - How can I know if my email is received and read using Asp.net 如何将电子邮件功能从ASP Classic迁移到ASP.NET? - How can I migrate email functionality from ASP Classic to ASP.NET? 如何在ASP.Net中加密文件并在Silverlight中解密? - How can I encrypt a file in ASP.Net and decrypt it in Silverlight? 如何在ASP.Net Core 2中返回CSV文件? - How can I return a CSV file in ASP.Net Core 2? 如何在ASP.NET中启动PDF文件? - How I can start a PDF File in ASP.NET? 我如何在asp.net MVC 3中导出为pdf文件 - How can I export to a pdf file in asp.net mvc 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM