Read Email Body text

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.

Thanks
ziorinfoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
richard_hughesCommented:
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.

Thanks,

Richard
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;

			try
			{
				string line = String.Empty;
				while (true)
				{
					line = reader.ReadLine();
					if (line == null)
						break;

					if (line == String.Empty)
						continue;

					// are we entering or leaving an email body block?
					if (line == "###")
					{
						// we are leaving a block, so save it
						if (state)
						{
							blocks.Add(currentBlock);

							currentBlock = String.Empty;
						}

						state = !state;

						continue;
					}

					if (state)
					{
						currentBlock = String.Format("{0}{1}\n\r",
							currentBlock,
							line);
					}
				}
			}
			catch (Exception ex)
			{
			}

			reader.Close();
			fp.Close();
			fp.Dispose();

			foreach (var block in blocks)
			{
				Console.WriteLine(block);
			}

			// don't exist just yet...
			Console.Read();
		}
	}
}

Open in new window

file.txt
harshgandhi18Commented:
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:

http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx 

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.FromAddress.Add("[from_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.



Éric MoreauSenior .Net ConsultantCommented:
I use this component to read emails: http://www.lesnikowski.com/mail/
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Email Protocols

From novice to tech pro — start learning today.