downloading an .eml attachment using exchange web services - exchange-server

I have a utility I built that checks an exchange email box and downloads attachments to a specified location. However, I'm running into a bug with messages that have another email attached to them (in *.msg). Whenever these pop up the attachments properties are not available so i can not access them to download them.:
versus when a zip or something like that comes in:
is there away to detect that this is an .msg attachment? or perhaps, "cast" it as such. I know I can wrap this in a try catch but i dont want to fall over into converting the attachment to a .msg when perhaps it is another file type that it is causing this.
Any help would be appreciated.
Zach

Just cleaning up my SO but here is how I solved this. The eml attachment is actually an "ItemAttachment" instead of a "FileAttachment" versus the generic "Attachment." So the loop looks like this:
foreach (Attachment att in itm.Attachments)
{
if (att is FileAttachment)
{
var fileAttachment = att as FileAttachment;
//do some stuff
}
else
{
var itemAttachment = att as ItemAttachment;
//do some more stuff (these are most likely eml/msg attachments...
}
}

Related

UniqueBody empty when Body is not

I have inherited responsibility for a project from a previous developer which takes incoming emails and processes them into customer support tickets.
It mostly works fine but it is having problems with one particular email and I can't work out why.
In Outlook the email clearly has a body (some short text, an image and a signature). It is a new message and not a reply.
The exchange server version is 2013.
But when being processed by the code below UniqueBody is empty, while Body contains the correct text. This does not happen with any other emails I've come across on that server.
if (serverVersion >= ExchangeVersion.Exchange2010)
body = msg.UniqueBody.Text;
else
body = msg.Body.Text;
What would cause UniqueBody to be empty while Body is not?
Why would the previous developer prefer to use UniqueBody over Body, how do they differ?
Could be related to this?
Check if you request the properties correctly:
PropertySet ps = new PropertySet(ItemSchema.UniqueBody);
var email = EmailMessage.Bind(service, item.ItemId, ps);
If you do so, the UniqueBody-Property should not be empty.
As far as I know, UniqueBody should be set by the exchange-server to show you which part of the mail is relevant for your ticket:
https://msdn.microsoft.com/en-us/library/office/dd877075(v=exchg.150).aspx
If your customer answers later to the ticket-conversation, you only want the new text.
With a new mail/ticket: body == uniqueBody == "the text you want to use".

File replace in HttpPostedFileBase MVC3

If the file is uploading with the name already in the specified path, it is showing error. I want to replace with the new file. I have placed the error and the code. Please help to replace the existing image.
Error: The process cannot access the file '' because it is being used by another process.
Code:
[HttpPost]
public ActionResult MyUpload(HttpPostedFileBase file)
{
string filePath = string.Empty;
string path = "C:\\";
string filePath = string.Empty;
try
{
if (file != null && file.ContentLength > 0)
{
filePath = path + file.FileName;
file.SaveAs(filePath);
file.InputStream.Dispose();
GC.Collect();
// other operations, where can occur an exception
// (because the uploaded file can have a bad content etc.)
}
}
catch (Exception e)
{
}
}
Based upon what you posted, I would suggest the following:
[HttpPost]
public ActionResult Foo(HttpPostedFileBase file)
{
var path = #"C:\";
var filename = file.FileName;
file.SaveAs(System.IO.Path.Combine(path, filename));
// do stuff here.
return RedirectToAction("Index");
}
I had this very same issue. I had a sub-folder called "files" where I uploaded files using file.SaveAs() ('file' being of type 'HttpPostedFileBase' [using MVC]). After some local investigation and online research, it turns out that an IIS worker thread/process was locking my file. At first I thought {HttpPostedFileBase}.SaveAs() was somehow locking my file. I even tried to (first) close, and then dispose the {HttpPostedFileBase}.InputStream, and that also didn't help. In the end, it wasn't even related to the file upload process at all. After uploading the file (a PDF), I processed it using ITextSharp, but forgot to dispose of the object wrapper (so it was probably sitting in the GC). Because of that, the next file upload failed due to the locked existing file. BTW, I also tried file.InputStream.Dispose(); file.InputStream.Close(); GC.Collect();, but removed them all in the end, and it still works perfectly now. Bottom line, if an IIS Worker process is locking your file, there's probably a good reason - something somewhere in your code you are missing, and it most likely isn't the MVC framework or IIS itself gone rogue. ;) Also, as it turns out, {HttpPostedFileBase}.SaveAs() WILL overwrite files if they exist, so there's no need to delete them first.
One other tip: I read somewhere to watch out who is creating the files based on how your authorization is setup (whether or not you using an IIS process identity, or the logged-in user identity). In some cases, a file may be created by one logged in user, who then becomes the owner, and another file of the same name from a different user may become blocked; might be something to keep in mind in special cases (like intranet based web apps).

Add inline image in java mail sender

In my project when i send mail to gmail or yahoo mail then my image bind as inline image to the body part. But when i open the same mail in outlook then it show that image as attachement and show blank space in body part.
MimeMessagePreparator messagePreparator = new MimeMessagePreparator() {
#Override
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true);
messageHelper.setTo("abc#xyz.com");
messageHelper.setFrom("info#abc.com");
messageHelper.setSubject("Test");
messageHelper.setText(text, true);
FileSystemResource res = new FileSystemResource(new File(message_image_path));
messageHelper.addInline("identifier1234", res);
}
};
where message_image_path is path of my image and text is my html message
Hmmm... I didn't actually see a question in there, but I'm assuming you're asking why is it different. The reason it's different is because they're different programs written by different people. There's nothing that requires an email message to be displayed in any specific way. The information about attachments vs. inline is just hints to the mail reader. Some mail readers ignore those hints. There's not much you can do about that.
If you're trying to get a specific mail reader to display a message in a specific way, your best bet is to compose such a message in that mail reader, then look at the structure of the message you composed and duplicate that with JavaMail. Of course, that might not mean the message will display as expected in some other mail reader.
Your best bet is to keep it simple. Simple html, referencing images included with the message (a multipart/related message), may be the best way to accomplish what you want.

Delete Exchange 2003 Emails with WebDav

I am trying to manage an Inbox in Exchange 2003 automatically using webdav from a C# application. Looking at msdn is not helping me a whole lot as the methods described here (http://msdn.microsoft.com/en-us/library/aa142917.aspx) do not coincide at all with the samples I have found otherwise. So there are two things I am trying to determine:
Of all the fields that return from a webdav query
string reqStr =
#"<?xml version=""1.0""?>
<g:searchrequest xmlns:g=""DAV:"">
<g:sql>
SELECT
*
FROM
""http://server/Exchange/email1#domain.com/Inbox/""
WHERE ""urn:schemas:mailheader:from"" = 'email2#domain.com'
</g:sql>
</g:searchrequest>";
Which one is the unique identifier? I have browsed it (but not sure of a reference to verify the fields) and it appears at first glance that DAV:id is what I want (), but I am not wanting to work on assumptions.
Secondly, what is the correct way to programmatically delete an email after I have processed it? Would something like the following work (will it remove the entry and all related metadata). I don't want any files left orphaned on the server...
string reqStr =
#"<?xml version=""1.0""?>
<g:searchrequest xmlns:g=""DAV:"">
<g:sql>
DELETE
FROM
""http://server/Exchange/email1#domain.com/Inbox/""
WHERE ""DAV:id"" = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
</g:sql>
</g:searchrequest>";
And finally, what are the best online sources for investigating all the data returned in the XML from the first request, and where are all the options documented for managing the webdav interface? Looking at MSDN just hasn't been fruitful.
Look for the dav:hef tags tag in the response. They contain an url you can use to issue a delete command.
From the result of a query that gets you the msg Uri then:
var request = (HttpWebRequest)WebRequest.Create(mail.MailUri);
request.Credentials = _credential;
request.Method = "DELETE";
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK)
{
//something might of broke
}

Flex 4 coltware airxmail - send vCal appointment

I am using coltware.airxmail to send emails from my Flex app.
I would like to send VCalendar appointment files generated from Flex straight to Outlook so they are opened in the Calender view. I am able to send the VCal files as an attachment on an email, however, these are not "auto-opened" in Outlook Calendar, which requires the user to double click on the file.
I have been trying to set the content type of the mail to "text/x-vCalendar", and pass in a byte array containing the VCal file, however, no joy. The vCal arrives as a .txt attachment to an empty email!
I wonder if anyone has had previous experience with this kit, or can suggest any pointers?
Or even suggest another component they have used to send VCal files straight to outlook, from ActionScript?
Here's my sample code (DEMO CODE VERY MESSY JUST TO GET POINT ACROSS):
var sender:SMTPSender = new SMTPSender();
// Set the from / to / host / port values here
var contentType:ContentType = new ContentType();
contentType.setMainType("text/x-vCalendar");
var message:MimeMessage = new MimeMessage(contentType,"UTF-8");
var file:File = File.desktopDirectory.resolvePath("vcal.vcs");
file.addEventListener(Event.COMPLETE,
function(ev:Event):void {
message.addRawContent(file.data);
sender.send(message);
sender.close();
});
file.load();
Hopefully I can achieve this using the coltware component. There's nothing on their site about using these methods, although the API guide is very incomplete - just "basic usage"... http://code.google.com/p/airxmail/wiki/HowToUseAPI
Did you try using a different content type, such as "text/calendar"? see here: http://weblogs.asp.net/bradvincent/archive/2008/01/16/creating-vcalendars-programmatically.aspx

Resources