Read Email Body text

Posted on 2011-10-02
Last Modified: 2013-11-29
I want to create a application which can read the data in email body and store that data in sql server database. Email may contain data of many fields. Can you please suggest how would be the template of the email and how i can extract the data from email body.

Question by:ziorinfo
    LVL 12

    Accepted Solution

    LVL 9

    Expert Comment

    Hello ziorinfo

    Are you able to get the email to read from? Assuming you can, try formatting the email like this:


    My E-Mail body content


    Everything between those lines could be your content. You can use code like this to read it in.


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections.Specialized;
    using System.Xml.Linq;
    using System.IO;
    namespace dump
    	class Program
    		public string MyProperty { get; set; }
    		static void Main(string[] args)
    			FileStream fp = File.Open("File.txt", FileMode.Open, FileAccess.Read);
    			StreamReader reader = new StreamReader(fp);
    			// this is used to indicate if we are reading the email body or not
    			bool state = false;
    			// the email blocks
    			List<string> blocks = new List<string>();
    			string currentBlock = String.Empty;
    				string line = String.Empty;
    				while (true)
    					line = reader.ReadLine();
    					if (line == null)
    					if (line == String.Empty)
    					// are we entering or leaving an email body block?
    					if (line == "###")
    						// we are leaving a block, so save it
    						if (state)
    							currentBlock = String.Empty;
    						state = !state;
    					if (state)
    						currentBlock = String.Format("{0}{1}\n\r",
    			catch (Exception ex)
    			foreach (var block in blocks)
    			// don't exist just yet...

    Open in new window

    LVL 1

    Expert Comment

    In which format you are getting the Mail?

    If its in .NET Mail object format then you can directly read it.

    Please refer the link below:

    Systme.Net.MailMessage  message = new MailMessage(); // Create a new Mail Message here.

    // You can assign all the fields here.
    // i.e.

    message .ToAddress.Add("[To_Address]");
    message.Body = "[Message_Bosy]";

    You can format your mail message like this only.

    If you are getting Mail Message in XML then I would say that first Parse your XML and form the MailMessage[] collection.

    Once you get the collection, you can get the Body and can store them in SQL.

    LVL 69

    Expert Comment

    by:Éric Moreau
    I use this component to read emails:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now