是用C#开发的
邮件发送功能总是出现如下错误“The request failed. 远程
服务器返回错误: (401) 未经授权”
代码如下:
- public class ExchangeEmail
- {
- public ExchangeService service;
- public ExchangeEmail()
- {
- service = new ExchangeService(ExchangeVersion.Exchange2010);
- service.Credentials = new WebCredentials("mailAddress", "PWD", "Domain");
- service.Url = new Uri("https://domain/ews/exchange.asmx");
- service.TraceEnabled = true;
- service.TraceFlags = TraceFlags.All;
-
- }
- Logger log = new Logger();
- public bool SendEmail(string SendTo, string EmailSubject, string EmailBody, string CCTo = "", string AttachmentName = "", byte[] AttachStream = null)
- {
- EmailMessage email = new EmailMessage(service);
- try
- {
- if (SendTo.IndexOf(";") > 0)
- {
- string[] EmailArray = SendTo.Split(';');
- foreach (string item in EmailArray)
- {
- email.ToRecipients.Add(item.TrimEnd().TrimStart());
- }
- }
- else
- {
- email.ToRecipients.Add(SendTo.TrimEnd().TrimStart());
- }
- if (CCTo != "")
- {
- if (CCTo.IndexOf(";") > 0)
- {
- string[] EmailArray = CCTo.Split(';');
- foreach (string item in EmailArray)
- {
- email.ToRecipients.Add(item.TrimEnd().TrimStart());
- }
- }
- else
- {
- email.CcRecipients.Add(CCTo.TrimEnd().TrimStart());
- }
- log.Log("Add CC Recipient " + CCTo + " Successfully");
- }
- if (AttachmentName != "" && AttachStream != null)
- {
- email.Attachments.AddFileAttachment(AttachmentName, AttachStream);
- }
- email.Subject = EmailSubject;
- email.Body = new MessageBody(EmailBody);
- email.Body.BodyType = BodyType.HTML;
- email.Send();
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- finally
- {
- email.ToRecipients.Clear();
- email.CcRecipients.Clear();
- email.BccRecipients.Clear();
- }
- }
- }
当执行完方法之后 再进入服务器上查看对应的账号状态会发现状态如下图:
正常情况下 状态如下:
谁知道这是什么原因造成的??