我的办公室里有一个交换在线服务365,
我想使用不同的方法将电子邮件发送到其他人邮箱
然后我找到了Exchange Web Services,所以我试着做一个基本的例子:
class Program
{
static void Main(string[] args)
{
//note that there no option for exchange server 2016 (my exchange online use exchange server 2016), so i use the default option
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Credentials = new WebCredentials("user@domain.com", "myPassword");
service.UseDefaultCredentials = false;
//for log purpose
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.AutodiscoverUrl("user@domain.com", RedirectionUrlValidationCallback);
EmailMessage email = new EmailMessage(service);
email.ToRecipients.Add("user2@domain.com");
email.Subject = "HelloWorld";
email.Body = new MessageBody("This is the first email I've sent by using the EWS Managed API.");
email.Send();
}
private static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
// The default for the validation callback is to reject the URL.
bool result = false;
Uri redirectionUri = new Uri(redirectionUrl);
// Validate the contents of the redirection URL. In this simple validation
// callback, the redirection URL is considered valid if it is using HTTPS
// to encrypt the authentication credentials.
if (redirectionUri.Scheme == "https")
{
result = true;
}
return result;
}
}
并且AutodiscoverUrl中存在异常,如下所示:
Microsoft.Exchange.WebServices.Data.ServiceXmlDeserializationException: 'The
expected XML node type was XmlDeclaration, but the actual type is Element.'
在我搜索了一段时间之后,它说交换机无法找到我的域名,我的交换机在线设置很好,我可以发送电子邮件,通过Outlook web访问我的邮箱中的其他人添加预约
我已经将我的名字服务器更改为
ns1.bdm.microsoftonline.com
ns2.bdm.microsoftonline.com
但仍无法解决我的问题..
是否有一些我失踪的设置? THX ..
答案 0 :(得分:1)
而不是使用
service.AutodiscoverUrl("user@domain.com", RedirectionUrlValidationCallback);
试
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
我建议你设置
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
到Office365的最高枚举,而不是最低,除非您尝试支持Exchange 2007。