Posted By

elean on 07/18/11


Tagged


Versions (?)

How to extract email content and store into database


 / Published in: C#
 

Hi, i am meeting problem on extracting out my gmail content to database. I have already connect my gmail to imap server and is able to retrieve out and save my gmail message into my computer local drive : temp folder.

However, i am meeting problem on how to store my gmail message to database.This is the url that I had taken from this website.

  1. http://www.aspose.com/documentation/.net-components/aspose.network-for-.net/displaying-email-information-on-screen.html
  2.  
  3. protected void Button1_Click(object sender, EventArgs e)
  4. {
  5. //create imapclient with host, user and password
  6.  
  7. ImapClient client = new ImapClient("imap.gmail.com", 993, "MedicalLearningSystem", "MLSMLSMLS");
  8.  
  9. try
  10. {
  11.  
  12. System.Console.WriteLine("Connecting to the Imap server");
  13.  
  14. //connect to the remote server.
  15.  
  16. //Note:If you use Connect(true), it will invoke the login internal.
  17.  
  18. // Otherwise, you need to call login sequently.
  19.  
  20. client.Connect();
  21.  
  22. System.Console.WriteLine("Connected to the Imap server");
  23.  
  24.  
  25.  
  26. //login to the remote server.
  27.  
  28. client.Login();
  29.  
  30.  
  31.  
  32. System.Console.WriteLine("Logged in to the Imap server");
  33.  
  34.  
  35. // select the inbox folder
  36.  
  37. client.SelectFolder(ImapFolderInfo.InBox);
  38.  
  39.  
  40.  
  41. // get the message info collection
  42.  
  43. ImapMessageInfoCollection list = client.ListMessages();
  44.  
  45.  
  46.  
  47. // download each message
  48.  
  49. for (int i = 0; i < list.Count; i++)
  50. {
  51. String From = list[i].From.ToString();
  52. String To = list[i].To.ToString();
  53.  
  54.  
  55. //save the eml file locally
  56.  
  57. client.SaveMessage(list[i].UniqueId, "C:\\Users\\L33505\\Desktop\\MLS(8Jul)\\Inbox(Emails)\\" + list[i].UniqueId + ".eml");
  58.  
  59. }
  60.  
  61.  
  62.  
  63. //Disconnect to the remote Imap server
  64.  
  65. client.Disconnect();
  66.  
  67. System.Console.WriteLine("Disconncect to the Imap server");
  68.  
  69. }
  70.  
  71. catch (Exception ex)
  72. {
  73.  
  74. System.Console.Write(ex.ToString());
  75.  
  76. }
  77.  
  78.  
  79.  
  80. }

Report this snippet  

You need to login to post a comment.