Imran Khunt
asked on
Tried reading the outlook email attachment in c#. Getting error as Microsoft.Exchange.WebServices.Data.ServiceResponseException: Id is malformed.
Hello everyone,
I am trying to read the email attachment for the outlook email in asp.net c# but i am new to this can anyone help me, i have the below code which select the new email with specific title.
String MailboxToAccess = "myemailaddress";
ExchangeService service = new Microsoft.Exchange.WebServ ices.Data. ExchangeSe rvice(Exch angeVersio n.Exchange 2007_SP1);
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
searchFilterCollection.Add (new SearchFilter.ContainsSubst ring(ItemS chema.Subj ect, "EDI"));
searchFilterCollection.Add (new SearchFilter.IsEqualTo(Ema ilMessageS chema.IsRe ad, false));
SearchFilter searchFilter = new SearchFilter.SearchFilterC ollection( LogicalOpe rator.Or, searchFilterCollection.ToA rray());
service.Credentials = new NetworkCredential("myemail ", "pwd");
service.AutodiscoverUrl(Ma ilboxToAcc ess, adAutoDiscoCallBack);
FolderId FolderToAccess = new FolderId(WellKnownFolderNa me.Inbox, MailboxToAccess);
ItemView ivItemView = new ItemView(50);
FindItemsResults<Item> FindItemResults = service.FindItems(FolderTo Access, searchFilter, ivItemView);
PropertySet ItemPropertySet = new PropertySet(BasePropertySe t.IdOnly);
ItemPropertySet.Add(ItemSc hema.Body) ;
ItemPropertySet.RequestedB odyType = BodyType.Text;
///error here??????
EmailMessage message = EmailMessage.Bind(service, new ItemId("AAMkA"), new PropertySet(BasePropertySe t.IdOnly, ItemSchema.Attachments));
// Iterate through the attachments collection and load each attachment.
foreach (Attachment attachment in message.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
// Load the file attachment into memory and print out its file name.
fileAttachment.Load();
Console.WriteLine("Attachm ent name: " + fileAttachment.Name);
// Load attachment contents into a file.
fileAttachment.Load("C:\\t emp\\" + fileAttachment.Name);
// Stream attachment contents into a file.
FileStream theStream = new FileStream("C:\\temp\\Stre am_" + fileAttachment.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
fileAttachment.Load(theStr eam);
theStream.Close();
theStream.Dispose();
}
else // Attachment is an item attachment.
{
// Load attachment into memory and write out the subject.
ItemAttachment itemAttachment = attachment as ItemAttachment;
itemAttachment.Load();
Console.WriteLine("Subject : " + itemAttachment.Item.Subjec t);
}
// }
}
}
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;
}
I am trying to read the email attachment for the outlook email in asp.net c# but i am new to this can anyone help me, i have the below code which select the new email with specific title.
String MailboxToAccess = "myemailaddress";
ExchangeService service = new Microsoft.Exchange.WebServ
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
searchFilterCollection.Add
searchFilterCollection.Add
SearchFilter searchFilter = new SearchFilter.SearchFilterC
service.Credentials = new NetworkCredential("myemail
service.AutodiscoverUrl(Ma
FolderId FolderToAccess = new FolderId(WellKnownFolderNa
ItemView ivItemView = new ItemView(50);
FindItemsResults<Item> FindItemResults = service.FindItems(FolderTo
PropertySet ItemPropertySet = new PropertySet(BasePropertySe
ItemPropertySet.Add(ItemSc
ItemPropertySet.RequestedB
///error here??????
EmailMessage message = EmailMessage.Bind(service,
// Iterate through the attachments collection and load each attachment.
foreach (Attachment attachment in message.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
// Load the file attachment into memory and print out its file name.
fileAttachment.Load();
Console.WriteLine("Attachm
// Load attachment contents into a file.
fileAttachment.Load("C:\\t
// Stream attachment contents into a file.
FileStream theStream = new FileStream("C:\\temp\\Stre
fileAttachment.Load(theStr
theStream.Close();
theStream.Dispose();
}
else // Attachment is an item attachment.
{
// Load attachment into memory and write out the subject.
ItemAttachment itemAttachment = attachment as ItemAttachment;
itemAttachment.Load();
Console.WriteLine("Subject
}
// }
}
}
internal static bool adAutoDiscoCallBack(string
{
// 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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I have recommended this question be closed as follows:
Accept: dsuyemoto (https:#a42048896)
If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.
frankhelk
Experts-Exchange Cleanup Volunteer