cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to get the mail body in HTML and Text simultaneously using EWS

JeevanR
Level 5
Hi All,

I am trying to fetch mail body in html and text format which are supposed to be in a single collection, FYI i am using the Microsoft Exchange Web services VBO, I am able to get html and xml bodies using the following code, 

string sub;
string body_h;
string html_body;
string from_m;
string atchmt;
Message= "";

DataTable table = new DataTable();
DataColumn column;
DataRow row;
//DataView view;
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "Subject";
table.Columns.Add(column);


column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "Sender";
table.Columns.Add(column);

column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName ="Sender Address";
table.Columns.Add(column);

column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "Body";
table.Columns.Add(column);

column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "html body";
table.Columns.Add(column);

column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "Item ID";
table.Columns.Add(column);

column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "Attachment";
table.Columns.Add(column);

column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "DateReceived";
table.Columns.Add(column);

Call3=table;
//ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);


if(UserName=="")
service.AutodiscoverUrl( MailID );
else
{

//service.AutodiscoverUrl( MailID,RedirectionUrlValidationCallback );
//service.Url = new Uri("https://mail1.eyqa.net/ews/exchange.asmx");
service.Url = new Uri(Service_URL);
service.Credentials = new NetworkCredential(UserName, Password );


}

try
{
Mailbox mb = new Mailbox(MailID);

FolderId fid1 = new FolderId(WellKnownFolderName.Inbox, mb);



PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties,ItemSchema.UniqueBody);

//PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties,ItemSchema.TextBody, EmailMessageSchema.Body);

itempropertyset.RequestedBodyType = BodyType.HTML;
//itempropertyset.RequestedBodyType = BodyType.Text;
ItemView itemview = new ItemView(1000);
itemview.PropertySet = itempropertyset;

PropertySet itemproperty = new PropertySet(BasePropertySet.FirstClassProperties,ItemSchema.UniqueBody);
itemproperty.RequestedBodyType = BodyType.Text;
ItemView itemview1 = new ItemView(1000);
itemview1.PropertySet = itemproperty;

//SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));




FindItemsResults<Item> findResults = service.FindItems(fid1,new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead,unread_mails)),new ItemView(1000));

service.LoadPropertiesForItems(findResults, PropertySet.FirstClassProperties);



foreach ( Item item in findResults.Items )
{
EmailMessage mes = (EmailMessage)item;
ItemId itemID = item.Id;

from_m=mes.Sender.Name;
item.Load(itempropertyset);
if (item.HasAttachments)
{
atchmt = "True";
}
else
{
atchmt = "False";
}
//Console.WriteLine(item.Body);

//body_h=item.Body.Text;
//body_h=item.Body.ToString();
sub=item.Subject;


//if( sub=="Fwd: THESIS PRESENTATION")
{
html_body=mes.UniqueBody.Text;
body_h=mes.UniqueBody.Text;
row = table.NewRow();

//row["Subject"] = body_h;

row["Subject"] =sub;
row["Sender"] =from_m;
row["Sender Address"]=mes.From.Address;
row["Body"] =body_h;
row["html body"]=html_body;
row["Item ID"]=itemID;
row["Attachment"]= atchmt;
row["DateReceived"]=mes.DateTimeReceived;
table.Rows.Add(row);

}

}
Call3=table;
Message = "Success";
Success= true;
}
catch (Exception e)
{
Call3=Coll;
Message = e.ToString();
Success= false;
}

finally
{
table.Dispose();
}

can someone please help me with this thanks in advance.
Jeevan Rangaraju - AD01 Certified RPA Professional Associate Consultant Ernst & Young LLP Bangalore, India Email - jjeevan152@gmail.com
6 REPLIES 6

NicholasZejdlik
Level 9
I'd suggest wrapping your code into a code tag to make your code easier to read - plain text formatting is not kind. I would also trim down the code to what is relevant for the email body, since it would make it easier for yourself and others to go through.

In this case, I believe the issue stems from the properties that are being loaded in item.Load(itempropertyset);. itempropertyset has the requested body type set to BodyType.HTML, so the message body will be HTML and not plain-text. In order to get both, you would need to load the item a second time with a requested body type set to BodyType.Text. Alternatively, there is probably a way to convert HTML text into plain text, probably with some other library, but personally I would just reload the item. For performance, the property set for the second load could specify only the body, to avoid unnecessary overhead of reloading the rest of the item properties.

JeevanR
Level 5
@Nicholas Zejdlik can you please suggest me where i can add the code to load the body in text, i am trying to load but i am still getting the body in html and xml.

thanks​
Jeevan Rangaraju - AD01 Certified RPA Professional Associate Consultant Ernst & Young LLP Bangalore, India Email - jjeevan152@gmail.com

NicholasZejdlik
Level 9
Changing this line: itempropertyset.RequestedBodyType = BodyType.HTML; to itempropertyset.RequestedBodyType = BodyType.Text; should result in getting the body in plain text. I do not believe you can receive the HTML and plain-text simultaneously, which is why you would want to make a second call to item.Load(). Note that because you are referring to the unique body and not the body, you may need to change that to itempropertyset.RequestedUniqueBodyType = BodyType.Text;

JeevanR
Level 5
@Nicholas Zejdlik, the second call technique worked, thanks a lot....

I need one more help, can we take a set of mail ids in collection and use it as a filter criteria which makes fetching mails faster, in my project i receive about 500 mails a day which is taking a lot of time so need some help on this.
Jeevan Rangaraju - AD01 Certified RPA Professional Associate Consultant Ernst & Young LLP Bangalore, India Email - jjeevan152@gmail.com

NicholasZejdlik
Level 9
Quick addendum, after looking through the Microsoft Docs again (it has been a while since I've used EWS), there is a TextBody property on the EmailMessage class; have you tried using that already? If that comes up as just text, it could save you the second call to Item.Load().

In regards to performance, it's a bit problematic since EWS uses blocking calls, there are no asynchronous functions. I think EWS may be retired at this point in favor of Microsoft Graph, at least for O365. Anyway, the problem without an asynchronous load method is that every call to Item.Load() is going to end up blocking a thread. Using tasks would not work well; I think the only way to speed things up would be to use threads. I do not know if EWS is thread-safe, but it's worth a shot. It would add some complexity, but could speed things up considerably.

JeevanR
Level 5
@Nicholas Zejdlik, thanks for the help, I have reduced the load by picking the mails from specific senders and now its working properly....thanks a lot....​
Jeevan Rangaraju - AD01 Certified RPA Professional Associate Consultant Ernst & Young LLP Bangalore, India Email - jjeevan152@gmail.com