Click here to Skip to main content
15,904,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.MailItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inBox = null;
Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
ns = app.GetNamespace("MAPI");
ns.Logon(null, null, false, false);
inBox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

//Microsoft.Office.Interop.Outlook.MAPIFolder inBox = Microsoft.Office.Interop.Outlook.MAPIFolder.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Microsoft.Office.Interop.Outlook.Items inBoxItems = inBox.Items;
Microsoft.Office.Interop.Outlook.MailItem newEmail = null;
inBoxItems = inBoxItems.Restrict("[Unread] = true");

try
{
    int k = 0;
    //******inBoxItems.Sort(, true);****////
    foreach (object collectionItem in inBoxItems)
    {
            newEmail = collectionItem as Microsoft.Office.Interop.Outlook.MailItem;
            if (newEmail != null)
            {
                if (newEmail.Attachments.Count > 0)
                {
                    for (int i = 1; i <= newEmail.Attachments.Count; i++)
                    {
                        if (newEmail.Attachments[i].FileName == "Call Letter.doc" || newEmail.Attachments[i].FileName == "VINEET SEHEJU_DOC.doc")
                        {
                            newEmail.Attachments[i].SaveAsFile(@"C:\1001\" + newEmail.Attachments[i].FileName);
                            //goto xLbl;
                        }
                    }
                }
            }
    }
    xLbl:
    MessageBox.Show("Done");
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    ns = null;
    app = null;
    inBox = null;
    item = null;
}

The above code I wrote is working, but if 2 emails with same attachment are found then it saves the latest one.

[Edit - Spelling/grammar and added code block]
Posted
Updated 27-Mar-12 8:00am
v2
Comments
wizardzz 27-Mar-12 13:41pm    
Your question is vague and doesn't match the title of post.
Vineet_2109 27-Mar-12 14:00pm    
if you understood the question, then please give some suggestion.

If you must store the attachmenst when you read them, then it's pretty simple - you have to do what Windows Explorer does for you:

Check if the file exists.
If it doesn't, write it.
If it does, read all the files which match yourfilename + " (" + number + ")".
Find the highest number
Increment it by one, and try writing your file as that.

You will end up with:
MyFile.txt
MyFile (1).txt
MyFile (2).txt
...
If you don't do this, then your code will just overwrite each file as it comes in.

Other alternatives include not to keep the attachments in files, but to keep them in a database instead, or to keep each message in a separate folder, together with it's attachments.

Personally, I would keep emails and attachment details in a database, and store the actual attachment files under a temporary name in a folder specifically for that.
 
Share this answer
 
If you only want to save the latest one:

Would add each email with an attachment to a List and then use Linq to do an OrderBy.

var orderedList = list.OrderBy (i => i.Date, SortDirection.Descending)

As you go through the emails add saved files to a list and search list before saving (you could also check to see if file exits, but then what do you do about existing files?)
 
Share this answer
 
v2

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