2012-02-16 222 views
2

正在使用EWS訪問電子郵件帳戶的收件箱。不過,我不斷收到此錯誤:請求失敗。遠程服務器返回錯誤:(401)未授權

The request failed. The remote server returned an error: (401) Unauthorized.

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); 
service.Credentials = new WebCredentials("[email protected]","password","domain"); 
service.Url = new Uri("https://webpage.net/EWS/exchange.asmx"); 
//SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, true)); 
ItemView view = new ItemView(10); 
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox,view);//i get the error in this line of code 

foreach (Item item in findResults.Items) 
{ 
    Console.WriteLine(item.Subject); 
} 
+0

您需要提供更多的上下文。事實上,服務器說你是未經授權的事實就意味着這一點。憑證是否正確? – Jacob 2012-02-16 16:44:10

+0

他們必須是,我訪問Outlook收件箱與我放在這些領域相同的憑據,這就是那個我flippin出的部分 – rdk1992 2012-02-16 16:48:46

+0

問題可能在網址,然後。服務端點可能配置錯誤或URL錯誤。 – Jacob 2012-02-16 17:17:48

回答

3

的@ company.net是打破它。

-1

我會讓你有一段我正在工作的代碼。我還遇到了401 Auth問題,那是在回來時,我不記得我剛剛修改了哪些內容以使其正常工作,但現在它已完成。當然你不需要整個代碼,但它給你一個想法。

public class Outlook 
{ 
    /// <summary> 
    /// Returns the selected node Index which equals selected mail index. 
    /// </summary> 
    public static Int32 selectedMailindex 
    { 
     get 
     { 
      return (Int32)Form1.GlobalAccess.Invoke((Func<int>)delegate 
      { 
       return Form1.GlobalAccess.mailTree.SelectedNode.Index; 
      }); 
     } 
    } 

    static PullSubscription subscriptionInbox; 
    public static ExchangeService runningService; 

    /// <summary> 
    /// Used to save EmailMessage retrieved from user inbox. 
    /// </summary> 
    public static List<EmailMessage> mailMessages = new List<EmailMessage>(); 

    /// <summary> 
    /// We start the procedure to grab Emails. 
    /// </summary> 
    public static void StartupLoadMails() 
    { 
     ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); 
     service.TraceEnabled = true; 
     service.Credentials = new WebCredentials("username", "password"); //Modify this 
     service.Url = new Uri("Exchange.asmx URL"); //Modify this 
     GetBinding(service); 
     runningService = service; 
     GetMailItems(runningService); 
    } 

    static ExchangeService GetBinding(ExchangeService service) 
    { 
     //Subscribe to Inbox newmail/modified events. 
     subscriptionInbox = service.SubscribeToPullNotifications(
     new FolderId[] { WellKnownFolderName.Inbox }, //Inbox 
     5, //TimeOut 
     null, 
     EventType.NewMail, EventType.Modified); //NewMail or Modified 
     // Display the service URL. 
     return service; 
    } 

    /*static bool RedirectionUrlValidationCallback(String redirectionUrl) 
    { 
     return true; 
    }*/ 

    public static void GetMailItems(ExchangeService service) 
    { 
     Form1.GlobalAccess.Invoke(new Action(() => 
     { 
      Form1.GlobalAccess.mailTree.Nodes.Clear();     
     })); 
     mailMessages.Clear(); 
     //SearchFilter to get unreaded messages only. 
     SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false)); 
     //Create new Item view with the last 9 unreaded items. 
     ItemView view = new ItemView(9); 
     //Execute the query 
     FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, sf, view); 
     //We use Parallel for faster initial app loading, reducing ~5/10 secs. 
     Parallel.ForEach(findResults, item => 
      { 
       try 
       { 
        EmailMessage message = EmailMessage.Bind(service, item.Id); 
        mailMessages.Add(message); 
       } 
       catch 
       { 
        MessageBox.Show("ERROR"); 
       } 
      }); 

     //Since we used parallel we need to sort the emails in our EmailMessage LIST by date, so they show cronologicaly over the treeview. 
     mailMessages.Sort(delegate(EmailMessage m1, EmailMessage m2) { return m2.DateTimeReceived.Date.CompareTo(m1.DateTimeReceived.Date); }); 
     foreach (EmailMessage m in mailMessages) 
     { 
      Form1.GlobalAccess.Invoke(new Action(() => 
      { 
       Form1.GlobalAccess.mailTree.Nodes.Add(m.Subject + " : " + m.Sender.Name); 
      })); 
     } 
    } 

    /// <summary> 
    /// Calls threaded MarkReaded function to mark an Email as readed localy and remotly. 
    /// </summary> 
    public static void MarkReaded() 
    { 
     Thread thread = new Thread(CallThreadedReadFunction); 
     thread.Priority = ThreadPriority.AboveNormal; 
     thread.Start(); 
    } 

    static void CallThreadedReadFunction() 
    { 
     ReadFunction(Outlook.mailMessages[selectedMailindex],selectedMailindex,runningService); 
    } 

    /// <summary> 
    /// Mark as readed 
    /// </summary> 
    /// <param name="message">E-mail Message</param> 
    /// <param name="index">Index Message Possition</param> 
    /// <param name="service">EWS Service</param> 
    static void ReadFunction(EmailMessage message, Int32 index, ExchangeService service) 
    { 
     Form1.GlobalAccess.Invoke(new Action(() => 
     { 
      Form1.GlobalAccess.mailTree.Nodes[index].SelectedImageIndex = 3; 
      Form1.GlobalAccess.mailTree.Nodes[index].ImageIndex = 3; 
     })); 
     EmailMessage msg = EmailMessage.Bind(service, message.Id); 
     msg.IsRead = true; 
     msg.Update(ConflictResolutionMode.AutoResolve); 
    } 

    /// <summary> 
    /// Calls threaded Delete function to delete an Email localy and remotly. 
    /// </summary> 
    public static void Delete() 
    { 
     var deleteWorker = new BackgroundWorker(); 

     deleteWorker.DoWork += (sender, args) => 
     { 
      DeleteFunction(Outlook.mailMessages[selectedMailindex], selectedMailindex, runningService); 
     }; 

     deleteWorker.RunWorkerCompleted += (sender, args) => 
     { 
      if (args.Error != null) 
       MessageBox.Show(args.Error.ToString()); 

      Form1.GlobalAccess.Invoke(new Action(() => 
      { 
       Form1.GlobalAccess.mailrefreshIcon.Visible = true; 
      })); 
     }; 

     deleteWorker.RunWorkerAsync(); 
    } 

    /// <summary> 
    /// Delete Emails choosen by the user. 
    /// </summary> 
    /// <param name="message">E-mail Message</param> 
    /// <param name="index">Index Message Possition</param> 
    /// <param name="service">EWS Service</param> 
    public static void DeleteFunction(EmailMessage message, Int32 index, ExchangeService service) 
    { 
     try 
     { 
      Form1.GlobalAccess.Invoke(new Action(() => 
      { 
       Form1.GlobalAccess.mailTree.Nodes.RemoveAt(index); 
       mailMessages.RemoveAt(index); 
       Form1.GlobalAccess.mailTree.SelectedNode = null; 
       Form1.GlobalAccess.mailBrowser.DocumentText = null; 
      })); 

      EmailMessage msg = EmailMessage.Bind(service, message.Id); 
      msg.Delete(DeleteMode.MoveToDeletedItems); 
      msg.Update(ConflictResolutionMode.AlwaysOverwrite); 
     } 
     catch 
     { 
      //En ocaciones, si se borran muchos emails consecutivamente y se da refresh al inbox rapidamente, causa un crash porque el servidor encuentra el email que ordenamos borrar y mientras esta 
      //Iterando es borrado, causando un crash. Lo mejor es unicamente ignorar la excepción, el programa seguira trabajando bien. 
     } 
    } 

    /// <summary> 
    /// Listens for new mails in the user inbox folder, if found, notifies the user and update mail list. 
    /// </summary> 
    /// <param name="service">EWS Service</param> 
    public static void GetLatests(ExchangeService service) 
    { 
     GetEventsResults eventsInbox = subscriptionInbox.GetEvents(); 
     EmailMessage message; 
     // Loop through all item-related events. 
     foreach (ItemEvent itemEvent in eventsInbox.ItemEvents) 
     { 
      switch (itemEvent.EventType) 
      { 
       case EventType.NewMail: 
        try 
        { 
         Item item = Item.Bind(service, itemEvent.ItemId); 
         if (item.ItemClass.ToLower() == "IPM.Note".ToLower()) 
         { 
          message = EmailMessage.Bind(service, itemEvent.ItemId); 
           Form1.GlobalAccess.Invoke(new Action(() => 
           { 
           if (Form1.GlobalAccess.mailTree.Nodes.Count == 8) 
           { 
            try 
            { 
             Form1.GlobalAccess.mailTree.Nodes.RemoveAt(8); 
             mailMessages.RemoveAt(8); 
            } 
            catch 
            { 
            } 
           } 
           Form1.GlobalAccess.mailTree.Nodes.Insert(0, message.Subject); 
           mailMessages.Insert(0, message); 
           })); 
          }    
        } 
        catch (Exception ex) 
        { 
         MessageBox.Show(ex.Message); 
        } 
        break; 
      } 
     } 
    } 

    /// <summary> 
    /// Reply to a single user. This method gets called from ReplyMail winform. 
    /// </summary> 
    /// <param name="message">Email Message OBJECT</param> 
    /// <param name="index">Email position in the tree</param> 
    /// <param name="service">EWS Current Service</param> 
    /// <param name="bodyText">Message Text</param> 
    /// <param name="subject">Message Original Subject</param> 
    public static void Reply(EmailMessage message, Int32 index, ExchangeService service, String bodyText, String subject) 
    { 
     var replyWorker = new BackgroundWorker(); 

     replyWorker.DoWork += (sender, args) => 
     { 
      bool replyToAll = false; 
      ResponseMessage responseMessage = message.CreateReply(replyToAll); 
      responseMessage.BodyPrefix = bodyText; 
      responseMessage.Subject = subject; 
      responseMessage.SendAndSaveCopy(); 
     }; 

     replyWorker.RunWorkerCompleted += (sender, args) => 
     { 
      if (args.Error != null) 
       MessageBox.Show(args.Error.ToString()); 

      ReplyMail.GlobalAccess.Invoke(new Action(() => 
      { 
       ReplyMail.GlobalAccess.mailSentPic.Visible = true; 
       ReplyMail.GlobalAccess.MailSentTxt.Visible = true; 
       ReplyMail.GlobalAccess.button2.Visible = true; 
      })); 
     }; 
     replyWorker.RunWorkerAsync(); 

    } 

    /// <summary> 
    /// Forward an E-mail, lets you introduce recipients. 
    /// </summary> 
    /// <param name="message">EMAILMESSAGE</param> 
    /// <param name="index">Mail position on the treeview</param> 
    /// <param name="service">EWS Service</param> 
    /// <param name="addresses">List with the mail adresses that the user is forwarding to</param> 
    public static void Forward(EmailMessage message, Int32 index, ExchangeService service, List<EmailAddress> addresses) 
    { 
     var forwardWorker = new BackgroundWorker(); 

     forwardWorker.DoWork += (sender, args) => 
     { 
      message.Forward(message.Body.Text, addresses); 
     }; 

     forwardWorker.RunWorkerCompleted += (sender, args) => 
     { 
      if (args.Error != null) 
       MessageBox.Show(args.Error.ToString()); 

      ReplyMail.GlobalAccess.Invoke(new Action(() => 
      { 
       ReplyMail.GlobalAccess.mailSentPic.Visible = true; 
       ReplyMail.GlobalAccess.MailSentTxt.Visible = true; 
       ReplyMail.GlobalAccess.button2.Visible = true; 
      })); 
     }; 
     forwardWorker.RunWorkerAsync();    
    } 

    /// <summary> 
    /// Send a single E-Mail 
    /// </summary> 
    /// <param name="service">EWS Service</param> 
    /// <param name="subject">EMAILMESSAGE subject</param> 
    /// <param name="bodyText">EMAILMESSAGE body.text</param> 
    /// <param name="destination">EMAILADDRESS from receiver</param> 
    public static void Send(ExchangeService service, String subject, String bodyText, String destination) 
    { 
     var sendWorker = new BackgroundWorker(); 

     sendWorker.DoWork += (sender, args) => 
     { 
      EmailMessage message = new EmailMessage(service); 
      message.Subject = subject; 
      message.Body = bodyText; 
      message.ToRecipients.Add(destination); 
      message.SendAndSaveCopy(); 
     }; 

     sendWorker.RunWorkerCompleted += (sender, args) => 
     { 
      if (args.Error != null) 
       MessageBox.Show(args.Error.ToString()); 

      ReplyMail.GlobalAccess.Invoke(new Action(() => 
      { 
       ReplyMail.GlobalAccess.mailSentPic.Visible = true; 
       ReplyMail.GlobalAccess.MailSentTxt.Visible = true; 
       ReplyMail.GlobalAccess.button2.Visible = true; 
      })); 
     }; 
     sendWorker.RunWorkerAsync();   
    } 
} 

}

+4

這是很好,你已經分享了你的代碼,但你提供了一個相當大的乾草堆,和一個暗示那裏可能有也可能沒有正確答案(針)。如果你能確定你的解決方案和OP的差異,然後建議這些改變,那會更好。但努力幫助和格式良好的答案的榮譽。 – Scott 2012-09-19 07:45:48

0

如果您的密碼已過期,你會得到這個錯誤。

我必須直接使用用戶帳戶登錄並設置新密碼才能使其重新工作。

相關問題