[英]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.