I am currently working on a former colleague's project that deals with saving emails from Exchange to our ERP system.
But I have run into a strange problem. Sometimes when the system receives an email, that contains an email as an attachment. It throws an error because the DataTimeReceived date is not set. Even after a .Load();
private int HandleEmail(Item item, Folder moveToFolder, Folder moveToFailedFolder, Mailbox mailbox, int fatherId = 0, string uploaderEmail = "", bool isEmbeddedMail = false)
The way it work. is that the HandleEmail() method goes through the original email until it gets the if-check.
If the attachment is not a FileAttachment, and is an ItemAttachment. The following code is called.
else if (attachment is ItemAttachment)
{
var itemAttachment = attachment as ItemAttachment;
itemAttachment.Load(new PropertySet(ItemSchema.Attachments, ItemSchema.TextBody, EmailMessageSchema.Sender, EmailMessageSchema.DisplayCc, EmailMessageSchema.DateTimeReceived, EmailMessageSchema.From, ItemSchema.MimeContent, ItemSchema.Body, ItemSchema.TextBody, EmailMessageSchema.BccRecipients, ItemSchema.Attachments));
var item_ = itemAttachment.Item;
HandleEmail(item_, null, null, mailbox, (int)mailid, uploaderEmail, true); // The attached email is then looped through like it is an regular email instead of like an image.
}
In this check. It takes the item attachment and loads an additional property set, which contains some data that is needed.
Finally the method calls it self, but now with the Item Attachment as to read it like a regular email.
My issue is the fact, that some attached emails do not have the DatetimeReceived property set.
Instead this error is shown in its place.
Microsoft.Exchange.Webservice.Data.ServicesObjectPropertyException
And this expection message is thrown
You must load or assign this property before you can read its value
And I can't quite figure out why it only happens to some attached emails. When I look at the emails it self, it does have an received date. But for some reason I can't get it using .Load()
I've tried a few things, such as using .Load for both Item and ItemAttachment, both without getting anything worthwhile.
Tried looking into using the service.loadpropertiesforitems()
I've forgotten a few of the other things I've looked into, since I have been looking at this for a few days before the weekend aswell.
Two suggestions i would have is first try the EmailMessageSchema.DateTimeSent (which should be the same as DateTimeReceived) the difference maybe being caused when attaching a messages that was sent. The other thing would be enable tracing and have a look at the traces to see what is actually coming back from the server also maybe just try loading the BasePropertySet.FirstClassProperties rather then your custom set (just to test that property anyway)
Related
I'm intercepting Outlook 2013's Application.ItemSend event in order to manipulate the categories assigned to a MailItem right before it's sent. Here's the event handler:
void Application_ItemSend(object Item, ref bool Cancel)
{
var mail = (Outlook.MailItem)Item;
mail.Categories = string.Join(";", "Foo", "Bar"); // Yes, the delimiter is ';' on my system.
mail.Save(); // Do I need this?
}
The problem is that the changes to the Categories property don't seem to be properly persisted. When I view the message in the Sent folder, it appears uncategorized.
Curiously, if I call mail.ShowCategoriesDialog() after changing the categories they appear checked as expected. This makes me suspect that I'm operating on a copy of the message.
What am I doing wrong?
It seems the issue was one of server configuration rather than my code. After connecting Outlook to GMail instead it worked as I expected.
It worked for me on Outlook 2013 ( 64-bit ) with a Gmail account configured. i.e. I can see the categories applied at all the steps - viz., while applying the categories in Visual Studio 2013, in the Outbox and also in the Sent Items folder.
Possible reason -
Some addin might be removing the categories in the Sent Item folder. try to disable other addin(s).
I have the problem that when i add a custom UserProperty to an Outlook MailItem, it does not get synced to other connected Outlooks.
What can i do to force Outlook to sync the whole email?
My overal problem:
I've got a shared Exchange Mailbox, opened on two clients (in Outlook)
I would like to lock a mail item, if it gets opened in one Outlook and show the second Outlook user a message like "The user XX is currently reading this email"
My way to solve the problem:
Creating a Outlook Plugin.
When user "A" is opening the Email, I am adding a "LockingUser" UserProperty to the MailItem object. If user "B" is trying to open the Email, I am first looking if a "LockingUser" Property exists.
I have disabled the cached mode.
I have tried to update the subject of the email: this works perfectly and gets synced immediatly (but is not a solution for my problem)
private void SetLockingUser(Outlook.MailItem mail)
{
var lockingUserProperty = mail.UserProperties.Find("LockingUser");
if (lockingUserProperty != null)
{
MessageBox.Show("Email locked by: " + lockingUserProperty.Value);
return;
}
var identity = System.Security.Principal.WindowsIdentity.GetCurrent();
var username = identity != null ? identity.Name : "";
lockingUserProperty = mail.UserProperties.Add("LockingUser", Outlook.OlUserPropertyType.olText, false, 1);
lockingUserProperty.Value = username;
mail.Save();
}
Please show the relevant snippet of your code and make sure you call MailItem.Save. Also keep in mind that there will always be lag since the changes will take up to a couple minutes to sync to Exchange and then to another user if cached mode is used. You'd be better off using some external sync mechanism instead of a user property.
I'm using the free lib ImapX and I'm making an application to mark all my mails as receieved. Can anyone lend me a hand?
EDIT: Nevermind, found it out myself. They get marked as read when you process them.
You need to add flag, which will help you to update status as seen.
foreach (var mess in messages)
{
mess.SEEN = true;
}
Let me know if you are unable to change status.
First of all, if you're using the old ImapX library, I invite you to upgrade to ImapX 2. It's being constantly developed and supported. There is also sample code for all common operations.
The Process method of a message doesn't mark the message as read, it only downloads the whole message including attachments. In your case, if you call the Search method setting the second parameter to true, you don't have to call it for every single message.
To mark a message as read simply use the AddFlag method of Message:
ImapX.FolderCollection folders = imapclient.Folders;
ImapX.MessageCollection messages = imapclient.Folders["INBOX"].Search("UNSEEN", true);
foreach (var mess in messages)
{
mess.AddFlag(ImapFlags.SEEN);
}
I got the following snippet (SomeName/SomeDomain contains real values in my code)
var entry = new DirectoryEntry("LDAP://CN=SomeName,OU=All Groups,dc=SomeDomain,dc=com");
foreach (object property in entry.Properties)
{
Console.WriteLine(property);
}
It prints OK for the first 21 properties, but then fail with:
COMException {"Unknown error (0x8000500c)"}
at System.DirectoryServices.PropertyValueCollection.PopulateList()
at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
at System.DirectoryServices.PropertyCollection.PropertyEnumerator.get_Entry()
at System.DirectoryServices.PropertyCollection.PropertyEnumerator.get_Current()
at ActiveDirectory.Tests.IntegrationTests.ObjectFactoryTests.TestMethod1() in MyTests.cs:line 22
Why? How can I prevent it?
Update
It's a custom attribute that fails.
I've tried to use entry.RefreshCache() and entry.RefreshCache(new[]{"theAttributeName"}) before enumerating the properties (which didn't help).
Update2
entry.InvokeGet("theAttributeName") works (and without RefreshCache).
Can someone explain why?
Update3
It works if I supply the FQDN to the item: LDAP://srv00014.ssab.com/CN=SomeName,xxxx
Bounty
I'm looking for an answer which addresses the following:
Why entry.Properties["customAttributeName"] fails with the mentioned exception
Why entry.InvokeGet("customAttributeName") works
The cause of the exception
How to get both working
If one wants to access a custom attribute from a machine that is not
part of the domain where the custom attribute resides (the credentials
of the logged in user don't matter) one needs to pass the fully
qualified name of the object is trying to access otherwise the schema
cache on the client machine is not properly refreshed, nevermind all
the schema.refresh() calls you make
Found here. This sounds like your problem, given the updates made to the question.
Using the Err.exe tool here
http://www.microsoft.com/download/en/details.aspx?id=985
It spits out:
for hex 0x8000500c / decimal -2147463156 :
E_ADS_CANT_CONVERT_DATATYPE adserr.h
The directory datatype cannot be converted to/from a native
DS datatype
1 matches found for "0x8000500c"
Googled "The directory datatype cannot be converted to/from a native" and found this KB:
http://support.microsoft.com/kb/907462
I have the same failure. I´m read and saw a lot of questions about the error 0x8000500c by listing attribute from a DirectoryEntry.
I could see, with the Process Monitor (Sysinternals), that my process has read a schema file. This schema file is saved under
C:\Users\xxxx\AppData\Local\Microsoft\Windows\SchCache\xyz.sch.
Remove this file and the program works fine :)
I just encountered the issue and mine was with a web application.
I had this bit of code which pulls the user out of windows authentication in IIS and pulls their info from AD.
using (var context = new PrincipalContext(ContextType.Domain))
{
var name = UserPrincipal.Current.DisplayName;
var principal = UserPrincipal.FindByIdentity(context, this.user.Identity.Name);
if (principal != null)
{
this.fullName = principal.GivenName + " " + principal.Surname;
}
else
{
this.fullName = string.Empty;
}
}
This worked fine in my tests, but when I published the website it would come up with this error on FindByIdentity call.
I fixed the issue by using correct user for the app-pool of the website. As soon as I fixed that, this started working.
I had the same problem with a custom attribute of a weird data type. I had a utility program that would extract the value, but some more structured code in a service that would not.
The utility was working directly with a SearchResult object, while the service was using a DirectoryEntry.
It distilled out to this.
SearchResult result;
result.Properties[customProp]; // might work for you
result.Properties[customProp][0]; // works for me. see below
using (DirectoryEntry entry = result.GetDirectoryEntry())
{
entry.Properties[customProp]; // fails
entry.InvokeGet(customProp); // fails as well for the weird data
}
My gut feel is that the SearchResult is a little less of an enforcer and returns back whatever it has.
When this is converted to a DirectoryEntry, this code munges the weird data type so that even InvokeGet fails.
My actual extraction code with the extra [0] looks like:
byte[] bytes = (byte[])((result.Properties[customProp][0]));
String customValue = System.Text.Encoding.UTF8.GetString(bytes);
I picked up the second line from another posting on the site.
The plot:
I am creating an outlook 2007 add-in using VSTO and C# in Visual Studio 2010. The purpose of the add-in is to cause tracking of mails sent to customers. The add-in should insert a tracking code into every outbound mail that later enables it to be recognized and auto archived once the customer replies. My goal is to insert a tracking code as an attribute in the opening tag of the mail's HTML:
<html tracking="ddfwer4w5c45cgx345gtg4g" ...>
This will be done in the event handler of the Application.ItemSend event:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
void Application_ItemSend(object Item, ref bool Cancel)
{
if (Item is Outlook.MailItem)
{
Outlook.MailItem mail = (Outlook.MailItem)Item;
mail.HTMLBody = "<html tracking=\"4w5te45yv5e6ye57j57jr6\" ...";
}
}
The problem:
It seem to never change the HTMLBody property. And it does not throw any exceptions. It just does nothing. I rewrote this logic directly in VBA in Outlook, and tried to change the HTMLBody, but it still did not change. How I know it did not change the HTMLBody is by stepping through and hovering over the property to see the current value.
I am however able to make changes to the MailItem.Body property. But since I need to hide the tracking code in some way, the Body property does not help me at all. I also added the MailItem.Save() method after changing the HTMLBody property, but no change.
I thought that perhaps the ItemSend event is to late and I can't change the HTMLBody at that time anymore, but I can't find any event like BeforeSend or alike.
Any ideas would be much appreciated.
I use VB a lot more than C#, but I think I see your problem.
Check the function signature of Application_ItemSend. I see that Cancel is passed by reference; shouldn't Item be passed that way too?
void Application_ItemSend(ref object Item, ref bool Cancel)
{
// ...
}
If Item is passed by value, then your code is only manipulating the copy that was made; the original object isn't being touched. At least, that's how it worked in C++ (but my C++ experience is before .Net, so maybe that doesn't apply).
I had a similar need to put some ids into the message. Seems like Outlook thoroughly scrubs the email before sending it, so by the time it is sent, all the nice hidden properties that you set have been removed.
For my particular need, I ended up instead using the UserProperties element of MailItem. This persists from when the email is created to when it ends up in the Sent Items folder. Perhaps this approach can work for you if the customer reply is tried to the sent email through a conversation?
mailItem.UserProperties.Add("myid", Outlook.OlUserPropertyType.olText);
mailItem.UserProperties["myid"].Value = myid;