|
-
May 15th, 2015, 04:52 AM
#1
Thread Starter
Fanatic Member
[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.
-
May 20th, 2015, 04:29 AM
#2
Thread Starter
Fanatic Member
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?
-
May 22nd, 2015, 02:35 AM
#3
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
-
May 22nd, 2015, 06:44 AM
#4
Re: Saving an email attachment to a different domain using EWS
 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?
 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
-
May 22nd, 2015, 08:44 AM
#5
Thread Starter
Fanatic Member
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?
-
May 22nd, 2015, 11:21 AM
#6
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
-
May 26th, 2015, 02:59 AM
#7
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
-
May 26th, 2015, 09:50 AM
#8
Thread Starter
Fanatic Member
Re: Saving an email attachment to a different domain using EWS
Thanks, guys - I'll try that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|