C# 我还可以如何使用ASP.NET通过电子邮件发送文件?

C# 我还可以如何使用ASP.NET通过电子邮件发送文件?,c#,asp.net,file,C#,Asp.net,File,我在ASP.NET页面中使用下面的代码,通过电子邮件将文件从用户的家用计算机发送到用于接收需要复印的工作的邮箱。在我们的网络中发送文件时,下面的代码工作正常,但当我们的用户在家并通过SSL VPN连接时失败。我们的VPN中似乎存在一个漏洞,在通过电子邮件发送文件之前,它不允许将文件临时保存在Web服务器上。对于如何将文件附加到ASP.NET页面并通过电子邮件发送文件而不将其存储在web服务器上,是否有人可以提供其他建议?非常感谢简 MailMessage mail = new MailMessa

我在ASP.NET页面中使用下面的代码,通过电子邮件将文件从用户的家用计算机发送到用于接收需要复印的工作的邮箱。在我们的网络中发送文件时,下面的代码工作正常,但当我们的用户在家并通过SSL VPN连接时失败。我们的VPN中似乎存在一个漏洞,在通过电子邮件发送文件之前,它不允许将文件临时保存在Web服务器上。对于如何将文件附加到ASP.NET页面并通过电子邮件发送文件而不将其存储在web服务器上,是否有人可以提供其他建议?非常感谢简

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
        {

        }
MailMessage mail=newmailmessage();
mail.From=txtFrom.Text;
mail.To=txtTo.Text;
mail.Cc=txtFrom.Text;
mail.Subject=txtSubject.Text;
mail.Body=“test”
mail.BodyFormat=MailFormat.Html;

字符串strdir=“E:\\TEMPforReprographics\\”// 如果您使用
System.Net.Mail
命名空间中的类,则其中的
附件
类支持流,因此假设您可以先将其作为流读入内存,然后可以将其添加到附件中,这样您就不必存储任何文件

更多信息(和示例)请参见:


使用
字符串strdir=Path.GetTempPath()

对于如何将文件附加到ASP.NET页面并通过电子邮件发送文件而不将其存储在web服务器上,是否有人可以提供其他建议?非常感谢简

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
        {

        }

那是不可能的。承载ASPX页面的web服务器在进一步处理该文件之前必须从客户端接收该文件。

在我的脑海中,创建附件,如下所示:

txtFile.PostedFile.InputStream.Position = 0  
mail.Attachments.Add(new MailAttachment(txtFile.PostedFile.InputStream, strfilename  ));
这将允许您创建附件,而无需将其保存到磁盘