Click here to Skip to main content
11,711,887 members (80,871 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
How to get attachments name from outlook? using C#. I have got the body of the email, sender email address, How to get the attachment names and how to get those attachments?

Thanks.. i got the attachment names. How to get those attachements. Like a text document, pdf file, Image. etc.. Please guide me!!!
Posted 4-Jul-12 0:04am
Edited 4-Jul-12 1:29am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

C# Code snippet to send an Email with attachment from Outlook, Yahoo, HotMail, AOL and Gmail[^]


<pre>using System.Text;   
// ...   
private void GetAttachmentsInfo(Outlook.MailItem email)   
{   
    StringBuilder attachmentInfo = new StringBuilder();   
    Outlook.Attachments mailAttachments = email.Attachments;   
    if (mailAttachments != null)   
    {   
       for (int i = 1; i < = mailAttachments.Count; i++)   
       {   
          Outlook.Attachment currentAttachment = mailAttachments.Item(i);   
          if (currentAttachment != null)   
          {   
              attachmentInfo.AppendFormat(   
                 "#{0}\n\rFile name: {1}\n\rDisplay Name: {2}\n\rType: {3}\n\n\r",   
                 i,  currentAttachment.FileName, currentAttachment.DisplayName,   
                 currentAttachment.Type);   
              Marshal.ReleaseComObject(currentAttachment);   
          }   
       }   
       if (attachmentInfo.Length > 0)   
          System.Windows.Forms.MessageBox.Show(   
             attachmentInfo.ToString(), "E-mail attachments");   
       Marshal.ReleaseComObject(mailAttachments);   
    }   
} 

from both you can drive out what you need....
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

private void GetAttachments(Outlook.MailItem mailItem)
 {
     Outlook.Attachments attachments = mailItem.Attachments;
     if (attachments != null && attachments.Count > 0)
     {
         for (int i = 1; i <= attachments.Count; i++)
         {
             Outlook.Attachment attachment = attachments[i];
             if (attachment.Type == Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue)
             {
                 // here you can get file name as
                 //string filename = attachment.FileName;
                 // if you want to save path in d:\Attachments\ then try as
                 string filename = Path.Combine(@"d:\Attachments\", attachment.FileName);
                 attachment.SaveAsFile(filename);
 
             }
         }
     }
 }
  Permalink  
v2
Comments
Gokulnath007 at 4-Jul-12 7:29am
   
Thanks.. i got the attachment names. How to get those attachements. Like a text document, pdf file, Image. etc.. Please guide me!!!
Damith Weerasinghe at 4-Jul-12 8:38am
   
Check updated answer
Hedi Sangoku at 15-Jan-14 11:21am
   
What do you mean by "get those attachments?" anyway, you can save attachment by calling Attachment.SaveFileAs("Directory\\filename")

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

  Print Answers RSS
0 CHill60 280
1 Sergey Alexandrovich Kryukov 264
2 OriginalGriff 170
3 Andy Lanng 160
4 Richard MacCutchan 130
0 CHill60 280
1 Sergey Alexandrovich Kryukov 125
2 Andy Lanng 120
3 Abhinav S 95
4 Maciej Los 95


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 4 Jul 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100