Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am using AE.NET to get mail from Gmail using IMAP. I am able to get the messages, however when I try iterate over the message attachments there are none. Returning message.Value.Attachments.Count() gives me 0. Please see my code below:

C#
using (ImapClient ic = new ImapClient("imap.gmail.com", "test*****@gmail.com", "******", ImapClient.AuthMethods.Login, 993, true))
               {
                   // Open a mailbox, case-insensitive
                   ic.SelectMailbox("INBOX");

                   // Get messages based on the flag "Undeleted()".
                   Lazy<MailMessage>[] messages = ic.SearchMessages(SearchCondition.Undeleted(), false);

                   int count = messages.Count();

                   Response.Write(count.ToString());

                   // Process each message
                   foreach (Lazy<MailMessage> message in messages)
                   {
                       MailMessage m = message.Value;
                       Response.Write("<br/>");
                       Response.Write(m.From.Address);
                       Response.Write("---");
                       Response.Write(m.Subject);
                       Response.Write("***");
                       Response.Write(m.MessageID);
                       Response.Write("%%%");
                       Response.Write(m.From.DisplayName);

                       if (m.Attachments.Count > 0)
                       {
                           // ExtractString(m.Subject, "(", ")");

                           foreach (Attachment attachment in m.Attachments)
                           {
                               string fileName = m.Subject + "_" + attachment.Filename;
                               attachment.Save(@"D:\DemoMail\" + fileName + Path.GetExtension(attachment.Filename));
                           }
                       }


                   }

               }
Posted

This code will be help full i had developed and tested vb version only just converted this code to c#, so i will post the vb code for reference. it includes snippets for saving attachments to selected folder and also for save cc and bcc in CSV format to a string variable:

VB Code

VB
Public Sub fetchMails()
          Dim imapClient As New Imap4Client()
          ' login to the mail
          Dim mailNumber As Integer = 10 'reading 10th mail
          Dim notYetProcessedBox As Mailbox = imapClient.SelectMailbox("Inbox")
          Dim msg As Message = notYetProcessedBox.Fetch.MessageObject(mailNumber)
          ' To read cc and bcc
          Dim mailCC As String = ""
          Dim mailBcc As String = ""
          Dim cc() As ActiveUp.Net.Mail.Address = msg.Cc.ToArray()
          Dim bcc() As ActiveUp.Net.Mail.Address = msg.Bcc.ToArray()
          For Each address As ActiveUp.Net.Mail.Address In cc
              mailCC &= address.ToString
          Next
          For Each address As ActiveUp.Net.Mail.Address In bcc
              mailBcc &= address.ToString
          Next
          ' For saving attachment
          If msg.Attachments.Count <> 0 Then
              If System.IO.Directory.Exists("../email_attachment/") = False Then
                  System.IO.Directory.CreateDirectory("../email_attachment/")
                  msg.Attachments.StoreToFolder("../email_attachment/")
              End If
          End If
      End Sub


C# Code

C#
public void fetchMails()
{
	Imap4Client imapClient = new Imap4Client();
	// login to the mail
	int mailNumber = 10;
	//reading 10th mail
	Mailbox notYetProcessedBox = imapClient.SelectMailbox("Inbox");
	Message msg = notYetProcessedBox.Fetch.MessageObject(mailNumber);
	// To read cc and bcc
	string mailCC = "";
	string mailBcc = "";
	ActiveUp.Net.Mail.Address[] cc = msg.Cc.ToArray();
	ActiveUp.Net.Mail.Address[] bcc = msg.Bcc.ToArray();
	foreach (ActiveUp.Net.Mail.Address address in cc) {
		mailCC += address.ToString;
	}
	foreach (ActiveUp.Net.Mail.Address address in bcc) {
		mailBcc += address.ToString;
	}
	// For saving attachment
	if (msg.Attachments.Count != 0) {
		if (System.IO.Directory.Exists("../email_attachment/") == false) {
			System.IO.Directory.CreateDirectory("../email_attachment/");
			msg.Attachments.StoreToFolder("../email_attachment/");
		}
	}
}
 
Share this answer
 
Comments
DevilsCod 3-Feb-15 1:59am    
Thanks for a good solution.
Sujith Karivelil 3-Feb-15 4:04am    
Make use of it. once again am not sure about C# code
This article should get you going

IMAP Client library using C#[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900