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