是用C#开发的邮件发送功能总是出现如下错误“The request failed. 远程服务器返回错误: (401) 未经授权”
代码如下:
1 public class ExchangeEmail 2 { 3 public ExchangeService service; 4 public ExchangeEmail() 5 { 6 service = new ExchangeService(ExchangeVersion.Exchange2010); 7 service.Credentials = new WebCredentials("mailAddress", "PWD", "Domain"); 8 service.Url = new Uri("https://domain/ews/exchange.asmx"); 9 service.TraceEnabled = true; 10 service.TraceFlags = TraceFlags.All; 11 12 } 13 Logger log = new Logger(); 14 public bool SendEmail(string SendTo, string EmailSubject, string EmailBody, string CCTo = "", string AttachmentName = "", byte[] AttachStream = null) 15 { 16 EmailMessage email = new EmailMessage(service); 17 try 18 { 19 if (SendTo.IndexOf(";") > 0) 20 { 21 string[] EmailArray = SendTo.Split(';'); 22 foreach (string item in EmailArray) 23 { 24 email.ToRecipients.Add(item.TrimEnd().TrimStart()); 25 } 26 } 27 else 28 { 29 email.ToRecipients.Add(SendTo.TrimEnd().TrimStart()); 30 } 31 if (CCTo != "") 32 { 33 if (CCTo.IndexOf(";") > 0) 34 { 35 string[] EmailArray = CCTo.Split(';'); 36 foreach (string item in EmailArray) 37 { 38 email.ToRecipients.Add(item.TrimEnd().TrimStart()); 39 } 40 } 41 else 42 { 43 email.CcRecipients.Add(CCTo.TrimEnd().TrimStart()); 44 } 45 log.Log("Add CC Recipient " + CCTo + " Successfully"); 46 } 47 if (AttachmentName != "" && AttachStream != null) 48 { 49 email.Attachments.AddFileAttachment(AttachmentName, AttachStream); 50 } 51 email.Subject = EmailSubject; 52 email.Body = new MessageBody(EmailBody); 53 email.Body.BodyType = BodyType.HTML; 54 email.Send(); 55 return true; 56 } 57 catch (Exception) 58 { 59 return false; 60 } 61 finally 62 { 63 email.ToRecipients.Clear(); 64 email.CcRecipients.Clear(); 65 email.BccRecipients.Clear(); 66 } 67 } 68 }
当执行完方法之后 再进入服务器上查看对应的账号状态会发现状态如下图:
正常情况下 状态如下:
谁知道这是什么原因造成的??请大神给予指导,谢谢。
很明显,没有授权