2008-11-19 41 views
9

Thats những gì tôi đang sử dụng để đọc e-mail bằng C#:Đọc e-mail mà không cần Outlook ứng dụng mở

outLookApp.NewMailEx += new ApplicationEvents_11_NewMailExEventHandler(outLookApp_NewMailEx); 
      Outlook.NameSpace olNameSpace = outLookApp.GetNamespace("mapi"); 

olNameSpace.Logon("xxxx", "xxxxx", false, true); 
Outlook.MAPIFolder oInbox = olNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); 
Outlook.Items oItems = oInbox.Items; 
MessageBox.Show("Total : " + oItems.Count); //Total Itemin inbox 
oItems = oItems.Restrict("[Unread] = true"); 
MessageBox.Show("Total Unread : " + oItems.Count); //Unread Items 
Outlook.MailItem oMsg; 


Outlook.Attachment mailAttachement; 
for (int i = 0; i < oItems.Count; i++) 
{ 
    oMsg = (Outlook.MailItem)oItems.GetFirst(); 

    MessageBox.Show(i.ToString()); 

    MessageBox.Show(oMsg.SenderName); 
    MessageBox.Show(oMsg.Subject); 
    MessageBox.Show(oMsg.ReceivedTime.ToString()); 
    MessageBox.Show(oMsg.Body); 

Vấn đề mà tôi phải đối mặt là ứng dụng này chỉ hoạt động nếu Outlook được mở trên máy tính này . Nếu Outlook bị đóng, nó sẽ ném một ngoại lệ:

Máy chủ không khả dụng. Liên hệ với quản trị viên của bạn nếu tình trạng này vẫn còn.

Có anyway tôi có thể đọc e-mail có Outlook mở không?

Trả lời

2

Bạn có thể sẽ gặp phải this khi Outlook đóng.

Cũng theo sau this tutorial sẽ đảm bảo bạn đang thực hiện tất cả các bước và một phần phù hợp.

Chúc bạn may mắn!

0

Bạn có chắc chắn muốn sử dụng Outlook làm proxy không?

peopleseems để đối phó mức thấp với một nhiệm vụ như vậy trong C# (đáng ngạc nhiên không có bất kỳ built-in thành phần trong khuôn khổ ...)

Về phản ứng của Mat, Redemption thực sự là một sản phẩm tốt (sử dụng nó để phân tích thư khi đến trong triển vọng), nhưng tôi nghi ngờ nó có thể làm việc mà không có triển vọng chạy.

0

Cá nhân tôi không sử dụng Outlook làm proxy. Nếu bạn đang cố gắng giám sát một cửa hàng Exchange, thì tôi sẽ sử dụng WebDav. Máy chủ Exchange của bạn phải hỗ trợ nó - nhưng nếu có, đó là một API XML đơn giản. Vâng, bit API rất đơn giản, nhưng XML khá phức tạp. Nhưng một khi bạn đã gói gọn nó trong một chút mã, nó là một doddle để sử dụng.

+0

Tôi nghĩ nó không liên quan đến câu hỏi. – Samuel

+0

Samuel, câu trả lời của tôi là một sự giải thích về "Không, bạn không thể". Tôi nghĩ nó sẽ hữu ích hơn! –

1

Đây là loại một câu hỏi cũ, nhưng tôi sẽ trả lời nó kể từ khi tôi phải vật lộn với vấn đề tương tự trong một thời gian dài và các câu trả lời trước trên trang này đã làm không thực sự giúp tôi.

Tôi phải viết một chương trình và sử dụng triển vọng để gửi email trên các máy khác nhau với các cấp UAC khác nhau và đây là những gì tôi đã đưa ra sau một thời gian dài.

using Outlook = Microsoft.Office.Interop.Outlook; 

// Create the Outlook application. 
Outlook.Application oApp = null; 

// Check whether there is an Outlook process running. 
int outlookRunning = Process.GetProcessesByName("OUTLOOK").Length; 
if (outlookRunning > 0) 
{ 
    // If so, use the GetActiveObject method to obtain the process and cast it to an Application object. 
    try 
    { 
     oApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application; 
    } 
    catch (Exception) 
    { 
     oApp = Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application")) as Outlook.Application; 
    } 
    finally 
    { 
     // At this point we must kill Outlook (since outlook was started by user on a higher prio level than this current application) 
     // kill Outlook (otherwise it will only work if UAC is disabled) 
     // this is really a kind of last resort 
     Process[] workers = Process.GetProcessesByName("OUTLOOk"); 
     foreach (Process worker in workers) 
     { 
      worker.Kill(); 
      worker.WaitForExit(); 
      worker.Dispose(); 
     } 
    } 
} 
else 
{ 
    // If not, create a new instance of Outlook and log on to the default profile. 
    oApp = new Outlook.Application(); 
    Outlook.NameSpace nameSpace = oApp.GetNamespace("MAPI"); 
    try 
    { 
     // use default profile and DO NOT pop up a window 
     // on some pc bill gates fails to login without the popup, then we must pop up and lets use choose profile and allow access 
     nameSpace.Logon("", "", false, Missing.Value); 
    } 
    catch (Exception) 
    { 
     // use default profile and DO pop up a window 
     nameSpace.Logon("", "", true, true); 
    } 
    nameSpace = null; 
} 

// Done, now you can do what ever you want with the oApp, like creating a message and send it 
// Create a new mail item. 
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 
Các vấn đề liên quan