0

I need to read the mail contents like To, CC, Subject, Body, Attachments of a mail and store it in Database tables. I am using the below code.

  EmailMessage msg = (EmailMessage)item;
  foreach (var col in msg.CcRecipients)
   {
   ccList += col.Address.ToString() + ";";
   }

   foreach (var col in msg.ToRecipients)
   {
   toList += col.Address.ToString() + ";";
   }

Similarly I can read Body and Subject. Problem is in reading Attachments How to read the Attachments and store it in Database.

I can use msg.Attachments. But What would be the type of the object. Please suggest some better solution to store the attachments to Database.

user3859666
  • 279
  • 1
  • 10
  • 24

2 Answers2

0

You would probably need a table with at least two columns. A varbinary to store the actual file (byte[]), and another column of varchar (string) to store the mime type.

Mike_G
  • 16,237
  • 14
  • 70
  • 101
0

Relate a EmailMessage to zero or many Attachments. Use varbinary(MAX) as the SQL column type. Or FILESTREAM. References below:

  1. varbinary(max) tames blob (with code)
  2. tsql binary & varbinary (msdn)
  3. Compare Options for Storing Blobs (SQL Server) (msdn)
  4. How To Update A BLOB In SQL SERVER Using TSQL (StackOverflow question)
  5. Blob data in huge SQL Server database (StackOverflow question)
  6. An Introduction to SQL Server FileStream (good tutorial)
  7. Download sqlserver varbinary (StackOverflow question)
Community
  • 1
  • 1
andrei.ciprian
  • 2,895
  • 1
  • 19
  • 29