65.9K
CodeProject 正在变化。 阅读更多。
Home

带有多个附件的 ASP.NET 电子邮件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.28/5 (31投票s)

2004年10月11日

3分钟阅读

viewsIcon

327886

downloadIcon

7203

一个简单的 Web 表单,允许用户上传多个附件以创建和发送电子邮件。

引言

此应用程序演示了使用 ASP.NET 可以完成的一些简单但有用的事情;上传、保存和删除文件,以及发送电子邮件。

背景

我们需要做三件事才能运行此代码,并且我假设您将在家里的 Windows 台式计算机上尝试此代码。

首先,您需要在 IIS 中创建一个虚拟目录来托管此 Web 应用程序,并设置写入权限,因为我们将保存和删除将作为电子邮件附件的文件。 在虚拟目录中,创建一个名为“attachments”的文件夹。 这就是我们将临时存储上传的文件以附加到电子邮件的位置。

要做的第二件事是确保 SMTP 服务器正在运行并且可以转发邮件。 从 IIS MMC 窗口中,在左侧选择“本地计算机”,您应该会看到“默认 SMTP 虚拟服务器”,如果看不到,则需要安装它。 右键单击它,进入“属性”,“访问”选项卡,然后单击“转发”按钮。 在选中“仅限以下列表”单选按钮的情况下,您应该看到本地 IP 地址:127.0.0.1,如果不存在,则需要添加它。

最后一件事是在应用程序中设置 SMTP 服务器。 在代码中,查找此行(第 149 行)

SmtpMail.SmtpServer = "localhost";

您需要将“localhost”替换为您的 SMTP 邮件服务器的名称或 IP 地址。 在 Windows 台式计算机上,“localhost”是默认值,通常有效。

使用代码

首先,我们创建一个新的 MailMessage 对象,我们称之为 "email"。 然后,我们将 FromToSubjectBody 属性以及此对象的 BodyFormatof 设置为 Web 表单上的值。

// Create a new blank MailMessage

 MailMessage email = new MailMessage ();

// Set the properties of the MailMessage to the

// values on the form

 if (rblMailFormat.SelectedItem.Text == "text")
  email.BodyFormat = MailFormat.Text;
 else
  email.BodyFormat = MailFormat.Html;
 email.From = txtSender.Text;
 email.To = txtReceiver.Text;
 email.Subject = txtSubject.Text;
 email.Body = txtBody.Text;

现在是这个小型 Web 应用程序的关键所在; 以下代码块检查 Web 表单的三个打开文件对话框中的第一个(打开文件对话框是一个 HTML 文件字段控件,我们已将 runat="server" 属性添加到其中)。 如果有值,则上传该文件,将其保存在服务器上,并作为附件添加到电子邮件中。 处理其他两个“打开文件”对话框也相同。

// Beginning of attachments processing

// Check the first open file dialog for a value

 if (ofdAttachment1.PostedFile != null)
 {
  // Get a reference to PostedFile object

  HttpPostedFile ulFile = ofdAttachment1.PostedFile;
  // Get size of the file

  int nFileLen = ulFile.ContentLength; 
  // Make sure the size of the file is > 0

  if( nFileLen > 0 )
  {
   // Get the file name

   strFileName = Path.GetFileName(ofdAttachment1.PostedFile.FileName);
   // Preced the file name with "attachments/" so 

   // the file is saved to our attachments directory

   strFileName = "attachments/" + strFileName;
   // Save the file on the server

   ofdAttachment1.PostedFile.SaveAs(Server.MapPath(strFileName));
   // Create the email attachment with the uploaded file

   MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
   // Attach the newly created email attachment

   email.Attachments.Add(attach);
   // Store filename so we can delete it later

   attach1 = strFileName;
  }
 }

然后我们发送电子邮件,最后删除附件。

// Set the SMTP server and send the email

 SmtpMail.SmtpServer = "localhost";
 SmtpMail.Send (email);

// Delete the attachements if any

 if (attach1 != null)
  File.Delete(Server.MapPath(attach1));

关注点

这个小项目展示了 System.Web.HttpPostedFileSystem.Web.Mail.SmtpMail 对象的实际用途。 HttpPostedFile 对象可能是让 Web 用户上传文件的最简单方法。 我还尝试使用 FileStream 创建文件附件,希望能够创建附件而无需先保存文件。 我还没有弄清楚这一点。

历史

我对此文章和代码做了一个小更新,但它基本上是同一个应用程序。 有人指出我没有包含将“attachments/”添加到第二个和第三个附件的文件名开头的行。 这是代码中唯一更改的内容。 我还对我的解释和注释做了一些小的更改。

© . All rights reserved.