I'm currently writing a Windows Service to log in to a specific Exchange account, get any new emails, parse them, and save the email in the appropriate folder.
Everything is working perfectly, except saving the email.
Relevant code blocks (try / catch blocks and irrelevant stuff removed to keep it short):-
Set up the Service
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new WebCredentials(emailAddress, password);
service.AutodiscoverUrl(emailAddress, RedirectionUrlValidationCallback);
CheckForNewEmails(service);
Getting and checking new emails
private static void CheckForNewEmails(ExchangeService service)
{
int offset = 0;
int pageSize = 50;
bool more = true;
ItemView view = new ItemView(pageSize, offset, OffsetBasePoint.Beginning);
view.PropertySet = PropertySet.IdOnly;
FindItemsResults<Item> findResults;
List<EmailMessage> emails = new List<EmailMessage>();
while (more)
{
findResults = service.FindItems(WellKnownFolderName.Inbox, view);
foreach (var item in findResults.Items)
{
emails.Add((EmailMessage)item);
}
more = findResults.MoreAvailable;
if (more)
{
view.Offset += pageSize;
}
}
if (emails.Count > 0)
{
PropertySet properties = (BasePropertySet.FirstClassProperties);
service.LoadPropertiesForItems(emails, properties);
var mailItems = new List<DatabaseService.MailItem>();
var dbService = new DatabaseService();
var defaultUser = dbService.GetDefaultUser(defaultUserID);
foreach (var email in emails)
{
var mailItem = new DatabaseService.MailItem();
mailItem.mail = email;
mailItem.MessageID = email.InternetMessageId;
mailItem.Sender = email.Sender.Address;
dbService.FindLinks(service, ref mailItem, defaultUser);
mailItems.Add(mailItem);
LogMessage += (string.Format("Message ID : {1}{0}Sent : {2}{0}From : {3}{0}Subject : {4}{0}Hash : {5}{0}{6}{0}{0}", Environment.NewLine,
mailItem.MessageID ,
email.DateTimeSent.ToString("dd/MM/yyyy hh:mm:ss"),
email.Sender,
email.Subject,
mailItem.Hash,
mailItem.LinkString
));
}
}
}
Finding who it should be linked to
public void FindLinks(ExchangeService service, ref MailItem mailItem, User defaultUser)
{
string address = mailItem.Sender;
// get file hash
var tempPath = Path.GetTempPath();
var fileName = GetFilenameFromSubject(mailItem);
var fullName = Path.Combine(tempPath, fileName);
SaveAsEML(service, mailItem, fullName);
var sha = new SHA256Managed();
mailItem.Hash = Convert.ToBase64String(sha.ComputeHash(File.OpenRead(fullName)));
File.Delete(fullName);
using (var db = DatabaseHelpers.GetEntityModel())
{
// Do all the linking stuff
}
}
And finally, the problem area: Saving the file to disk (in this case to a temporary folder so I can get the file hash, to check it's not a duplicate (e.g. CC'd etc))
Looking through StackOverflow and various other sources, there seem to be two ways to do this:-
1) Using the MailItem.Load method
private string SaveAsEML(ExchangeService service, MailItem mailItem, string savePath)
{
using (FileStream fileStream = File.Open(savePath, FileMode.Create, FileAccess.Write))
{
mailItem.mail.Load(new PropertySet(ItemSchema.MimeContent));
fileStream.Write(mailItem.mail.MimeContent.Content, 0, mailItem.mail.MimeContent.Content.Length);
}
}
Using the above code, the file is created and has the correct content.
However, attempting to access any of the email properties AFTER this point results in a Null Exception crash (big crash too, no Try/Catch or UnhandledException trap will pick it up, just kills the Service)
In the above code, this line crashes the entire service:-
LogMessage += (string.Format("Message ID : {1}{0}Sent : {2}{0}From : {3}{0}Subject : {4}{0}Hash : {5}{0}{6}{0}{0}", Environment.NewLine,
mailItem.MessageID ,
email.DateTimeSent.ToString("dd/MM/yyyy hh:mm:ss"),
email.Sender,
email.Subject,
mailItem.Hash,
mailItem.LinkString
));
Specifically, referencing email.DateTimeSent
I added in some diagnostic code directly before this line:-
if (email == null) { Log it's null; } else { Log NOT null;}
if (email.DateTimeSent == null) { Log it's null; } else { Log NOT null; }
The first line logs NOT null, so email still exists.
However, the second line instantly crashes with a null exception error, without logging anything.
If I comment out the line SaveAsEML(service, mailItem, fullName); from FindLinks then everything works perfectly (except of course the file isn't saved).
2) Binding a Property Set
private string SaveAsEML(ExchangeService service, MailItem mailItem, string savePath)
{
using (FileStream fileStream = File.Open(savePath, FileMode.Create, FileAccess.Write))
{
PropertySet props = new PropertySet(EmailMessageSchema.MimeContent);
var email = EmailMessage.Bind(service, mailItem.mail.Id, props);
fileStream.Write(mailItem.mail.MimeContent.Content, 0, mailItem.mail.MimeContent.Content.Length);
}
}
Doing it this way, nothing crashes, it goes through every email just fine, and can reference email.DateTimeSent and all the other properties.
Unfortunately, it creates zero-length files, no content.
Been banging my head against the wall for hours now over this (took me an hour of adding diagnostics everywhere just to track down the crash happening when referencing properties), and it's undoubtedly something trivial I've overlooked, so if some kind soul could point out my stupidity I'd be most grateful!
Edit:
I was able to work around the above problem easily enough by simply saving the value of the properties before using them:-
foreach (var email in emails)
{
var dateTimeSent = email.DateTimeSent;
var sender = email.Sender;
var subject = email.Subject;
var mailItem = new DatabaseService.MailItem();
mailItem.mail = email;
mailItem.MessageID = email.InternetMessageId;
mailItem.Sender = email.Sender.Address;
dbService.FindLinks(service, ref mailItem, defaultUser);
mailItems.Add(mailItem);
LogMessage += (string.Format("Message ID : {1}{0}Sent : {2}{0}From : {3}{0}Subject : {4}{0}Hash : {5}{0}{6}{0}{0}", Environment.NewLine,
mailItem.MessageID,
dateTimeSent.ToString("dd/MM/yyyy hh:mm:ss"),
sender,
subject,
mailItem.Hash ?? "No Hash",
mailItem.LinkString ?? "No Linkstring"
));
}
This allowed me to use the MailItem.Load method which saved the file, and thus do what I was after in this specific case.
However there are other things this Service will need to do, and I really don't want to have to save a copy of every single property I'll need to access.
The reason this would fail
private string SaveAsEML(ExchangeService service, MailItem mailItem, string savePath)
{
using (FileStream fileStream = File.Open(savePath, FileMode.Create, FileAccess.Write))
{
PropertySet props = new PropertySet(EmailMessageSchema.MimeContent);
var email = EmailMessage.Bind(service, mailItem.mail.Id, props);
fileStream.Write(mailItem.mail.MimeContent.Content, 0, mailItem.mail.MimeContent.Content.Length);
}
}
Is that you have used Bind to Load the message with the MimeContent in the email variable and then you haven't used that. mailItem.mail won't be linked at all to email variable created as part of this operation (even though they are the same object on the server). Locally these are just two separate variables. EWS is client/server so you make a request and the Managed API will return a local object that represents the result of the operation. But that object is disconnected hence when you do the bind above it just generates another client object to represent the result of that operation. eg so the above should have been
private string SaveAsEML(ExchangeService service, MailItem mailItem, string savePath)
{
using (FileStream fileStream = File.Open(savePath, FileMode.Create, FileAccess.Write))
{
PropertySet props = new PropertySet(EmailMessageSchema.MimeContent);
var email = EmailMessage.Bind(service, mailItem.mail.Id, props);
fileStream.Write(email.MimeContent.Content, 0, email .MimeContent.Content.Length);
}
}
Related
Currently my application has ability to process incoming emails and bounce then back if it does not match with given criteria in code. However, I want to add up another type of email to Process which are NDR "Non-Delivery Reports" from Microsoft Exchange Server. So my application do not responsd/Bounce back NDR to exchange server which cause a loop between my Mailbox and Exchange Server.
Following method triggers when Invalid doesn't have a specific
private static void ProcessInvidMsgWithoutNo(string sMsgFrom, string sFromEmail, EmailMsg sMsgReceived, EmailMessage message)
{
EmailMsg.MoveToInvalid(message);
sMsgReceived.IsValid = false;
SaveMsgReceived(sMsgReceived, 0, string.Empty);
if (!sFromEmail.Equals(string.Empty))
{
ResponseForInvidMsg(sFromEmail);
}
else
{
curLog.WriteLog(string.Format(CultureInfo.CurrentCulture, MsgEMChecker27, sMsgFrom));
}
}
Following Method triggers to respond incoming Invalid message as stated above.
private static void ResponseForInvidMsg(string sFromEmail)
{
string tErrSubjectMsg = String.Format(CultureInfo.CurrentCulture, "{0}\\Resource\\MsgErrorSubjectAck.html", Entity.GetSetting("DocRootDir"));
StringBuilder htmlText = new StringBuilder();
FileStream fsFile = new FileStream(tErrSubjectMsg, FileMode.Open);
if (fsFile != null)
{
StreamReader reader = new StreamReader(fsFile, Encoding.Default);
string text;
do
{
text = reader.ReadLine();
if ((text != null) && (text != ""))
htmlText.Append(text + "\n");
} while (text != null);
reader.Close();
fsFile.Close();
fsFile = null;
}
else
htmlText.Append("hello");
string tToCustomerSubject = ReplyForInvalid;
string tMessage = htmlText.ToString();
EmailMsg emTo = new EmailMsg(string.Empty, sFromEmail, tToCustomerSubject, tMessage);
emTo.MsgType = EmailMsg.TypeSentCustomer;
emTo.Send(false); //Not save but CC to generic email box
}
Please, help me to find a way where I can stop my code to respond Exchange Server NDR. Thanks
The place to start would be to check the ItemClass of the message see https://learn.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-asemail/51d84da6-a2da-41e9-8ca7-eb6c4e72c28d . NDR's, delivery report etc should have a prefix of Report eg
REPORT.IPM.NOTE.NDR Non-delivery report for a standard message.
REPORT.IPM.NOTE.DR Delivery receipt for a standard message.
I'm using the EWS Managed API in C# to download a bunch of messages from my company's exchange server. Loading the messages themselves takes a long time considering that service.FindItems() only fetches limited information about the messages, but it's not a huge deal. The serious problem I'm facing is how long it takes to load attachments.
The program is supposed to display an email and its image attachment side-by-side. When loading a new email, it can take well over a minute for the attachment to load. I initially fetched the attachments for each message when the message was loaded, but I thought it would be better to try to load them all at once into a List<EmailMessage> so the program wouldn't have to fetch the attachments when loading individual messages.
Here's the code I used to do that:
fetchView.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
fetchView.Traversal = FolderTraversal.Deep;
//create itemView for actual message query since we finally found the damn folder
ItemView iView = new ItemView(int.MaxValue);
FolderId sharedInboxFolder = new FolderId(WellKnownFolderName.Root, sharedMailbox);
FolderId targetFolder = new FolderId(WellKnownFolderName.Root, sharedMailbox);
FindFoldersResults inboxFolders = service.FindFolders(sharedInboxFolder, fetchView);
bool folderFound = false;
//look through the folders in the inbox to find the user-specified one by name
foreach(Folder f in inboxFolders)
{
if (f.DisplayName == Properties.Settings.Default.InboxFolder)
{
targetFolder = f.Id;
folderFound = true;
}
}
// Set itemview properties for FindItems() operation
fullProperties.Add(ItemSchema.Body);
fullProperties.Add(ItemSchema.Attachments);
fullProperties.Add(ItemSchema.DateTimeReceived);
fullProperties.Add(ItemSchema.Subject);
if (!folderFound)
{
MessageBox.Show("Folder not found!");
} else {
SearchFilter greaterthanfilter = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, searchDate);
SearchFilter lessthanfilter = new SearchFilter.IsLessThan(ItemSchema.DateTimeReceived, searchDate.AddDays(1));
SearchFilter dayFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, greaterthanfilter, lessthanfilter);
FindItemsResults<Item> fetchedMessages = service.FindItems(targetFolder, dayFilter, iView);
foreach (Item i in fetchedMessages.Items)
{
EmailMessage msg = EmailMessage.Bind(service, i.Id, fullProperties);
emails.Add(msg);
}
}
}
I then save all the attachments to disk with
for (int i = 0; i < message.Attachments.Count; i++)
{
if (message.Attachments[i] is FileAttachment)
{
FileAttachment att = message.Attachments[i] as FileAttachment;
att.Load();
using (FileStream attStream = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + #"\Program\images\" + i.ToString(), FileMode.Create, FileAccess.ReadWrite))
{
att.Load(attStream);
attStream.Close();
attStream.Dispose();
}
}
else
{
MessageBox.Show("Not FileAttachment!");
}
}
Then, to load an image and it's attachment, I do
imgViewer.Source = new BitmapImage(new Uri(/some/path/to/image/))
I suspect the hangup is during the save attachments phase. I found this post that indicated TraceFlags should be disabled, so I did service.TraceFlags = TraceFlags.None but that didn't seem to help at all. I'm contemplating just downloading all the attachments up front, or figuring out some kind of caching mechanism where I download the attachments of message[n+1...x] in the background while the user works on message[n], but this has limited usefulness, because the program should also let the user select an image and load it relatively instantly (i.e. much less than a minute).
Any suggestions are much appreciated.
Your loading the attachments twice for no apparent reason.
Remove the att.Load(); from your if (message.Attachments[i] is FileAttachment) statement.
Maybe have a look at implementing paging and in turn consider reducing your ItemView pageSize to batches of 1000 or less, instead of MaxValue
Also make sure you are only returning what you need in your PropertySet.
BasePropertySet.FirstClassProperties Consider changing this to just return what you need.
Side Note:
A good way to identify slow performing code is to use the .Net Stopwatch class.
Stopwatch sw = new Stopwatch();
sw.Start();
// code
sw.Stop();
StopWatchLog stopWatchLog = new StopWatchLog();
stopWatchLog.CreateXMLTextFile("MethodName()" + " took" + sw.Elapsed.Seconds + " seconds.");
I've got a strange problem with EWS. I am writing a console app which queries my inbox for messages with a subject containing 'Ref:' which is absolutely fine, I get a connection no problem, run the query, get some results. but strangely it is only returning messages that are from internal senders, any messages sent from outside the exchange organisation do not appear in the results.
ultimately I am going to be using a service account to connect to various mailboxes and find various emails with certain references in the subject....
Does anyone have any idea what might be going on? I've gone over it and over it and can't find any reason why it's doing it.
It's Exchange 2010 SP2
ExchangeService exchService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
exchService.Url = new System.Uri("https://xxx.xxxxxxxxx.co.uk/EWS/Exchange.asmx");
exchService.UseDefaultCredentials = true;
var userMailbox = new Mailbox("xxxx#xxxx.co.uk");
var inboxFolder = new FolderId(WellKnownFolderName.Inbox, userMailbox);
String qString = "subject:\"Ref:\"";
ItemView view = new ItemView(20);
view.PropertySet = new PropertySet(ItemSchema.Id);
try
{
FindItemsResults<Item> results = exchService.FindItems(inboxFolder, qString, view);
if (results.Items.Count > 0)
{
foreach (Item item in results.Items)
{
Console.WriteLine(item.Id);
if (item is EmailMessage)
{
EmailMessage bindMessage = EmailMessage.Bind(exchService, item.Id.ToString());
String sender = bindMessage.Sender.Address.ToString();
String Body = bindMessage.Body.Text.ToString();
Console.WriteLine("Sender: " + sender);
}
}
}
}
When I Use OutlookSpy to get the EntryIDs from MailItems in a particular folder and supply them to the following code:
Outlook.Application myApp = new Outlook.ApplicationClass();
Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
try
{
object obj = mapiNameSpace.GetItemFromID(sEntryID);
if (obj is Outlook.MailItem)
{
var getItem = (Outlook.MailItem)mapiNameSpace.GetItemFromID(sEntryID);
getItem.Display();
}
}
catch (Exception Ex)
{
Global.Common.LogError("Error accessing MailItem", Ex, "EntryID " + sEntryID + " not found in " + sFolder, "Warning");
}
I get unknown messaging errors for some EntryID values and successful display of the messages in Outlook with others. Can anyone suggest what attributes the MailItems might have which will affect whether I can display them successfully using GetItemFromID or any other method of displaying all messages by EntryID reliably?
Was the message store where the message resides touched in the active Outlook session? The way MAPI providers work, when a provider is loaded by MAPI, it registers the set of entry id guids (bytes 5-20 in the entry id) that it will handle. If the particular PST store has not been touched in the current session, MAPI does not know anything about its entry ids.
You can either access all the stores in the current session first (to make sure MAPI knows about their entry ids) or use the store entry id (second parameter, optional) when calling GetItemFromId - this way Outlook will open the store first, then ask the store to open the item. You can also call Namespace.AddStore / AddStoreEx to load the given PST file if it is not already in the current profile.
You might also want to log the exception details (Ex.Message) in your exception handler.
Thanks to all the respondents - this explains why sometimes particular messages would open and sometimes they would not. By getting the StoreId using the following code:
Outlook.Application myApp = new Outlook.ApplicationClass();
Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Object oStoreID = Common.GetFolder(myApp, sFolder).StoreID;
try
{
object obj = mapiNameSpace.GetItemFromID(sEntryID,oStoreID);
if (obj is Outlook.MailItem)
{
Outlook.MailItem getItem = (Outlook.MailItem)mapiNameSpace.GetItemFromID(sEntryID,oStoreID);
getItem.Display();
}
}
Where
public static Outlook.Folder GetFolder(Outlook.Application App, string folderPath)
{
Outlook.Folder folder;
string backslash = #"\";
try
{
if (folderPath.StartsWith(#"\\"))
{
folderPath = folderPath.Remove(0, 2);
}
String[] folders =
folderPath.Split(backslash.ToCharArray());
folder =
App.Session.Folders[folders[0]]
as Outlook.Folder;
if (folder != null)
{
for (int i = 1; i <= folders.GetUpperBound(0); i++)
{
Outlook.Folders subFolders = folder.Folders;
folder = subFolders[folders[i]]
as Outlook.Folder;
if (folder == null)
{
return null;
}
}
}
return folder;
}
catch { return null; }
}
All MailItems now display in Outlook.
I'll just throw this in here for posterity-- Outlook 2002 requires that the entry ID supplied to GetItemFromID use upper-case hex characters.
I want to move/copy my Email Attachments to new folder in outlook ;/ and my code doesn't work properly.
foreach(Item item in findResults.Items)
{
EmailMessage email = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments));
if(false)
{
// OTC Marker HTML Body
}
else
{
if (email.HasAttachments)
{
foreach (Attachment attachment in email.Attachments)
{
EmailMessage emailAttachment = EmailMessage.Bind(service, attachment.Id, new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments));
ItemAttachment itemAttachment = attachment as ItemAttachment;
itemAttachment.Load();
EmailMessage mess = itemAttachment.Item as EmailMessage;
moveToTestFolder (mess, #"TestFolder");
}
}
else
{
//to do
}
}
}
And my moveToTestFolder method:
private void moveToTestFolder (EmailMessage item, string folderName)
{
Folder rootfolder = Folder.Bind(service, WellKnownFolderName.MsgFolderRoot);
rootfolder.Load();
var folders = rootfolder.FindFolders(new FolderView(20));
var folderItemToMove = folders.FirstOrDefault(f => f.DisplayName.Equals(folderName, StringComparison.OrdinalIgnoreCase));
item.Move(folderItemToMove.Id);
}
I'am trying to move attachment (if it is an email) to special folder in outlook. Moving the normal message is working now.
That won't work because you can only use the Move and Copy operations to copy an actual Mailbox Item not Attachments (you should be getting an error about and Invalid Id). One workaround for this is to get the MimeContent for the Email Attachment you want to move and then create a New object from that MimeCotent and save it to the folder you want to move the Item to eg
foreach (Attachment Attach in EWSItem.Attachments)
{
if (Attach is ItemAttachment)
{
PropertySet psProp = new PropertySet(BasePropertySet.FirstClassProperties);
psProp.Add(ItemSchema.MimeContent);
((ItemAttachment)Attach).Load(psProp);
if (((ItemAttachment)Attach).Item.MimeContent != null)
{
EmailMessage NewMessage = new EmailMessage(service);
NewMessage.MimeContent = ((ItemAttachment)Attach).Item.MimeContent;
NewMessage.SetExtendedProperty(new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer), "1");
NewMessage.Save(folderItemToMove.Id);
}
}
}
You don't get full Fidelity of all the Exchange properties on the Message with this method as only the MimeContent is copied which is generally not a problem with Email but will be an issue for other objects types like Contacts, Tasks etc.
Cheers
Glen