关于ASP.NET邮件发送功能的问题

昨天接到任务,要搞一个邮件发送功能,找了一下blogs内的相关文章,其中宝玉的文章给了我很大启发。但是由此也引发了一些新的困惑。程序清单如下:
HTML部分:
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="jobdiary._default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>default</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <asp:Label id="lblServer" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 16px" runat="server"
    Width="104px">Server:</asp:Label>
   <asp:Label id="lblMessage" style="Z-INDEX: 121; LEFT: 16px; POSITION: absolute; TOP: 280px"
    runat="server" Width="104px">Message:</asp:Label>
   <asp:Label id="lblMailFormat" style="Z-INDEX: 120; LEFT: 16px; POSITION: absolute; TOP: 248px"
    runat="server" Width="104px">Mail Format:</asp:Label>
   <asp:Label id="lblSubject" style="Z-INDEX: 119; LEFT: 16px; POSITION: absolute; TOP: 224px"
    runat="server" Width="104px">Subject:</asp:Label>
   <asp:Label id="lblBCC" style="Z-INDEX: 118; LEFT: 16px; POSITION: absolute; TOP: 200px" runat="server"
    Width="104px">BCC:</asp:Label>
   <asp:Label id="lblCC" style="Z-INDEX: 117; LEFT: 16px; POSITION: absolute; TOP: 176px" runat="server"
    Width="104px">CC:</asp:Label>
   <asp:TextBox id="txtSubject" style="Z-INDEX: 114; LEFT: 128px; POSITION: absolute; TOP: 224px"
    tabIndex="9" runat="server">Test of CDONT</asp:TextBox>
   <asp:TextBox id="txtBCC" style="Z-INDEX: 113; LEFT: 128px; POSITION: absolute; TOP: 200px" tabIndex="8"
    runat="server"></asp:TextBox>
   <asp:TextBox id="txtCC" style="Z-INDEX: 112; LEFT: 128px; POSITION: absolute; TOP: 176px" tabIndex="7"
    runat="server"></asp:TextBox>
   <asp:TextBox id="txtTo" style="Z-INDEX: 111; LEFT: 128px; POSITION: absolute; TOP: 152px" tabIndex="6"
    runat="server"></asp:TextBox>
   <asp:TextBox id="txtFrom" style="Z-INDEX: 110; LEFT: 128px; POSITION: absolute; TOP: 128px" tabIndex="5"
    runat="server"></asp:TextBox>
   <asp:TextBox id="txtPassword" style="Z-INDEX: 109; LEFT: 152px; POSITION: absolute; TOP: 88px"
    tabIndex="4" runat="server" Width="128px" TextMode="Password"></asp:TextBox>
   <asp:TextBox id="txtUserName" style="Z-INDEX: 108; LEFT: 152px; POSITION: absolute; TOP: 64px"
    tabIndex="3" runat="server" Width="128px"></asp:TextBox>
   <asp:Label id="lblTo" style="Z-INDEX: 106; LEFT: 16px; POSITION: absolute; TOP: 152px" runat="server"
    Width="104px">To:</asp:Label>
   <asp:Label id="lblFrom" style="Z-INDEX: 105; LEFT: 16px; POSITION: absolute; TOP: 128px" runat="server"
    Width="104px">From:</asp:Label>
   <asp:Label id="lblPassword" style="Z-INDEX: 104; LEFT: 40px; POSITION: absolute; TOP: 88px"
    runat="server" Width="104px">Password:</asp:Label>
   <asp:Label id="lblUserName" style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 64px"
    runat="server" Width="104px">User Name:</asp:Label>
   <asp:CheckBox id="chkRequireLogin" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 40px"
    runat="server" Width="232px" Text="Server Requires Login" tabIndex="2"></asp:CheckBox>
   <asp:TextBox id="txtServer" style="Z-INDEX: 107; LEFT: 128px; POSITION: absolute; TOP: 16px"
    runat="server" tabIndex="1"></asp:TextBox>
   <asp:DropDownList id="ddlMailFormat" style="Z-INDEX: 115; LEFT: 128px; POSITION: absolute; TOP: 248px"
    tabIndex="10" runat="server" Width="152px"></asp:DropDownList>
   <asp:TextBox id="txtMessage" style="Z-INDEX: 116; LEFT: 16px; POSITION: absolute; TOP: 304px"
    tabIndex="11" runat="server" Width="280px" Height="136px" Rows="20" MaxLength="255">This is a test of CDONT Functionality.</asp:TextBox>
   <asp:Button id="btnSend" style="Z-INDEX: 122; LEFT: 240px; POSITION: absolute; TOP: 456px" tabIndex="12"
    runat="server" Text="Send"></asp:Button>
   <asp:Label id="lblResult" style="Z-INDEX: 123; LEFT: 24px; POSITION: absolute; TOP: 496px"
    runat="server" Width="100%" Height="200px"></asp:Label>
  </form>
 </body>
</HTML>

C#部分:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Mail;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace jobdiary
{
 /// <summary>
 /// _default 的摘要说明。
 /// </summary>
 public class _default : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Label lblServer;
  protected System.Web.UI.WebControls.Label lblMessage;
  protected System.Web.UI.WebControls.Label lblMailFormat;
  protected System.Web.UI.WebControls.Label lblSubject;
  protected System.Web.UI.WebControls.Label lblBCC;
  protected System.Web.UI.WebControls.Label lblCC;
  protected System.Web.UI.WebControls.TextBox txtSubject;
  protected System.Web.UI.WebControls.TextBox txtBCC;
  protected System.Web.UI.WebControls.TextBox txtCC;
  protected System.Web.UI.WebControls.TextBox txtTo;
  protected System.Web.UI.WebControls.TextBox txtFrom;
  protected System.Web.UI.WebControls.TextBox txtPassword;
  protected System.Web.UI.WebControls.TextBox txtUserName;
  protected System.Web.UI.WebControls.Label lblTo;
  protected System.Web.UI.WebControls.Label lblFrom;
  protected System.Web.UI.WebControls.Label lblPassword;
  protected System.Web.UI.WebControls.Label lblUserName;
  protected System.Web.UI.WebControls.CheckBox chkRequireLogin;
  protected System.Web.UI.WebControls.TextBox txtServer;
  protected System.Web.UI.WebControls.DropDownList ddlMailFormat;
  protected System.Web.UI.WebControls.TextBox txtMessage;
  protected System.Web.UI.WebControls.Button btnSend;
  protected System.Web.UI.WebControls.Label lblResult;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   this.ddlMailFormat.Items.Add( new ListItem( System.Enum.GetName(typeof(System.Web.Mail.MailFormat), MailFormat.Text), MailFormat.Text.ToString()));
   this.ddlMailFormat.Items.Add( new ListItem( System.Enum.GetName(typeof(System.Web.Mail.MailFormat), MailFormat.Html), MailFormat.Html.ToString()));
  
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void btnSend_Click(object sender, System.EventArgs e)
  {
   try {
            MailMessage Message = new MailMessage();

            if( this.chkRequireLogin.Checked ) {
                Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1" ); //basic authentication
                Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", txtUserName.Text ); //set your username here
                Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", txtPassword.Text ); //set your password here
            }
            Message.To = txtTo.Text;
            Message.Cc = txtCC.Text;
            Message.Bcc = txtBCC.Text;
            Message.From = txtFrom.Text;
            Message.Subject = txtSubject.Text;
            Message.BodyFormat = (MailFormat)System.Enum.Parse(typeof(MailFormat), ddlMailFormat.SelectedValue );
            Message.Body = txtMessage.Text;

            SmtpMail.SmtpServer = txtServer.Text;
            SmtpMail.Send(Message);

            lblResult.Text = "Mail Sent";
        }
        catch( Exception ex ) {
            lblResult.Text = ex.ToString();
        }

  }
 }
}


注意宝玉的说明:

在Asp.Net中邮件发送非常容易,一般都需要Smtp服务器,但是一些免费信箱的Smtp服务器(如smtp.163.com)都需要账号密码验证。在MailMessage对象的Filds集合中加入以下内容即可:

Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1" );
//basic authentication
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", txtUserName.Text );
// 设置smtp服务器登录账号(如您163.com的信箱账号)
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", txtPassword.Text );
// 设置smtp服务器登录密码 (如您的163.com的信箱账号密码)

但是使用免费的Smtp服务器一定要注意一个问题,就是发送邮件的账号(即Message.From)必须和上面的Smtp服务器登录账号一致,否则将会导致找不到cdo对象的异常。

里面使用MailMessage对象在.net1.1中的新属性FILEDS来调用微软的cdo对象进行验证,很多人对cdo会比较陌生,下面我就转贴一下微软的原文解释:

Overview of CDO

Microsoft® Collaboration Data Objects (CDO) is a technology for building messaging or collaboration applications. The current version of CDO is 1.2.1. In versions previous to 1.1, CDO was called OLE Messaging; in version 1.1 it was called Active Messaging. It is designed to simplify the creation of applications with messaging functionality, or to add messaging functionality to existing applications. For example, CDO and Active Server Pages enable you to add script to a Web site to provide support for creating, sending, and receiving e-mail as well as participating in discussions and other public folder applications.

CDO does not represent a new messaging model, but rather an additional scripting interface to the Messaging Application Programming Interface (MAPI) model.

CDO is made available through the two CDO libraries. They are described in detail in Introduction and Overview of CDO Rendering.

These libraries expose programmable messaging objects (including folders, messages, recipient addresses, attachments, and other messaging components), which are extensions to the programmable objects offered as part of Microsoft® Visual Basic®, such as forms and controls.

原始文档网址:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/exchanchor/htms/msexchsvr_cdo_top.asp
希望对大家有所助益。

原文地址:https://www.cnblogs.com/pipisong/p/121366.html