Results 1 to 8 of 8

Thread: [RESOLVED] Saving an email attachment to a different domain using EWS

  1. #1

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Resolved [RESOLVED] Saving an email attachment to a different domain using EWS

    I've written an application that will read the emails in a given mailbox and save the attachments. Unfortunately, I've now been told that they will have to be saved to a network location on another domain.

    Does anyone know how to achieve this? My current code is below, and it's the fileAttachment.Load(savedFileName); statement that does the saving.

    Code:
    Log.Debug("Retrieving items from Inbox.");
    var theseItems = folder.FindItems(new ItemView(200));
    foreach (Item outlookItem in theseItems)
    {
        if (outlookItem is EmailMessage)
        {
            var message = (EmailMessage)outlookItem;
            Log.Debug("Processing email " + message.Subject + " from " + message.Sender);
            foreach (Attachment attachment in message.Attachments)
            {
                if (attachment is FileAttachment)
                {
                    FileAttachment fileAttachment = attachment as FileAttachment;
                    if (fileAttachment.Name.EndsWith(".csv") || fileAttachment.Name.EndsWith(".xls"))
                    {
                        var savedFileName = destinationDirectory + fileAttachment.Name;
                        Log.Debug("Saving file " + savedFileName);
                        fileAttachment.Load(savedFileName);
                    }
                }
            }
        }
    }
    Incidentally, I haven't tested this yet because I still haven't been given the mailbox credentials, so I'm not 100% certain that it will work as it is.
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  2. #2

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Re: Saving an email attachment to a different domain using EWS

    Hmmm. Okay, maybe I shouldn't have asked a bunch of VB developers!

    I think my only option is to save it locally on my current domain and then use impersonation (another new thing for me!) to copy it over. Does that sound sensible?
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  3. #3
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Saving an email attachment to a different domain using EWS

    Hmmm. Okay, maybe I shouldn't have asked a bunch of VB developers!
    Lol there are a few of us here who do C# but EWS and exchange integration are fairly specialist, either you have done it or you haven't!

    I've written an application that will read the emails in a given mailbox and save the attachments. Unfortunately, I've now been told that they will have to be saved to a network location on another domain.
    I think my only option is to save it locally on my current domain and then use impersonation (another new thing for me!) to copy it over. Does that sound sensible?
    Why do you need to use Impersonation? if you save the attachments locally to you, can you not just copy them across as files to the other domain location? OR am i missing part of your requirement?
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Saving an email attachment to a different domain using EWS

    Quote Originally Posted by InvisibleDuncan View Post
    Hmmm. Okay, maybe I shouldn't have asked a bunch of VB developers!

    I think my only option is to save it locally on my current domain and then use impersonation (another new thing for me!) to copy it over. Does that sound sensible?
    Quote Originally Posted by NeedSomeAnswers View Post
    Lol there are a few of us here who do C# but EWS and exchange integration are fairly specialist, either you have done it or you haven't!

    Why do you need to use Impersonation? if you save the attachments locally to you, can you not just copy them across as files to the other domain location? OR am i missing part of your requirement?
    NSA - I am currently a part of one domain... I cannot simply access a resource on a different domain ... that's what the impersonation does... allows the current context to impersonate a user on a different domain. We do this in our app, although we are usually impersonating a different user on the same domain - it's a web-based app, the IISUSER doesn't always have access to certain file system resources, so the user has the ability to tell the system, hey when you access the file system, use this user instead.

    Duncan - yes, impersonation is going to be likely the way to go.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Re: Saving an email attachment to a different domain using EWS

    Thanks for the answers, guys.

    TG: One thing I'm confused about with impersonation is that usually I would use a File.Move to move the file. How can I be two different users when doing that - the one with access to the source location and the one with access to the destination location? At what point do I need to impersonate the second user?
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Saving an email attachment to a different domain using EWS

    you should have access locally, no? so you save the file locally, then initiate the impersonation context, copy the file form the local file system to the alternate domain location, then drop the impersonation.

    You may not even need the local file... read the attachment(s) into a memory stream/buffer, start impersonation, write the bits to the file and badda boom, badda bing, you're done.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Saving an email attachment to a different domain using EWS

    NSA - I am currently a part of one domain... I cannot simply access a resource on a different domain ... that's what the impersonation does... allows the current context to impersonate a user on a different domain.
    Of course it can't i should and do really know that but hey, brain fade
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  8. #8

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Re: Saving an email attachment to a different domain using EWS

    Thanks, guys - I'll try that.
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width