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

使用 SMTP 服务器在 ASP.NET 中发送带附件的电子邮件

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.44/5 (30投票s)

2005年6月28日

2分钟阅读

viewsIcon

458527

downloadIcon

23379

本文介绍了如何使用 SMTP 服务器发送带附件的邮件,以及如何配置 SMTP 服务器。

引言

此应用程序演示了如何使用 SMTP 服务器发送带附件的邮件,以及如何配置默认 SMTP 服务器。 System.Web.Mail 命名空间提供了在 .NET 中发送电子邮件的类。 涉及的类有 MailMessage(管理邮件消息的内容)和 MailAttachment(管理邮件附件)。

设置 SMTP 服务器

在运行应用程序之前,您需要确认这些服务器设置

  1. 确保 SMTP 服务器正在运行,只有这样才能中继邮件。 如果不是,打开 IIS 窗口,在 IIS 中查找左侧树视图中的“本地计算机”,您将在其下看到“默认 SMTP 虚拟服务器”,如果不可用,则需要安装它。
  2. 要配置“默认 SMTP 虚拟服务器”,请右键单击它,进入“属性”,选择“访问”选项卡,然后单击“中继”按钮。 在选择“仅限以下列表”单选按钮后,您应该看到本地 IP 地址:“127.0.0.1”,如果不存在,您需要添加它。
  3. 如果您使用“localhost”或“127.0.0.1”作为 SmtpMail.SmtpServer,请确保“允许匿名访问”。 要允许访问,请打开 IIS。 找到 SMTP 虚拟服务器,然后右键单击并选择属性。 在访问选项卡上,单击身份验证按钮。 确保只选中“匿名访问”复选框。
  4. 使用在 SmtpMail.SmtpServer 上存在的真实 fromto 地址。 不要使用无效地址。

获取对象

最初,我们需要为 MailMessage 类创建一个新对象,我们将其称为“mailMessage”。 然后,我们将此对象的 FromToCcBccSubjectBody 属性以及 BodyFormat 设置为 Web 窗体上的值

 /* Create a new blank MailMessage */
 MailMessage mailMessage = new MailMessage ();  
 mailMessage.From = txtSender.Text;
 mailMessage.To = txtReceiver.Text;
 mailMessage.Cc = txtCc.Text;
 mailMessage.Bcc = txtBcc.Text;
 mailMessage.Subject = txtSubject.Text;
 mailMessage.Body = txtBody.Text;
 /* Set the properties of the MailMessage to the
    values on the form  */
 if (rblMailFormat.SelectedItem.Text == "Text")
    mailMessage.BodyFormat = MailFormat.Text;
 else
    mailMessage.BodyFormat = MailFormat.Html;

制作附件

要制作附件,我们需要使用 MailAttachment 类,为此我们创建一个对象 attach。 以下代码块检查 Web 窗体的打开文件对话框(打开文件对话框是一个 HTML 文件字段控件,我们在其中添加了 runat="server" 属性)。 如果有值,则上传该文件,将其保存在服务器上,并将其作为附件添加到电子邮件中。

/* Beginning of Attachment1 process   & 
   Check the first open file dialog for a attachment */
if (inpAttachment1.PostedFile != null)
{
/* Get a reference to PostedFile object */
HttpPostedFile attFile = inpAttachment1.PostedFile;
 /* Get size of the file */
 int attachFileLength = attFile.ContentLength; 
 /* Make sure the size of the file is > 0  */
 if (attachFileLength > 0)
 {
 /* Get the file name */
 strFileName = Path.GetFileName(inpAttachment1.PostedFile.FileName);
 /* Save the file on the server */      
 inpAttachment1.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 */      
 mailMessage.Attachments.Add(attach);
 /* Store the attach filename so we can delete it later */
 attach1 = strFileName;
 }
}

现在,我们发送带附件的电子邮件

SmtpMail.SmtpServer = "127.0.0.1";
(Or)
SmtpMail.SmtpServer.Insert(0,"127.0.0.1");
SmtpMail.Send (mailMessage);

最后,如果有附件,我们需要将其删除

/* Delete the attachements if any */
 if (attach1 != null)
  File.Delete(Server.MapPath(attach1));
 if (attach2 != null)
  File.Delete(Server.MapPath(attach2));
 if (attach3 != null)
       File.Delete(Server.MapPath(attach3));

设置 SMTP 邮件服务器的各种方法

SmtpMail.SmtpServer = "localhost"

SmtpMail.SmtpServer = "127.0.0.1"

SmtpMail.SmtpServer.Insert( 0, 
    "127.0.0.1 or your mail server name here")

您需要将“localhost”替换为 Windows 桌面计算机上的 SMTP 邮件服务器的名称或 IP 地址,或者您可以插入服务器,但“localhost”是默认值,通常可以工作。

© . All rights reserved.