2012-12-09 15 views
3

獲取附件我使用AE.NET使用IMAP從Gmail中獲取郵件。我能夠獲取消息,但是當我嘗試迭代消息附件時,沒有消息附件。回到message.Value.Attachments.Count()給了我0.1請參閱下面我的代碼:使用Gmail的AE.NET不工作

using (var imap = new AE.Net.Mail.ImapClient("imap.gmail.com", mailAccount.UserName, mailAccount.Password, AE.Net.Mail.ImapClient.AuthMethods.Login, 993, true)) 
{ 
    //Get all new messages 
    var msgs = imap.SearchMessages(
     SearchCondition.Unseen() 
    ); 
    string ret = ""; 

    foreach (var message in msgs) 
    { 
     foreach (var attachment in message.Value.Attachments) 
     { 
      //Save the attachment 
     } 
    } 
} 

正如我說我已經登錄的附件與郵件的主題計數一起,並相信該郵件是但是他們沒有附件,這是不正確的,因爲我可以在Gmail中看到附件。

任何幫助,這是非常讚賞。

回答

1

你只是做imap.SearchMessages(SearchCondition.Unseen()),但這種方法只加載郵件的標題。您需要通過您在SearchMessages方法得到消息的ID來使用下面的代碼:

List<string> ids = new List<string>(); 
List<AE.Net.Mail.MailMessage> mails = new List<AE.Net.Mail.MailMessage>(); 

using (var imap = new AE.Net.Mail.ImapClient("imap.gmail.com", mailAccount.UserName, mailAccount.Password, AE.Net.Mail.ImapClient.AuthMethods.Login, 993, true)) 
{ 
    var msgs = imap.SearchMessages(SearchCondition.Unseen()); 
    for (int i = 0; i < msgs.Length; i++) { 
     string msgId = msgs[i].Uid; 
     ids.Add(msgId);    
    } 

    foreach (string id in ids) 
    { 
     mails.Add(imap.GetMessage(id, headersonly: false)); 
    } 
} 

然後用:

foreach(var msg in mails) 
{ 
    foreach (var att in msg.Attachments) 
    { 
     string fName; 
     fName = att.Filename; 
    } 
} 
0
using (var imap = new AE.Net.Mail.ImapClient("imap.gmail.com", mailAccount.UserName, mailAccount.Password, AE.Net.Mail.ImapClient.AuthMethods.Login, 993, true)) { 
     var msgs = imap.SearchMessages(SearchCondition.Unseen()); 
     for (int i = 0; i < msgs.Length; i++) { 
      MailMessage msg = msgs[i].Value; 

      foreach (var att in msg.Attachments) { 
       string fName; 
       fName = att.Filename; 
      } 
     } 
    }