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?
 
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
0
 
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.



0
 
Éric MoreauSenior .Net ConsultantCommented:
I use this component to read emails: http://www.lesnikowski.com/mail/
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.