以编程方式获取域的SMTP服务器? [英] Getting the SMTP server for a domain programmaticly?
问题描述
有没有办法从域获取主交换smtp服务器,例如Active Directory?
我开发了一个应该发送一些电子邮件的软件,我们运行这个使用混合版本的Exchange Server在许多不同域上的软件,但我无法弄清楚如何自动获取发送电子邮件时使用的服务器。
发送电子邮件的代码使用SmtpClient类C#。
提前谢谢。
您可以转换并使用下面提到的示例vbscript来获取SMTP服务器名称:
注意:以下编程示例仅供参考,不附带任何明示或暗示保证,包括但不限于适销性和/或特定用途适用性的暗示保证。此示例代码假定您熟悉所演示的编程语言以及用于创建和调试过程的工具。提供此示例代码仅用于说明目的,不应用于"生产环境"。
选项显式
昏暗的信号
昏暗的组件
昏暗的iAdRootDSE
Dim strNC
Dim strDefNC
Dim strQry
Dim RS
Dim strstmsrv
Dim svsSmtpserver
Dim crServerName
set conn = createobject(" ADODB.Connection")
set com = createobject(" ADODB.Command")
设置iAdRootDSE = GetObject(" LDAP:// RootDSE ")
strNC = iAdRootDSE。 Get("configurationNamingContext")
strDefNC = iAdRootDSE.Get(" defaultNamingContext")
conn.Provider =" ADsDSOObject"
conn.Open" ADs Provider"
com.ActiveConnection = Conn
com.ActiveConnection = Conn
Wscript.echo
Wscript.echo"SMTP Virtual Servers"
strQry ="< LDAP://" &安培; strNC& "> ;;(objectCategory = protocolCfgSMTPServer); name,distinguishedName; subtree"
com.ActiveConnection = conn
com.CommandText = strQry
设置RS = com.Execute
而不是RS.EOF
strstmsrv =" LDAP://" &安培; rs.fields(&distinguishedName")
set svsSmtpserver = getobject(strstmsrv)
crServerName = mid(svsSmtpserver.distinguishedName,instr(svsSmtpserver.distinguishedName," CN = Protocols,")+ 16, instr(svsSmtpserver.distinguishedName,",CN = Servers") - (instr(svsSmtpserver.distinguishedName," CN = Protocols,")+ 16))
wscript.echo
wscript.echo" ;服务器名称:" &安培; crServerName
RS.movenext
wend
Set RS = Nothing
Set com = Nothing
Set conn = Nothing
希望这有帮助
问候,
Brij Raj Singh
? Brijs Blogging ... 超越明显 < span style ="font-family:'Arial','sans-serif';颜色:#17365d; font-size:10pt"lang = EN-US>
Is there a way to get the primary exchange smtp server for a domain, from for example Active Directory?
I have developed a software that is supposed to send some e-mails, we run this software on a number of different domains with mixed versions of Exchange Server, but i cant really figure out how to automagicaly get what server to use when sending emails.
The code that sends Emails use the SmtpClient class in C#.
Thank you in advance.
You can convert and use below mentioned sample vbscript to get the SMTP server name:
'NOTE: Following programming examples is for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties 'of merchantability and/or fitness for a particular purpose. This sample code assumes that you are familiar with the programming language being demonstrated 'and the tools used to create and debug procedures. This sample code is provided for the purpose of illustration only and is not intended to be used in a 'production environment.
Option Explicit
Dim conn
Dim com
Dim iAdRootDSE
Dim strNC
Dim strDefNC
Dim strQry
Dim RS
Dim strstmsrv
Dim svsSmtpserver
Dim crServerNameset conn = createobject("ADODB.Connection")
set com = createobject("ADODB.Command")
Set iAdRootDSE = GetObject("LDAP://RootDSE")strNC = iAdRootDSE.Get("configurationNamingContext")
strDefNC = iAdRootDSE.Get("defaultNamingContext")conn.Provider = "ADsDSOObject"
conn.Open "ADs Provider"
com.ActiveConnection = Conn
com.ActiveConnection = ConnWscript.echo
Wscript.echo "SMTP Virtual Servers"strQry = "<LDAP://" & strNC & ">;(objectCategory=protocolCfgSMTPServer);name,distinguishedName;subtree"
com.ActiveConnection = conn
com.CommandText = strQrySet RS = com.Execute
While Not RS.EOF
strstmsrv = "LDAP://" & rs.fields("distinguishedName")
set svsSmtpserver = getobject(strstmsrv)
crServerName = mid(svsSmtpserver.distinguishedName,instr(svsSmtpserver.distinguishedName,"CN=Protocols,")+16,instr(svsSmtpserver.distinguishedName,",CN=Servers")-(instr(svsSmtpserver.distinguishedName,"CN=Protocols,")+16))
wscript.echo
wscript.echo "ServerName:" & crServerName
RS.movenext
wendSet RS=Nothing
Set com=Nothing
Set conn=Nothing
Hope this helpsRegards,
Brij Raj Singh
? Brijs Blogging... Looking Beyond the Obvious
这篇关于以编程方式获取域的SMTP服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!