Can anyone tell me how to get an email body, receipt, sender, CC info using Exchange Web Service API? I only know how to get subject.
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
service.Credentials = new NetworkCredential("user", "password", "domain");
service.Url = new Uri("https://208.243.49.20/ews/exchange.asmx");
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
FindItemsResults<Item> findResults = service.FindItems(
WellKnownFolderName.Inbox,
new ItemView(10));
foreach (Item item in findResults.Items)
{
div_email.InnerHtml += item.Subject+"<br />";
}
My development environment is asp.net c# Exchange-server 2010
Thank you.
Since the original question specifically asked for "email body, receipt, sender and CC info," I thought I would address those. I assume "receipt" is recipient info, and not the "notify sender" feature of email that no one uses. CC looks like it is handled the same way as recipients.
I liked Henning's answer to reduce the function to two calls, but had a little bit of difficulty figuring out how to handle a PropertySet. Google search was not immediately clear on this, and I ended up using someone else's tutorial:
// Simplified mail item
public class MailItem
{
public string From;
public string[] Recipients;
public string Subject;
public string Body;
}
public MailItem[] GetUnreadMailFromInbox()
{
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(128));
ServiceResponseCollection<GetItemResponse> items =
service.BindToItems(findResults.Select(item => item.Id), new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.From, EmailMessageSchema.ToRecipients));
return items.Select(item => {
return new MailItem() {
From = ((Microsoft.Exchange.WebServices.Data.EmailAddress)item.Item[EmailMessageSchema.From]).Address,
Recipients = ((Microsoft.Exchange.WebServices.Data.EmailAddressCollection)item.Item[EmailMessageSchema.ToRecipients]).Select(recipient => recipient.Address).ToArray(),
Subject = item.Item.Subject,
Body = item.Item.Body.ToString(),
};
}).ToArray();
}
Using FindItems will only get you so far, because it does only return the first 255 bytes of a body. What you should do is a combination of FindItem to request the ids of the mails and issue one or more GetItem calls to get the properties you are interested in.
Instead of using the ExtendedProperties, you could also cast to EmailMessage and read the property you want directly. For example the sender address:
((Microsoft.Exchange.WebServices.Data.EmailMessage)(item)).From.Address;
here you will find the solution.
http://blogs.msdn.com/b/akashb/archive/2010/03/05/how-to-build-a-complex-search-using-searchfilter-and-searchfiltercollection-in-ews-managed-api-1-0.aspx
// Send the request to search the Inbox and get the results.
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, FinalsearchFilter, view);
// Process each item.
if (findResults.Items.Count > 0)
{
foreach (Item myItem in findResults.Items)
{
if (myItem is EmailMessage)
{
Console.WriteLine((myItem as EmailMessage).Subject);
}
if (myItem.ExtendedProperties.Count > 0)
{
// Display the extended property's name and property.
foreach (ExtendedProperty extendedProperty in myItem.ExtendedProperties)
{
Console.WriteLine(" Extended Property Name: " + extendedProperty.PropertyDefinition.Name);
Console.WriteLine(" Extended Property Value: " + extendedProperty.Value);
}
}
}
}
else
{
Console.WriteLine("No Items Found!");
}
}
Related
I have a service running as a scheduled job on a machine. It's running under a service account that doesn't have it's own mailbox. We would like it to send emails from the team's shared inbox.
Using impersonation here is what I tried.
var service = new ExchangeService
{
TraceEnabled = true,
ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, Resources.EmailUsername)
};
service.AutodiscoverUrl(Resources.EmailUsername, RedirectionUrlValidationCallback);
var email = new EmailMessage(service);
if (!string.IsNullOrWhiteSpace(recipients))
{
foreach (var recipient in recipients.Split(','))
{
email.ToRecipients.Add(recipient.Trim());
}
}
email.Subject = subject;
email.Body = new MessageBody(BodyType.HTML, body);
if (attachmentName != null && attachment != null)
{
email.Attachments.AddFileAttachment(attachmentName, attachment);
}
email.Send();
It fails and I get an exception saying:
When making a request as an account that does not have a mailbox, you
must specify the mailbox primary smtp address for any distinguished
folder Ids.
The TraceEnabled part led me to notice MessageDisposition=SaveOnly was set in the xml. I looked up MessageDisposition and figured I wanted SendOnly.
After much searching I ended up here: https://github.com/OfficeDev/ews-managed-api/blob/master/Core/ServiceObjects/Items/EmailMessage.cs
Which shows:
public void Send()
{
this.InternalSend(null, MessageDisposition.SendOnly);
}
Well that looks like what I wanted in the first place... But then:
private void InternalSend(FolderId parentFolderId, MessageDisposition messageDisposition)
{
this.ThrowIfThisIsAttachment();
if (this.IsNew)
{
if ((this.Attachments.Count == 0) || (messageDisposition == MessageDisposition.SaveOnly))
{
this.InternalCreate(
parentFolderId,
messageDisposition,
null);
}
else
{
// If the message has attachments, save as a draft (and add attachments) before sending.
this.InternalCreate(
null, // null means use the Drafts folder in the mailbox of the authenticated user.
MessageDisposition.SaveOnly,
null);
this.Service.SendItem(this, parentFolderId);
}
}
...
The two comments are the most enlightening parts. The attachment is being saved to the drafts folder of the user running the process.
To get around this, the message must already be saved when we call Send. Let's make sure it is saved in the mailbox we know exists. So we remove the impersonation, add a step to save it, and modify the From field. Then we can safely send the message, and it will remove itself from the draft folder.
var service = new ExchangeService
{
TraceEnabled = true
};
service.AutodiscoverUrl(Resources.EmailUsername, RedirectionUrlValidationCallback);
var email = new EmailMessage(service);
if (!string.IsNullOrWhiteSpace(recipients))
{
foreach (var recipient in recipients.Split(','))
{
email.ToRecipients.Add(recipient.Trim());
}
}
email.Subject = subject;
email.Body = new MessageBody(BodyType.HTML, body);
if (attachmentName != null && attachment != null)
{
email.Attachments.AddFileAttachment(attachmentName, attachment);
}
var folderId = new FolderId(WellKnownFolderName.SentItems, Resources.EmailUsername);
email.Save(folderId);
email.From = Resources.EmailUsername;
email.Send();
I get this error because the FindItemsResult isn't compatible with exchange version I'm using which is 2013.
Exchange Server doesn't support the requested version.
My codes:
SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
FindItemsResults<Item> items = service.FindItems(WellKnownFolderName.Inbox, sf, new ItemView(10));
foreach (Item item in items.Items)
{
PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.TextBody);
EmailMessage email = EmailMessage.Bind(service, item.Id, propSet);
Program.SearchItems(email);
}
I could just change it into Exchange 2010 but I get an error in TextBody since this is only for Exchange 2013 and later versions.
Is there any way to convert the code which can work in Exchange 2013?
You need to show more of the code your using as your question doesn't really make sense. The ItemSchema.TextBody was added in Exchange 2013 so as long you are running Exchange 2013 and you have set the Initial server version correctly it will work (So you are either not running 2013 or you have other issue in the code you haven't show). If your looking for something that will work on both Exchange 2007,2010 and 2013 the I would suggest you use.
String MailboxToAccess = "user#domain.com";
ExchangeService service = new Microsoft.Exchange.WebServices.Data.ExchangeService(ExchangeVersion.Exchange2010_SP1);
SearchFilter sfSearchFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead,false);
service.Credentials = new NetworkCredential("user#domain.com", "password");
service.AutodiscoverUrl(MailboxToAccess, adAutoDiscoCallBack);
FolderId FolderToAccess = new FolderId(WellKnownFolderName.Inbox, MailboxToAccess);
ItemView ivItemView = new ItemView(10);
FindItemsResults<Item> FindItemResults = service.FindItems(FolderToAccess, sfSearchFilter, ivItemView);
PropertySet ItemPropertySet = new PropertySet(BasePropertySet.IdOnly);
ItemPropertySet.Add(ItemSchema.Body);
ItemPropertySet.RequestedBodyType = BodyType.Text;
if (FindItemResults.Items.Count > 0)
{
service.LoadPropertiesForItems(FindItemResults.Items, ItemPropertySet);
}
foreach (Item item in FindItemResults.Items)
{
Console.WriteLine(item.Body.Text);
}
internal static bool adAutoDiscoCallBack(string redirectionUrl)
{
// The default for the validation callback is to reject the URL.
bool result = false;
Uri redirectionUri = new Uri(redirectionUrl);
// Validate the contents of the redirection URL. In this simple validation
// callback, the redirection URL is considered valid if it is using HTTPS
// to encrypt the authentication credentials.
if (redirectionUri.Scheme == "https")
{
result = true;
}
return result;
}
That will return just the Text body and will work on any version of EWS.
Cheers
Glen
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 Microsoft Exchange Services to retrieve contacts from Outlook. I use the following code. It executes without any error. However, I get nothing in Contacts.
public ActionResult Index()
{
ExchangeService _service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
_service.Credentials = new WebCredentials("username", "password");
_service.AutodiscoverUrl("****");
_service.Url = new Uri("https://***/EWS/Exchange.asmx");
foreach (Contact contact in _service.FindItems(WellKnownFolderName.Contacts, new ItemView(int.MaxValue)))
{
// do something
}
return View();
}
How can i get the contacts?
Please help,
Thanks.
I would suggest you clean up your code as there a few reason I can see it failing eg
_service.AutodiscoverUrl("****");
_service.Url = new Uri("https://***/EWS/Exchange.asmx");
use one or the other and for AutoDiscoverURL you may need a callback in here
foreach (Contact contact in _service.FindItems(WellKnownFolderName.Contacts, new ItemView(int.MaxValue)))
First of all a Contacts folder can contain objects other then Contacts so if your code comes across a Distribution list its going to generate an exception. Also using int.MaxValue is a bad idea you should page the Items in groups of 1000 (Throttling on Exchange 2010 will enforce this for you anyway so your code will just fail to get all the Contacts if there are more then 1000). Also does the Mailbox your trying to access belong to the security credentials your using. I would suggest you use something like
String mailboxToAccess = "user#domain.onmicrosoft.com";
ExchangeService _service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
_service.Credentials = new WebCredentials("upn#domain.onmicrosoft.com", "password");
_service.AutodiscoverUrl(mailboxToAccess, redirect => true);
// _service.Url = new Uri("https://***/EWS/Exchange.asmx");
ItemView iv = new ItemView(1000);
FolderId ContactsFolderId = new FolderId(WellKnownFolderName.Contacts,mailboxToAccess);
FindItemsResults<Item> fiResults;
do
{
fiResults = _service.FindItems(ContactsFolderId, iv);
foreach (Item itItem in fiResults.Items)
{
if (itItem is Contact)
{
Contact ContactItem = (Contact)itItem;
Console.WriteLine(ContactItem.Subject);
}
}
iv.Offset += fiResults.Items.Count;
} while (fiResults.MoreAvailable);
You can test EWS itself using the EWSEditor http://ewseditor.codeplex.com/.
I'm currently pulling emails from an exchange inbox like so...
var exchangeService = new ExchangeService(ExchangeVersion.Exchange2007_SP1)
{
Credentials = new NetworkCredential("user", "password", "domain")
};
exchangeService.AutodiscoverUrl("user#domain.com");
var emails = exchangeService.FindItems(WellKnownFolderName.Inbox, new ItemView(5));
foreach (var email in emails)
{
//var senderEmail = email.???
}
The email object doesn't seem to have any property for getting the sender's email address. How do I get that?
Here's some quick source I pulled from a working project example.
Basically, you can get minor details just by casting your result to an EmailMessage. However if you want to get richer details about the sender (display name, etc.) then you have to make a specific, additional bind (Web service request) against the message.
findResults = exchangeService.FindItems(folder.Id, messageFilter, view);
foreach (Item item in findResults)
{
if (item is EmailMessage)
{
EmailMessage message;
if (!toFromDetails)
message = (EmailMessage)item;
else
message = EmailMessage.Bind(exchangeService, item.Id);
As you can see in this code, I have an option to perform the additional bind, because it can take awhile, and I'm often dealing with thousands of results from hundreds of mailboxes. Sometimes the additional time may not be worth it to a particular customer.