To extract vcf files, is it up to date or you’ve got newer methods implemented?
Dim MyStorage As Email.Storage.Pst.PersonalStorage = Email.Storage.Pst.PersonalStorage.FromFile(OFD.FileName, AsposeLoadOptionsStorage)
Dim MyFolderInfo As Email.Storage.Pst.FolderInfo = MyStorage.RootFolder.GetSubFolder(“Contacts”)
For Each MyMessageInfo As Email.Storage.Pst.MessageInfo In MyFolderInfo.GetContents()
Try
Dim MyContact As Email.Mapi.MapiContact = DirectCast(MyStorage.ExtractMessage(MyMessageInfo).ToMapiMessageItem, Email.Mapi.MapiContact)
MyContact.Save(FBD.SelectedPath + "" + MyContact.NameInfo.DisplayName + “.vcf”, Email.Mapi.ContactSaveFormat.VCard)
Catch Exception As Exception
MsgBox(Exception.Message)
End Try
Next
MyStorage.Dispose()
It throws some exceptions during the loop:
Unable to cast object of type ‘Aspose.Email.Mapi.MapiDistributionList’ to type ‘Aspose.Email.Mapi.MapiContact’.
Is it related to the lack of DistList enum as I suggested here? StandardIpmFolder
I know I can use TryCast and in the next line check if MyContact IsNot Nothing, but I will still get ArgumentNull and NullReference exceptions! I’m attaching my pst so you can test against the provided code.
As I inspected MyStorage after last line of code: MyStorage.Dispose
Even after disposing, MyStorage still holds the values of these 2 properties: RootFolder and Store!
.Dispose is just wiping out CanWrite, Format and IsUnicode, but not the above 2 mentioned ones!
So related to number 2, if I run MyStorage.Dispose again and again, it will never throw a null ref exception, since those 2 properties are never wiped out
The contacts folder can contain both contacts and distribution lists. Therefore, to save contacts only, you must check whether the item is a contact. Please check the following code example.
var messageInfoCollection = folderInfo.GetContents();
foreach (MessageInfo messageInfo in messageInfoCollection)
{
var msgItem = personalStorage.ExtractMessage(messageInfo).ToMapiMessageItem();
var contact = msgItem as MapiContact;
if (contact != null)
{
contact.Save(contact.NameInfo.DisplayName + ".vcf",
ContactSaveFormat.VCard);
}
}
It is to inform you that the issue which you are facing is actually not a bug in Aspose.Email. So, we have closed this issue (EMAILNET-40752) as ‘Not a Bug’.
If the pst.RootFolder.GetSubFolder("Contacts") method returns null, it means that the root folder doesn’t contain a subfolder named “Contacts”.
If you want to get a folder that contains contacts, use the pst.GetPredefinedFolder(StandardIpmFolder.Contacts) method. For this method, null also means there is no folder.