I'm connecting fine to the Exchange Server and I'm getting all the unread mails but a new message doesn't want to send
Message Code
var newHTML = html.HTMLCode.Replace("{House}", house.Number)
.Replace("{Token}", token.Number)
.Replace("{contactPerson}",
string.Format("<a href=mailto:{0}>{1}</a>",
contactPerson, contactPerson));
LogError.WriteToFile("Has House and token");
//Send mail with token to user
EmailMessage message = new EmailMessage(emailService);
message.ToRecipients.Add(email.From.Address);
message.Subject = string.Format("Electricity token for: {0}", house.Number);
message.Body = new MessageBody(html.HTMLCode);
LogError.WriteToFile("Trying to send");
message.Send();
I have a try catch around this so in the log file I get "Trying to send" but then a error occurs that reads as
"EmailAddress or ItemId must be included in the request."
From examples seen, the way I construct my message seems sufficient but clearly isn't
This is how I got all my unread emails
SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
ItemView view = new ItemView(int.MaxValue);
FindItemsResults<Item> findResults = emailService.FindItems(WellKnownFolderName.Inbox, sf, view);
foreach (EmailMessage email in findResults)
{}
Note that I got them as a "EmailMessage"
But were never able to get the senders email address so that's why it didn't want to send.
and then I found this article: FindItem returns only the first 512 bytes of any streamable property
So then I went to go get that specific email like this. Note that I now get the "Item" from the findResults and with that "Item" I do the following.
SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
ItemView view = new ItemView(int.MaxValue);
FindItemsResults<Item> findResults = emailService.FindItems(WellKnownFolderName.Inbox, sf, view);
foreach (Item item in findResults)
{
//Get the email message
EmailMessage email = EmailMessage.Bind(emailService, item.Id,
new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments));
if (email != null)
{}
}
Now I could finally send emails
Related
Is it possible to assign EmailMessage specific GUID/ID, which will be later used for search?
var email = new EmailMessage(_service);
email.ExternalGuid = /*Guid or Identifier*/;
email.Send();
And later I should be able to use it to find if this mail is present:
var isExist = _service.IsExistByExternalGuid(/*Guid or Identifier*/);
Why don't you use the InternetMessageid eg Internet Message ID FROM EWS Managed API Send Email c# this Id will then appear in any tracking logs associated with the Message and you can search for the message at a later date using a SearchFilter eg
ItemView ivew = new ItemView(3);
service.TraceEnabled = true;
ExtendedPropertyDefinition PidTagInternetMessageId = new ExtendedPropertyDefinition(4149, MapiPropertyType.String);
SearchFilter sf = new SearchFilter.IsEqualTo(PidTagInternetMessageId, MessageID);
FindItemsResults<Item> iCol = service.FindItems(WellKnownFolderName.Inbox, sf, ivew);
foreach (Item item in iCol.Items)
{
Console.WriteLine(item.Subject);
}
I'm having trouble attaching an email to a new email using EWS.
So i have the Microsoft.Exchnage.Webservice.Data.Item in my findResults.
If I find an issue in the form data of the email then I want to attach that item to a new email and send it to a supervisor for manual input.
I have tried;
EmailMessage newMessage = new EmailMessage(exchange);
newMessage.Subject = "Failed lead creation";
ItemAttachment attachment = new ItemAttachment("New Lead", message);
I can't seem to create the ItemAttachment as the erro I am getting is "ItemAttachment does not contain a constructor that takes 2 arguments".
How do I create a new message in EWS, attach the current Item to it and send to another recipient?
Thaks
You can't another message directly you need to use the MimeContent of the Original Message and then create an ItemAttachment based on that eg something like
FolderId folderid= new FolderId(WellKnownFolderName.Inbox,"MailboxName");
Folder Inbox = Folder.Bind(service,folderid);
ItemView ivItemView = new ItemView(1) ;
FindItemsResults<Item> fiItems = service.FindItems(Inbox.Id,ivItemView);
if(fiItems.Items.Count == 1){
EmailMessage mail = new EmailMessage(service);
EmailMessage OriginalEmail = (EmailMessage)fiItems.Items[0];
PropertySet psPropset= new PropertySet(BasePropertySet.IdOnly);
psPropset.Add(ItemSchema.MimeContent);
psPropset.Add(ItemSchema.Subject);
OriginalEmail.Load(psPropset);
ItemAttachment Attachment = mail.Attachments.AddItemAttachment<EmailMessage>();
Attachment.Item.MimeContent = OriginalEmail.MimeContent;
ExtendedPropertyDefinition PR_Flags = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
Attachment.Item.SetExtendedProperty(PR_Flags,"1");
Attachment.Name = OriginalEmail.Subject;
mail.Subject = "See the Attached Email";
mail.ToRecipients.Add("glen.scales#domain.com");
mail.SendAndSaveCopy();
Cheers
Glen
Utilizing answer from Glen Scales, if you have message id, code should look like below.
Additional info on PR_Flags extended property can be found on folowing link:
http://systemmanager.ru/windowsmobile_6_5.en/html/45cd0e95-9622-4180-bf85-290c421524f3.htm
PropertySet psPropset = new PropertySet(BasePropertySet.IdOnly);
psPropset.Add(ItemSchema.MimeContent);
psPropset.Add(ItemSchema.Subject);
EmailMessage attachment = EmailMessage.Bind(_exchangeService, new ItemId(ewsItemAsAttachment).UniqueId, psPropset).Result;
ItemAttachment Attachment = message.Attachments.AddItemAttachment<EmailMessage>();
Attachment.Item.MimeContent = attachment.MimeContent;
ExtendedPropertyDefinition PR_Flags = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
Attachment.Item.SetExtendedProperty(PR_Flags, "1");
Attachment.Name = attachment.Subject;
I'm using Exchange web service to read email from Office365. The C# program works fine. I can get EmailMessage in Inbox, and get their subject and message body, etc. However, I could not figure out the way to check whether the message is a bounce back message or not.
Do I have to parse the message body to see whether there are some special sentence, ie. Mail Delivery Failure? If so, is it possible different email servers bounce back emails with different words? i.e some use 'Mail Delivery Failure', some use 'Mail Delivery Not Succeeded'? (just an example, I do not know whether this is true)
Or, the message object has an attribute that can be used for this purpose?
Thanks
*** Just found that the exchange webservice can not see 'Bounce back' messages in INBOX. I'm using below code, all messages can be 'seen' except Bounce Back ones. Do I miss anything to filter te bounce back messages? They are actually in INBOX, unread, and I can see it from Office365 page.
private static void ProcessEmailMessages(SearchFolder searchFolder, Folder folderHistory, Folder folderBounceBack)
{
if (searchFolder == null)
{
return;
}
const Int32 pageSize = 50;
ItemView itemView = new ItemView(pageSize);
PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties);
itempropertyset.RequestedBodyType = BodyType.Text;
itemView.PropertySet = itempropertyset;
PropertySet propertySet = new PropertySet(BasePropertySet.IdOnly, FolderSchema.DisplayName);
folderHistory.Load(propertySet);
folderBounceBack.Load(propertySet);
FindItemsResults<Item> findResults = null;
do
{
findResults = searchFolder.FindItems(itemView);
foreach (Item item in findResults.Items)
{
if (item is EmailMessage)
{
// load body text
item.Load(itempropertyset);
EmailMessage email = item as EmailMessage;
//email.Move(folder.Id);
// check email subject to find the bounced emails
bool subjectContains = Regex.IsMatch(email.Subject, "Mail Delivery Failure", RegexOptions.IgnoreCase);
bool bodyContains = Regex.IsMatch(email.Subject, "Delivery", RegexOptions.IgnoreCase);
if (subjectContains || bodyContains)
{
email.Move(folderBounceBack.Id);
Console.WriteLine("Move the Bounced email: {0}", email.Subject);
ShowMessageInfo(email);
}
else
{
email.Move(folderHistory.Id);
Console.WriteLine(">>> Keep the email: {0}", email.Subject);
}
}
}
itemView.Offset += pageSize;
} while (findResults.MoreAvailable);
}
Check the ItemClass attribute. Messages like that should have a class that contains "REPORT" in it.
I use EWS in an O365 environment to help process bounce backs and use the following code to just get the NDRs from a users' inbox.
var sf = new SearchFilter.IsEqualTo(ItemSchema.ItemClass, "REPORT.IPM.Note.NDR");
var SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, sf);
var view = new ItemView(1000) {PropertySet = new PropertySet(BasePropertySet.IdOnly)};
var findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);
Hope it helps.
I Am using a SearchFilter collection to restrict what emails are returned by a request to an Exchange 2010 mailbox using EWS.
I have no problem connecting to the service, and opening the mailbox.
The problem is that my searchFilter is being ignored, and all the emails are being returned by the request to EWS.
Here is my code:
static void Main(string[] args)
{
ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
//creates an object that will represent the desired mailbox
Mailbox mb = new Mailbox(#"bbtest#bocuk.local");
// Find all items where the body contains "move reports".
//string qstring = "Body:\"move reports\"";
// Identify the item properties to return.
//view.PropertySet = new PropertySet(BasePropertySet.IdOnly,
//ItemSchema.Subject);
//creates a folder object that will point to inbox fold
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);
//this will bind the mailbox you're looking for using your service instance
Folder inbox = Folder.Bind(service, fid);
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
searchFilterCollection.Add(new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false)));
searchFilterCollection.Add(new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments, true)));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "FATS")));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Assignment")));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Sandbox: Assignment")));
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());
ItemView view = new ItemView(100);
string sAttachmentPath = "C:\\Dev\\EWSHelloWorld\\attachments\\";
// Find the first email message in the Inbox that has attachments. This results in a FindItem operation call to EWS.
FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);
foreach (EmailMessage email in results)
// looping through all the emails
{
System.Diagnostics.Debug.Write("Found attachemnt on msg with subject: " + email.Subject);
.... code removed for brevity!
So, according to my understanding of the searchFilter, only unread emails with attachments should be returned and they should not have FATS or Sandbox: Assignment as the subject.
But that's not working, the request to EWS just returnes all the emails.
What am I doing wrong please?
Philip,
I started debugging your code and got a little confused as to what you are trying to get returned. In your code you are using an OR operator when you create the search filter, but in your text you describe the required output as
only unread emails with attachments should be returned and they should not have FATS or Sandbox: Assignment as the subject.
I took the parameters that you were trying to filter on and came up with the following filter that combines all the filters with a logical AND that works on my machine:
SearchFilter.SearchFilterCollection searchFilterCollection = new SearchFilter.SearchFilterCollection(LogicalOperator.And);
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments, true));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, "FATS")));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, "Assignment")));
searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, "Sandbox: Assignment")));
FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, searchFilterCollection, new ItemView(100));
A few things to note:
I'm not using a generic list. I created a SearchFilterCollection with an AND logical operator and then added the filters to that collection.
My testing was with my local mailbox so I didn't bind to a different mailbox and get the folder id for the inbox. That shouldn't make a difference in your code though.
I used the EmailMessageSchema.Subject rather than ItemSchema.Subject. I also used my own string values in my testing but I placed your string values in the example.
When I ran my tests, if first filtered on unread messages with an attachment. I then verified that the returned results filtered further when I added the subject filter.
I hope this helps.
Im using EWS Managed API to communicate between my c# project and our Exchange 2010 server.
I use this code to get all mails in the inbox from now and three days back.
var ews = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
ews.Credentials = new NetworkCredential(usr, psw, dmn);
ews.AutodiscoverUrl(url);
PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties);
itempropertyset.RequestedBodyType = BodyType.Text;
ItemView view = new ItemView(int.MaxValue);
FindItemsResults<Item> findResults;
view.PropertySet = itempropertyset;
do
{
findResults = ews.FindItems(WellKnownFolderName.Inbox, view);
foreach (Item item in findResults.Items)
{
if (item.DateTimeCreated < DateTime.Now.AddDays(-3)) continue;
item.Load(itempropertyset);
var message = EmailMessage.Bind(ews, item.Id,
new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments));
string to = message.ToRecipients[0].Address.ToLower();
string body = item.Body;
}
view.Offset += findResults.TotalCount;
} while (findResults.MoreAvailable);
Now the problem. I want to improve this line if (item.DateTimeCreated < DateTime.Now.AddDays(-3)) continue; because when i use this, the api gets all the messages from inbox and just continue if its older then three days. I want specify this filter earlier in the code, so the api dont have to handle all messages.
If I understand the problem correctly, this should work. You can see all search filters available here: EWS Search Filters
ItemView view = new ItemView(int.MaxValue);
FindItemsResults<Item> findResults;
view.PropertySet = itempropertyset;
SearchFilter searchFilter =
new SearchFilter.IsGreaterThan(ItemSchema.DateTimeReceived, DateTime.Now.AddDays(-3));
findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);