2016-12-27 65 views
0

我在這裏只是嘗試從服務器使用IMAP與ComponentPro .net組件(Asynchronous Task-Based Approach)下載「INBOX」中的電子郵件,但每次嘗試異步方法時都會引發錯誤。異步操作此時無法啓動

我已經使用IMAP以同步方式下載了Inbox電子郵件,但這需要將近3-4分鐘才能下載至少80多封電子郵件,所以我想嘗試異步方法對此有任何建議,因爲我嘗試的是第一種異步方法時間。

錯誤:

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle.

控制器代碼:

 public ActionResult ImportEmailDemo() 
    { 
     var sImportedEmails = ARepository.ImportEmailForDemo(); 
     return null; 
    } 

庫代碼:

public async Task<string> ImportEmailForDemo() 
    { 
     //Async 
     // Create a new instance of the Imap class. 
     Imap client1 = new Imap(); 

     // Connect to the server. 
     client1.Connect("Server Address"); 

     // Or you can specify the IMAP port with 
     // client.Connect("myserver", 143); 

     // Login to the server. 
     client1.Authenticate("EmailID", "Password"); 


     // Select 'INBOX' mailbox. 
     client1.Select("INBOX"); 

     // Download a mail message with sequence number 1. 

     ComponentPro.Net.Mail.MailMessage msg = await client1.DownloadMailMessageAsync(1); 

     // ... 

     Console.WriteLine("Message downloaded successfully."); 
     Console.WriteLine("Message ID: {0}, Subject: {1}", msg.MessageIdentifier, msg.Subject); 

     // Disconnect. 
     client1.Disconnect(); 
     return null; 
    } 

我用不同的方式來解決的錯誤期待在互聯網,但沒有什麼工作,我試過, 誰能幫我嗎。 乾杯!

+3

問題在於你的調用方法。請將其添加到您的問題。 – Sefe

+0

好吧,我已經添加了我調用它的控制器代碼。 – pavan

回答

2

I already got Inbox emails downloaded using IMAP with Synchronous approach but this takes nearly 3-4 minutes to download atleast 80+ emails, So I want to try asynchronous approach

異步將不會更快。

An asynchronous operation cannot be started at this time.

當您的代碼在同步處理程序中啓動異步操作時會發生此錯誤。您應該使用而不是使用WaitResult;相反,你需要await返回的任務,這使得該方法async等,直到您的處理程序更改爲異步。

但如上所述,使用async無論如何不會讓它變得更快。

+0

好的感謝您的信息,那麼你知道任何最好的.Net組件(DLL),它可以更快地下載電子郵件,那麼我在這裏使用的那個? – pavan

+0

@pavan:由於下載是基於I/O的操作,您的速度可能受到電子郵件服務器的限制。 –

+0

謝謝你的回覆,我改變了主意,使用同步的。 – pavan