Click here to Skip to main content
15,886,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 4-Jul-12 1:29am
v2

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


C#
<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....
 
Share this answer
 
v2
C#
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);

             }
         }
     }
 }
 
Share this answer
 
v2
Comments
Gokulnath007 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!!!
DamithSL 4-Jul-12 8:38am    
Check updated answer
Hedi Sangoku 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)



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