Get Sender email address from PST

Hi,

How to get Sender email address from PST.
I do not see property in MessageInfoCollection:

MessageInfoCollection messageInfoCollection = folderInfo.getContents();
for (int i = 0; i < messageInfoCollection.size(); i++)
{
MessageInfo messageInfo = (MessageInfo) messageInfoCollection.get(i);
System.out.println("Subject: " + messageInfo.getSubject());
System.out.println("Sender: " + messageInfo.getSenderRepresentativeName());
System.out.println("To: " + messageInfo.getDisplayTo());
System.out.println("CC: " + messageInfo.getDisplayCC());
System.out.println("EntryID: " + messageInfo.getEntryIdString());
}

Please help.

Regards,
pamanager

Hi Pamanager,
Thank you for writing to Aspose support team.

Sender email address cannot be retrieved directly from the MessageInfo object. However, ExtractProperty() function is provided to retrieve selected MapiProperty from the PST without extracting the whole content to improve the performance. You may please try following sample code and retrieve any property as per your requirement.

PersonalStorage pst = PersonalStorage.FromFile("test.pst");
FolderInfo folderInfo = pst.GetPredefinedFolder(StandardIpmFolder.Inbox);
MessageInfoCollection messageInfoCollection = folderInfo.GetContents();

foreach(MessageInfo info in messageInfoCollection)
{
    MapiProperty prop = pst.ExtractProperty(info.EntryId, MapiPropertyTag.PR_SENDER_EMAIL_ADDRESS);
}

Hi Kashif,



I was running the following code:



PersonalStorage pst = PersonalStorage.FromFile(@“C:\zzz\backup.pst”);



// FolderInfo folderInfo = pst.GetPredefinedFolder(StandardIpmFolder.Inbox);

FolderInfo folderInfo = pst.RootFolder.GetSubFolder(“Inbox”);



MessageInfoCollection messageInfoCollection = folderInfo.GetContents();

int iMessageCount = messageInfoCollection.Count; // I have 1264 messages in Inbox



foreach (MessageInfo info in messageInfoCollection)

{



MapiProperty prop = pst.ExtractProperty(info.EntryId, MapiPropertyTag.PR_SENDER_EMAIL_ADDRESS);

if (prop != null)

{

string ea = prop.Name;

}



}





And prop is always null.

Why?



Thank you.

Pamanager

Hi Pamanager,


This property is just an example and retrieval depends on the presence of this property in the object. This example is given to just show you that you can directly retrieve the MapiProperty without extracting the complete mail.

It should be noted that sender email address is not available in the MessageInfo. However depending upon the availability of some MapiProperty, you may use above code example to retrieve it. Therefore, it is not a bug in Aspose.Email, and this value is null because this property may be missing in the collection.

Please feel free to write us back if you have any other query in this regard.

Hi Kashif,

To get sender address I am using code:
foreach (MessageInfo messageInfo in messageInfoCollection)
{
MapiMessage message = pst.ExtractMessage(messageInfo);
string senderSmptAddress = message.SenderSmtpAddress;
}

But it is slow and some addresses is empty. Why?
Is it some another way to get sender address from PST?

Hi Pamanager,


Could you please share your PST file with us? We will extract messages from this PST and observe the properties. It will help us to analyze the problem and provide assistance accordingly.

Hi Kashif,

I cannot send my company PST file.

I would be very appreciate if you send me some other version of code sample to retrieve sender email address from PST.

Thank you,
Pamanager

Hi,

We are sorry to share that unless the issue is reproduced at our end and reported to our Product team for a fix, we cannot provide any version of the code that will be helpful to you. The presence or absence of the property we have shared above depends upon the message in the PST file and we’ll be in need of your sample file that we can use for investigating the issue further. I would request you to please share your sample PST file or extract the specific messages from the PST and share with us in a separate PST file so that we can investigate it for assisting you further.

Hi,

Sorry, but I cannot share company PST.
I think you have some PST for testing and you can send it to me and I can test with your code.

Thank you,
Pamanager

Hi,

Following is a sample code and sample PST is attached which shows the desired property and required email address. Please give it a try and observe the output. If your PST does not behave in the similar manner, then it is not possible to extract the required information from your PST as it does not contain the properties from which Sender Email Address is retrieved. If still your issue is not resolved, then your PST is must required to observe the problem and provide assistance accordingly.

PersonalStorage pst = PersonalStorage.FromFile(path + "Sample.pst");

FolderInfo folderInfo = pst.GetPredefinedFolder(StandardIpmFolder.Inbox);

if (folderInfo != null)
{
    MessageInfoCollection messageInfoCollection = folderInfo.GetContents();

    foreach (MessageInfo info in messageInfoCollection)
    {
    
        MapiMessage mapi = pst.ExtractMessage(info);
        
        foreach (MapiProperty prop in mapi.Properties.Values)  
        {
            if (prop.Name != "" && prop.Name != null && prop.Name.Contains("SENDER_EMAIL"))
            {
                Console.WriteLine("Name = " + prop.Name);
                Console.WriteLine(mapi.SenderEmailAddress);
            }
            
            if (prop.PropertyTagName != "" && prop.PropertyTagName != null && prop.PropertyTagName.Contains("SENDER_EMAIL"))
            {
                Console.WriteLine("PropertyTagName = " + prop.PropertyTagName);
                Console.WriteLine(mapi.SenderEmailAddress);
            }
        }
            
        foreach (MapiNamedProperty prop in mapi.NamedProperties.Values)
        {
            if (prop.Name != "" && prop.Name != null && prop.Name.Contains("SENDER_EMAIL"))
            {
                Console.WriteLine("Name = " + prop.Name);
                Console.WriteLine(mapi.SenderEmailAddress);
            }
            
            if (prop.PropertyTagName != "" && prop.PropertyTagName != null && prop.PropertyTagName.Contains("SENDER_EMAIL"))
            {
                Console.WriteLine("PropertyTagName = " + prop.PropertyTagName);
                Console.WriteLine(mapi.SenderEmailAddress);
            }
        }
    }
}