I've been fighting this problem for some time now, and have failed to find an answer online that works. I am using the Exchange EWS API to do some email processing. One of the things I need to process is an EmailMessage that has attachments on it. One of those attachments happens to be another EmailMessage. I will refer to this as the attached EmailMessage.
I want to convert this EmailMessage to a byte[], however every time I try, I get an Exception. Below is my code:
if (((ItemAttachment)attachment).Item is EmailMessage)
{
EmailMessage msg = ((ItemAttachment)attachment).Item as EmailMessage;
msg.Load(new PropertySet(ItemSchema.MimeContent));
byte[] content = msg.MimeContent.Content;
}
The problem is no matter what I try to load, I get an exception thrown saying
An exception of type 'System.InvalidOperationException' occurred in Microsoft.Exchange.WebServices.dll but was not handled in user code
Additional information: This operation isn't supported on attachments.
If I don't call msg.Load(), I get a different error saying I need to load the content.
I don't understand this. If I do the same operation on an EmailMessage that was not attached to anything, it works just fine. Why does it matter that the EmailMessage was an attachment at one point in time? How can I get the EWS/.NET/Whatever is throwing the Exception to treat the attached EmailMessage as an EmailMessage and not an ItemAttachment?
You need to use the GetAttachments operations on Each on the Embedded Attachments with a propertyset that includes the MimeContent. eg something like (this can made a lot more effienct by grouping the GetAttachment requests if your processing multiple message etc).
PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties);
psPropSet.Add(ItemSchema.MimeContent);
foreach (Attachment attachment in CurrentMessage.Attachments)
{
if (attachment is ItemAttachment)
{
attachment.Load();
if (((ItemAttachment)attachment).Item is EmailMessage)
{
EmailMessage ebMessage = ((ItemAttachment)attachment).Item as EmailMessage;
foreach (Attachment ebAttachment in ebMessage.Attachments)
{
if (ebAttachment is ItemAttachment)
{
Attachment[] LoadAttachments = new Attachment[1];
LoadAttachments[0] = ebAttachment;
ServiceResponseCollection<GetAttachmentResponse> getAttachmentresps = service.GetAttachments(LoadAttachments, BodyType.HTML, psPropSet);
foreach (GetAttachmentResponse grResp in getAttachmentresps)
{
EmailMessage msg = ((ItemAttachment)grResp.Attachment).Item as EmailMessage;
msg.Load(new PropertySet(ItemSchema.MimeContent));
byte[] content = msg.MimeContent.Content;
}
}
}
}
}
}
You have to load the attachment with the flag to load MimeContent:
{
if (attachment is ItemAttachment ia)
{
ia.Load(ItemSchema.MimeContent);
}
}
Related
Hello I recently got into development around EWS. One of the issue came up to me is that a client ask me to import emails into database and he wants to detect the duplicate based on InternetMessageID this way he doesn't have to import the duplicate emails and my code came up to this point.
private static string GetInternetMessageID(Microsoft.Exchange.WebServices.Data.Item email)
{
EmailMessage emailMsg = email as EmailMessage;
string returnId = string.Empty;
if ((emailMsg != null)) {
try {
emailMsg.Load();
//loads additional info, without calling this ToRecipients (and more) is empty
} catch (ArgumentException ex) {
//retry
email.Load();
}
returnId = emailMsg.InternetMessageId;
} else {
//what to do?
}
return returnId;
}
I can handle regular emails, but for special exchange objects such as contact, Calendar, Posts etc it does not work because it could not cast it to an EmailMessage object.
And I know you can extract the internetMessageId from those objects. Because the client used to have another software that extract this ID for them, maybe the property is not called internetMessageID, I think I probally have to extract it from the internetMessageHeader. However when ever I try to get it from the item object it just throws me an error. How do I get the internet messageID from these "Special" exchange items?
PS i am aware of item.id.UniqueID however that is not what I want as this id changes if I move items from folder to another folder in exchange
Only objects that have been sent via the Transport service will have an InternetMessageId so things like Contacts and Tasks because they aren't messages and have never been routed via the Transport service will never have an Internet MessageId. You probably want to look at using a few properties to do this InternetMessageId can be useful for messages PidTagSearchKey https://msdn.microsoft.com/en-us/library/office/cc815908.aspx is one that can be used (if you good this there are various examples of using this property).
If your going to use it in Code don't use the method your using to load the property on each item this is very inefficient as it will make a separate call for each object. Because these I'd's are under 256 Kb just retrieve then when using FindItems. eg
ExtendedPropertyDefinition PidTagSearchKey = new ExtendedPropertyDefinition(0x300B, MapiPropertyType.Binary);
ExtendedPropertyDefinition PidTagInternetMessageId = new ExtendedPropertyDefinition(0x1035, MapiPropertyType.String);
PropertySet psPropSet = new PropertySet(BasePropertySet.IdOnly);
psPropSet.Add(PidTagSearchKey);
psPropSet.Add(PidTagInternetMessageId);
ItemView ItemVeiwSet = new ItemView(1000);
ItemVeiwSet.PropertySet = psPropSet;
FindItemsResults<Item> fiRess = null;
do
{
fiRess = service.FindItems(WellKnownFolderName.Inbox, ItemVeiwSet);
foreach (Item itItem in fiRess)
{
Object SearchKeyVal = null;
if (itItem.TryGetProperty(PidTagSearchKey, out SearchKeyVal))
{
Console.WriteLine(BitConverter.ToString((Byte[])SearchKeyVal));
}
Object InternetMessageIdVal = null;
if (itItem.TryGetProperty(PidTagInternetMessageId, out InternetMessageIdVal))
{
Console.WriteLine(InternetMessageIdVal);
}
}
ItemVeiwSet.Offset += fiRess.Items.Count;
} while (fiRess.MoreAvailable);
If you need larger properties like the Body using the LoadPropertiesForItems Method https://blogs.msdn.microsoft.com/exchangedev/2010/03/16/loading-properties-for-multiple-items-with-one-call-to-exchange-web-services/
I am trying to get unread emails and then mark them as read.
I am getting this exception when I run the code:
ServiceObjectPropertyException was unhandeld:
An unhandled exception of type
'Microsoft.Exchange.WebServices.Data.ServiceObjectPropertyException'
occurred in Microsoft.Exchange.WebServices.dll
This error occurred when I try to map my Exchange mail object to my business model object.
This is the Map method:
class MailMapper
{
public static PhishingMail Map(EmailMessage OutlookMail)
{
//Map Exchange email object op Business model email object
PhishingMail readMail = new PhishingMail();
readMail.Subject = OutlookMail.Subject;
return readMail;
}
}
This is the code where it should mark emails as read.
public List<PhishingMail> GetEmails()
{
phishingMailList = new List<PhishingMail>();
FolderId InboxId = new FolderId(WellKnownFolderName.Inbox, "A*******m#i*****nl");
FindItemsResults<Item> findResults = service.FindItems(InboxId, new ItemView(100));
foreach (Item phishingmail in findResults.Items)
{
((EmailMessage)phishingmail).Load(new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.IsRead));
if (!((EmailMessage)phishingmail).IsRead)
{
((EmailMessage)phishingmail).IsRead = true;
((EmailMessage)phishingmail)
.Update(ConflictResolutionMode.AutoResolve);
}
PhishingMail mail = MailMapper.Map((EmailMessage)phishingmail);
phishingMailList.Add(new PhishingMail());
/// Console.WriteLine(mail.Subject);
}
return phishingMailList;
}
What am I doing wrong? What is wrong with map method?
When you load an item, you speficy the properties to load.
item.Load(new PropertySet(PropertySet.FirstClassProperties));
If you need more properties after loading, you can do the following:
Service.LoadPropertiesForItems(items, PropertySet.FirstClassProperties);
I think the issue is that when you Load the email you're only asking for the ID and IsRead properties. Since your map routine fetches the Subject property, you would need to add that to the Load as well.
I'm trying to use the attachments included in calendar items pulled progmatically.
I have a list of chosen calendar subject lines from a previous dialog box, and while the subject is transferring properly, the body isn't working well (another question altogether) but the attachments aren't working whatsoever.
Here's my foreach loop where the attachments are being placed into an Attachments array for use later:
string[] subjects = new string[dialog.chosen.Count];
string[] bodies = new string[dialog.chosen.Count];
Attachments[] attach = new Attachments[dialog.chosen.Count];
foreach (Outlook.AppointmentItem appt in rangeAppts)
{
foreach (string text in dialog.chosen)
{
if (text == appt.Subject)
{
subjects[i] = appt.Subject;
bodies[i] = Convert.ToString(appt.Body);
attach[i] = appt.Attachments;
i = i + 1;
}
}
}
And then here's where I actually call the method:
sendEmailTemplate(bodies[i], subject, to, "", attachment: attach[i]);
And then the method itself:
public void sendEmailTemplate(string body, string subject, string to, string cc , Attachments attachment = null)
{
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook._MailItem oMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
oMailItem.HTMLBody = body;
oMailItem.Subject = subject;
try
{
oMailItem.Attachments.Add(attachment);
}
catch {}
oMailItem.To = to;
oMailItem.CC = cc;
oMailItem.Display(false);
oMailItem.Application.ActiveInspector().WindowState = Microsoft.Office.Interop.Outlook.OlWindowState.olNormalWindow;
}
I've tried several things, however when I actually go to send the e-mail, I end up getting:
Exception: Member not found. HRESULT: 0x80020003
And then I haven't been able to get anything else to work. The try/catch loop on the method is to prevent the above exception as I was getting that exception regardless of whether or not an attachment was present, and now attachments just aren't being added.
I'm using Interop that comes with Office along with C#. Winforms if that makes a difference.
MailItem.Attachments takes either a string (fully qualified file name), or another Outlook item (MailItem, ContactItem, etc.).
You cannot pass Attachments object as an argument. If you need to copy the attachments, loop through all attachments in the Attachments collection, call Attachment.SaveAsFile for each attachment, pass the file name to MailItem.Attachments.Add, delete thee temporary file.
I have a need to find a particular email on a Google IMAP server and then save the attachments from the email. I think I have it all figured out except for the part of determining what the file name is of the attachment.
Below is my code so far, I am hoping that someone can point me in the right direction to determine the file name.
I have Googled and SO'd but have not been able to find something using the attachment approach.
internal class MailKitHelper
{
private void SaveAttachementsForMessage(string aMessageId)
{
ImapClient imapClient = new ImapClient();
imapClient.Connect("imap.google.com", 993, SecureSocketOptions.Auto);
imapClient.Authenticate("xxxx", "xxxx");
HeaderSearchQuery searchCondition = SearchQuery.HeaderContains("Message-Id", aMessageId);
imapClient.Inbox.Open(FolderAccess.ReadOnly);
IList<UniqueId> ids = imapClient.Inbox.Search(searchCondition);
foreach (UniqueId uniqueId in ids)
{
MimeMessage message = imapClient.Inbox.GetMessage(uniqueId);
foreach (MimeEntity attachment in message.Attachments)
{
attachment.WriteTo("WhatIsTheFileName"); //How do I determine the file name
}
}
}
}
And the winner is.....
attachment.ContentDisposition.FileName
Is there a way to remove HTML tags from Item which is from ItemAttachment?
I can get only text from Item. But not from Item which is from ItemAttachment.
Here is my code:
foreach (ItemAttachment itemAttach in item.Attachments.OfType<ItemAttachment>())
{
Console.WriteLine(itemAttach.Name);
itemAttach.Load();
PropertySet propSet = new PropertySet();
propSet.RequestedBodyType = BodyType.Text;
propSet.BasePropertySet = BasePropertySet.FirstClassProperties;
itemAttach.Item.Load(propSet);
Console.WriteLine(itemAttach.Item.Body.Text);
}
It will get this exception
This operation isn't supported on attachments
I tried binding to the exchange service with item ID.
It also gives me some exception!
Please give some advice on how I can do.
Jin,
The exception you are getting has to do with the property set you are creating. I don't see your code for getting the items so I can't determine the exact cause. I was able to get the following code to work on my machine. You should be able to modify it for your needs.
// Return the first ten items.
ItemView view = new ItemView(10);
// Set the query string to only find emails with attachments.
string querystring = "HasAttachments:true Kind:email";
// Find the items in the Inbox.
FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, querystring, view);
// Loop through the results.
foreach (EmailMessage email in results)
{
// Load the email message with the attachments
email.Load(new PropertySet(EmailMessageSchema.Attachments));
// Loop through the attachments.
foreach (Attachment attachment in email.Attachments)
{
// Only process item attachments.
if (attachment is ItemAttachment)
{
ItemAttachment itemAttachment = attachment as ItemAttachment;
// Load the attachment.
itemAttachment.Load(new PropertySet(EmailMessageSchema.TextBody));
// Output the body.
Console.WriteLine(itemAttachment.Item.TextBody);
}
}
For each email that had an item attachment I was able to see the body of the item with the HTML tags removed.
I hope this helps. If this solves your problem, please mark this post as answered.
Thanks,
--- Bob ---