Exchange server 如何从我的应用程序(C)以编程方式获取Exchange服务器

Exchange server 如何从我的应用程序(C)以编程方式获取Exchange服务器,exchange-server,Exchange Server,目前我可以使用C通过WebDAV成功发送电子邮件,但我的应用程序中存在一个不足,即我对outlook的Exchange服务器进行了硬编码,因此它可能只适用于我,如果它被移动到另一台PC并使用另一个outlook帐户,这可能不起作用,因为此outlook帐户的Exchange服务器可能与我的不同,这是因为我们公司为不同的电子邮件帐户分配了不同的Exchange服务器,所以我的问题是如何动态获取当前电子邮件帐户的Exchange服务器。事实上,当我单击菜单项添加新的outlook帐户时,我可以从ou

目前我可以使用C通过WebDAV成功发送电子邮件,但我的应用程序中存在一个不足,即我对outlook的Exchange服务器进行了硬编码,因此它可能只适用于我,如果它被移动到另一台PC并使用另一个outlook帐户,这可能不起作用,因为此outlook帐户的Exchange服务器可能与我的不同,这是因为我们公司为不同的电子邮件帐户分配了不同的Exchange服务器,所以我的问题是如何动态获取当前电子邮件帐户的Exchange服务器。事实上,当我单击菜单项添加新的outlook帐户时,我可以从outlook客户端获取此Exchange服务器,但是是否存在任何API可供我以编程方式(如使用C)获取此Exchange服务器

事实上,我使用以下代码发送电子邮件:

using System;
using System.Net;
using System.IO;   

namespace WebDavNET
{
 /// <summary>
 /// Summary description for Class1.
 /// </summary>
 class Class1
 {
  static void Main(string[] args)
  {
   try 
   {
    // TODO: Replace with the name of the computer that is running Exchange 2000.
    string  strServer = "ExchServe";
    // TODO: Replace with the sender's alias.
    string  strSenderAlias = "sender";  
    // TODO: Replace with the sender's e-mail address.
    string  strFrom = "sender@example.com"; 
    // TODO: Replace with the recipient's e-mail address.
    string  strTo = "recipient@example.com"; 

    string  strSubject = "Send Using HttpWebRequest";
    string  strBody = "Hello World";

    string sUri;
    sUri = "http://" + strServer + "/Exchange/" + strSenderAlias;
    sUri = sUri + "/%23%23DavMailSubmissionURI%23%23/"; 

    System.Uri myUri = new System.Uri(sUri);
    HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);

    string sQuery;
    DateTime mySentTime = new DateTime();
    sQuery = "From: " + strFrom + "\n" + 
     "To: " + strTo + "\n" + 
     "Subject: " + strSubject + "\n" + 
     "Date: " + DateTime.Now.ToString() + "\n" +
     "X-Mailer: My DAV mailer" + "\n" + 
     "MIME-Version: 1.0" + "\n" + 
     "Content-Type: text/plain;" + "\n" + 
     "Charset = \"iso-8859-1\"" + "\n" + 
     "Content-Transfer-Encoding: 7bit" + "\n" + "\n" + 
     strBody;

    // Set the credentials.
    // TODO: Replace with the appropriate user credential.
    NetworkCredential myCred = new NetworkCredential(@"DomainName\User", "Password");
    CredentialCache myCredentialCache = new CredentialCache();
    myCredentialCache.Add(myUri, "Basic", myCred);
    HttpWRequest.Credentials = myCredentialCache;

    // Set the headers.
    HttpWRequest.Headers.Set("Translate", "f");
    HttpWRequest.ContentType =  "message/rfc822";
    HttpWRequest.ContentLength = sQuery.Length;

    //Set the request timeout to 5 minutes.
    HttpWRequest.Timeout = 300000;
    // Set the request method.
    HttpWRequest.Method = "PUT";

    // Store the data in a byte array.
    byte[] ByteQuery = System.Text.Encoding.ASCII.GetBytes(sQuery);
    HttpWRequest.ContentLength = ByteQuery.Length;
    Stream QueryStream = HttpWRequest.GetRequestStream();
    // write the data to be posted to the Request Stream
    QueryStream.Write(ByteQuery,0,ByteQuery.Length);
    QueryStream.Close();

    // Send the request and get the response.
    HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();

    // Get the Status code.
    int iStatCode =  (int)HttpWResponse.StatusCode;
    string sStatus = iStatCode.ToString();
    Console.WriteLine("Status Code: {0}", sStatus);
    // Get the request headers.
    string sReqHeaders = HttpWRequest.Headers.ToString();
    Console.WriteLine(sReqHeaders);

    // Read the response stream.
    Stream strm = HttpWResponse.GetResponseStream();
    StreamReader sr = new StreamReader(strm);
    string sText = sr.ReadToEnd();
    Console.WriteLine("Response: {0}", sText);

    // Close the stream.
    strm.Close();

    // Clean up.
    myCred = null;
    myCredentialCache = null;
    HttpWRequest = null;
    HttpWResponse = null;
    QueryStream = null;
    strm = null;
    sr = null;
   }
   catch (Exception e)
   {
    Console.WriteLine("{0} Exception caught.", e);
   }
  }
 }
}
正如您在代码中看到的,有一个名为strServer的变量,在我的应用程序中,我只是为这个变量硬编码了我的Exchange服务器,所以我的问题是,是否存在任何API可以让我为特定的outlook帐户动态获取Exchange服务器


谢谢

您也可以使用Exchange Web服务。这是

您可以使用XML creator创建链接中操作所需的项目或请求。只需完成链接上给出的操作