File attachments :names of each attached file

please could you provide the code to obtain the names of each attached file (for each email in a selected folder for a selected mailbox) using

a) the imap protocol

b) the pop3 protocol

(with of the code to detect if the currently selected email has any attachments)

ps... it would be very useful if the help file for the product (.aspose.network) provided more examples of how use the product

thanks

Dear rickwats,

Please check it out. Basically, if you want to access any info about an email message, you can get a MailMessage instance firstly. Pop3Client and ImapClient all support to fetch message from server and cast into a MailMessage instance.

1) for Pop3

Pop3Client client = new Pop3Client("localhost", "user", "12345678");
Pop3MessageInfoCollection infos = null;

client.Connect(true); //connect to server, and auto login to it
infos = client.ListMessages(); //list the messages in the server

if(infos != null)
{
for (int i = 0; i < infos.Count; i++)
{
MailMessage msg = client.FetchMessage(infos[ i ].SequenceNumber);//fetch message to local

foreach (Attachment att in msg.Attachments) //enumerate attachments in the message
{
Console.WriteLine(att.Name + " " + att.MediaType + " " + att.ContentType);
}
}
}

2) for Imap

ImapClient client2 = new ImapClient("localhost", "testuser", "psw");
client2.Connect(true); //connect to server, and auto login to it
client2.SelectFolder("Inbox"); //select the folder
ImapMessageInfoCollection infos2 = null;
infos2 = client2.ListMessages(); // list the messages
if(infos2 != null)
{
for (int i = 0; i < infos2.Count; i++)
{
MailMessage msg = client2.FetchMessage(infos2[ i ].SequenceNumber); //fetch message to local

foreach (Attachment att in msg.Attachments) //enumerate attachements in the message
{
Console.WriteLine(att.Name + " " + att.MediaType + " " + att.ContentType);
}
}
}

Hello, hope someone is still watching this thread.

If i try to get the attachments to save i loose the names og the attachments when i do this

foreach (Attachment att in msg.Attachments) //enumerate attachments in the message
{
Console.WriteLine(att.Name);
}

If i do a count on the msg.Attachments is shows the correct number of attachments, but the name is always == string.Empty

Eny Ideers?

Regards, CIN


Hi,

Could you please attach a sample file for which this is happenning. If you are reading email files after connecting to some mail server, you can save them to your disk by calling MailMessage.Save() method.

Thanks for the fast reply

There is a txt file code snippet attached.

currentItem is a SharePoint ListItem that is feached onItemAdded event in a DocumentLibrary

Hope this is sufficeint info, else please let me know.

I have deployed the Aspose.Network dll to the GAC ? along with my eventhandler

Regards, CIN

Hi,

Sorry for the inconvenience. We are looking into this issue.

As a workaround, could you please try Aspose.Outlook.MapiMessage class. The following code snippet loads a file from disk and iterates through the attachments. You can also call FromStream() method to load the msg file from stream.

MapiMessage msg = MapiMessage.FromFile("c:\\temp\\sample.msg");

foreach (MapiAttachment attachment in msg.Attachments)

{

Console.WriteLine("File Name: " + attachment.FileName); // 8 character file name + 3 character ext

Console.WriteLine("LongFile Name: " + attachment.LongFileName); // full file name + 3 char ext

}

Thanks.

The MapiMessage object worked. thow a had to use the FromStream method, as my files is web based.

using (Stream stream = currentItem.File.OpenBinaryStream())
{
//set to start of stream
stream.Seek(0, SeekOrigin.Begin);
//open the file as mapi message
MapiMessage msg = MapiMessage.FromStream(stream); ........

Regards, CIN