Skip to main content
 首页 » 编程设计

c#之如何使用 Microsoft.Exchange.WebServices

2024年01月12日21jyk

我尝试使用:Microsoft.Exchange.WebServices.dll 来使用 outlook。但是连接返回错误

错误返回行:service.AutodiscoverUrl("myusernamek@xxxx.com");

找不到自动发现服务。 我的代码:

 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net.Mail; 
using System.Net; 
using Microsoft.Exchange.WebServices.Data; 
using Microsoft.Exchange.WebServices.Autodiscover; 
using System.Net.Security; 
using System.Security.Cryptography.X509Certificates; 
 
namespace test 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            try 
            { 
                // Connect to Exchange Web Services as user1 at contoso.com. 
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); 
                service.Credentials = new WebCredentials("myusernamek@xxxx.com", "mypassword", "xxxx.com"); 
                service.TraceEnabled = true; 
                service.AutodiscoverUrl("myusernamek@xxxx.com"); 
 
                // Create the e-mail message, set its properties, and send it to user2@contoso.com, saving a copy to the Sent Items folder.  
                EmailMessage message = new EmailMessage(service); 
                message.Subject = "Interesting"; 
                message.Body = "The proposition has been considered."; 
                message.ToRecipients.Add("recipientname@xxxx.aero"); 
                message.SendAndSaveCopy(); 
 
                // Write confirmation message to console window. 
                Console.WriteLine("Message sent!"); 
                Console.ReadLine(); 
            } 
            catch (Exception ex) 
            { 
                Console.WriteLine("Error: " + ex.Message); 
                Console.ReadLine(); 
            } 
 
        } 
    } 
}

请您参考如下方法:

我知道这是一个老问题,但最近遇到了这个和类似的错误(包括 ISA 服务器)。它已修复:

service.EnableScpLookup = false; 

这在使用显式 URL 时不是必需的,但在使用自动发现时是必需的