none
[E2007] [EWSMA] [C#]: Attachment FileName null? RRS feed

  • Question

  • Hi All I have this code to retrieve the file attachment:

     

    private void ProcessItem(EmailMessage mailItem)
        {
          if (!mailItem.HasAttachments)
          {
            return;
          }
    
          foreach (Attachment attachment in mailItem.Attachments)
          {
            if (!(attachment is FileAttachment))
            {
              continue;
            }
    
            try
            {
              FileAttachment fileAttachment = attachment as FileAttachment;
              fileAttachment.Load();
    
              FileProcessorHelper fileProcessor = new FileProcessorHelper();
              fileProcessor.ProcessFile(fileAttachment, _targetPath);
            }
            catch (Exception exception)
            {
              log.Fatal(string.Format("Error processing message {0} received from {1} at {2}. Exception was: {3}.", mailItem.ConversationTopic, mailItem.Sender, mailItem.DateTimeReceived, exception));
    
              // Let the exception bubble up now.
              throw;
            }
          }
          
          log.Info(string.Format("Processed message {0} received from {1} at {2}.", mailItem.ConversationTopic, mailItem.Sender, mailItem.DateTimeReceived));
        }
    

     

    The attachment names are usually like PO_758023_789_800_{A8054372-29FD-4FC0-9F29-2AD754B603D7}.XML, but this is being changed to body.xml somewhere between being sent and being received by Exchange.

    I thought I could try and read the fileAttachment.FileName but it is null. Has anyone come across this, or a possible solution?

    Thanks

    James

     

     

    Wednesday, June 29, 2011 8:26 AM

All replies

  • I am not sure why the fileName is null but the workaround I found was to use "fileAttachment.Name".

    What I found was once you load the fileAttachment to a file on your hard disk rather than memory, i.e.:

    fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);

    Then the Filename will not be null and will have the value e.g.:

    C:\\temp\\Warning_bg.gif

    I think the Name of the attachment will show "Warning_bg.gif" regardless of whether you load to memory or to file on hard disk

    Wednesday, June 29, 2011 12:24 PM
  • Thanks rbarr sandi I am currently using the ".Name" property, however the issue I am really trying to overcome is the fact the filename is changed in the first place! :(
    Thursday, June 30, 2011 12:12 AM
  • Thanks rbarr sandi I am currently using the ".Name" property, however the issue I am really trying to overcome is the fact the filename is changed in the first place! :(

    I don't know if Exchange automagically renames attachments, maybe it does when it considers the filename to be invalid.

    Out of interest what does the class/method:

         FileProcessorHelper fileProcessor = new FileProcessorHelper();
         fileProcessor.ProcessFile(fileAttachment, _targetPath);
    

    do?

    What does the code that sends the email look like?

     

     

    Thursday, June 30, 2011 9:24 AM
  • The file processor is just a class that processes the attachment based on content. In this case an xml packet is picked up and processed accordingly. i.e. Iterates the xml and determines what actions needs to occur per "record".

    The code that sends the email is from a customer, their ICT / development team have not been very helpful.

    Reading directly from the Exchange via webDav has the same issue, so I am guessing the server has the issue... bugger.

    Friday, July 1, 2011 3:42 AM