[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.
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?
Re: Saving an email attachment to a different domain using EWS
Quote:
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!
Quote:
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.
Quote:
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?
Re: Saving an email attachment to a different domain using EWS
Quote:
Originally Posted by
InvisibleDuncan
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
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
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?
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
Re: Saving an email attachment to a different domain using EWS
Quote:
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 :)
Re: Saving an email attachment to a different domain using EWS
Thanks, guys - I'll try that.