Asp.net 如何通过电子邮件发送图像而不在发送前将其保存到文件夹

Asp.net 如何通过电子邮件发送图像而不在发送前将其保存到文件夹,asp.net,file-upload,Asp.net,File Upload,我正在使用fileupload将图像上载到asp.net网站,我需要通过电子邮件发送上载的图像。为此,我正在将图像保存到服务器上的一个文件夹中,然后我正在发送图像,但我是否可以在发送图像之前不将其保存到文件夹中,或者如果我需要将其保存到文件夹中,是否可以在发送后立即将其删除? 谢谢 我的代码: string filename = Path.GetFileName(file.FileName); file.SaveAs(Server.MapPath("UploadI

我正在使用fileupload将图像上载到asp.net网站,我需要通过电子邮件发送上载的图像。为此,我正在将图像保存到服务器上的一个文件夹中,然后我正在发送图像,但我是否可以在发送图像之前不将其保存到文件夹中,或者如果我需要将其保存到文件夹中,是否可以在发送后立即将其删除? 谢谢

我的代码:

string filename = Path.GetFileName(file.FileName);
                file.SaveAs(Server.MapPath("UploadImages/" + filename));
                string attachmentPath = Server.MapPath("UploadImages/" + filename);
                Attachment inline = new Attachment(attachmentPath);
                inline.ContentDisposition.Inline = true;
                inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
                inline.ContentType.Name = Path.GetFileName(attachmentPath);
                mail.Attachments.Add(inline);
using System.Net.Mail;

[WebMethod]
public void SendFile(HttpPostedFileBase file)
{
    //...setup mail message etc
    Attachment attachment = new Attachment(file.InputStream, file.FileName);
    message.Attachments.Add(attachment);
}