如何从经典 asp 页面上的 Web 配置文件获取邮件设置

How do I get mailsettings form webconfig file on a classic asp page

目前正在处理一个经典的 asp 页面,想了解当页面上发生事件时人们将如何发送电子邮件。使用服务器端编码,即没有javascript;我将如何阅读经典 asp 页面上 web.config 文件中的邮件设置。

您可以使用 MSXML2.DOMDocument.6.0 对象加载和读取 web.config 文件并提取元素。

示例web.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="smtp@example.com">
        <network 
            defaultCredentials="true" 
            host="localhost" 
            port="587" 
            userName="username" 
            password="password"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

阅读使用MSXML2.DOMDocument.6.0:

<%

    Dim oXML, oNode
    
    Set oXML = Server.CreateObject("MSXML2.DOMDocument.6.0")
    
        oXML.async = False    
        oXML.load Server.MapPath("/web.config")
        
        ' smtp attributes
        
        Set oNode = oXML.selectSingleNode("configuration/system.net/mailSettings/smtp")
                
            Response.Write "<b>deliveryMethod:</b> " & oNode.GetAttribute("deliveryMethod") & "<br>"
            Response.Write "<b>from:</b> " & oNode.GetAttribute("from") & "<br>"
        
        ' network attributes
                                            
        Set oNode = oXML.selectSingleNode("configuration/system.net/mailSettings/smtp/network")
                
            Response.Write "<b>defaultCredentials:</b> " & cBool(oNode.GetAttribute("defaultCredentials")) & "<br>"
            Response.Write "<b>host:</b> " & oNode.GetAttribute("host") & "<br>"
            Response.Write "<b>port:</b> " & cInt(oNode.GetAttribute("port")) & "<br>"
            Response.Write "<b>userName:</b> " & oNode.GetAttribute("userName") & "<br>"
            Response.Write "<b>password:</b> " & oNode.GetAttribute("password")
                                    
        Set oNode = Nothing
        
    Set oXML = Nothing
    
%>

输出:

deliveryMethod: Network
from: smtp@example.com
defaultCredentials: True
host: localhost
port: 587
userName: username
password: password