zoukankan      html  css  js  c++  java
  • 在Asp.Net中使用SmtpMail发送邮件的方法

    在ASP中,就可以通过调用CDONTS组件发送简单邮件,在ASP.Net中,自然也可以。不同的是,.Net Framework中,将这一组件封装到了System.Web.Mail命名空间中。

    一个典型的邮件发送程序如下:
    <%@ Import Namespace="System.Web.Mail" %>
    <script runat="server">
    MailMessage mail=new MailMessage();
     mail.From="service@brookes.com";
     mail.To="brookes@brookes..com";
     mail.BodyFormat=MailFormat.Text;
     mail.Body="a test smtp mail.";
     mail.Subject="r u ok?";
     SmtpMail.SmtpServer="localhost";
     SmtpMail.Send(mail);
    </script>

    通常情况下,系统调用IIS自带的默认SMTP虚拟服务器就可以实现邮件的发送。但是也经常会遇到这样的错误提示:

    The server rejected one or more recipient addresses. The server response was: 550 5.7.1 Unable to relay for brookes@brookes.com

     产生这个错误的原因除了地址错误的可能外,还有一个重要原因。如上文提到的,IIS并不带有真正的邮件功能,只是借用一个“SMTP虚拟服务器”实现邮件的转发。在MSDN中,有如下提示:

    如果本地 SMTP 服务器(包括在 Windows 2000 和 Windows Server 2003 中)位于阻塞任何直接 SMTP 通信量(通过端口 25)的防火墙之后,则需要查找网络上是否有可用的智能主机能用来中转发往 Internet 的 SMTP 消息。
    智能主机是一个 SMTP 服务器,它能够中转从内部 SMTP 服务器直接发送到 Internet 的外出电子邮件。智能主机应能同时连接到内部网络和 Internet,以用作电子邮件网关。

    打开默认SMTP虚拟服务器-属性-访问-中继限制,可以看到,这种转发或者中继功能受到了限制。在限制列表中,添加需要使用此服务器的主机的IP地址,就可以解决上文提到的问题。

    如果不使用IIS自带的SMTP虚拟服务器而使用其他真正的邮件服务器,如IMail,Exchange等,常常遇到服务器需要寄送者身份验证的问题(ESMTP)。在使用需要验证寄送者身份的服务器时,会出现错误:

    The server rejected one or more recipient addresses. The server response was: 550 not local host ckocoo.com, not a gateway

    以前在ASP中,遇到这种问题没有什么解决的可能,只能直接使用CDO组件(CDONTS的父级组件):
     conf.Fields[CdoConfiguration.cdoSMTPAuthenticate].Value=CdoProtocolsAuthentication.cdoBasic;
     conf.Fields[CdoConfiguration.cdoSendUserName].Value="brookes";
     conf.Fields[CdoConfiguration.cdoSendPassword].Value="XXXXXXX";

    在.Net Framework 1.1中,显然对这一需求有了考虑,在MailMessage组件中增加了Fields集合易增加ESMTP邮件服务器中的寄送者身份验证的问题。不过,这一方法仅适用于.Net Framework 1.1,不适用于.Net Framework 1.0版本。带有寄送者身份验证的邮件发送程序如下:


    <%@ Import Namespace="System.Web.Mail" %>
    <script runat="server">
    MailMessage mail=new MailMessage();
     mail.From="service@brookes.com";
     mail.To="brookes@brookes.com";
     mail.BodyFormat=MailFormat.Text;
     mail.Body="a test smtp mail.";
     mail.Subject="r u ok?";
     mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication 
     mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "brookes"); //set your username here 
     mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "walkor"); //set your password here 
     SmtpMail.SmtpServer="lsg.moon.net";
     SmtpMail.Send(mail);
    </script>

  • 相关阅读:
    JavaScript 电话手机号码正则表达式
    查找和删除sqlserver数据库中的重复记录
    配置文件app.config
    数据类型 ntext 和 varchar 在 equal to 运算符中不兼容的错误信息
    用jscript处理repeater生成的表格, 实现分页打印
    我在Repeater控件中有CheckBox控件,我怎么能选中CheckBox控件后,Repeater控件重新绑定一下?
    种方式遍历repeater中的CheckBox全选
    Repeater导出为excel格式
    给Repeater、Datalist和Datagrid增加自动编号
    js中对datagrid ,repeater的checkbox进行全选反选
  • 原文地址:https://www.cnblogs.com/gc2013/p/4174483.html
Copyright © 2011-2022 走看看