• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

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
0
ziorinfo
Asked:
ziorinfo
1 Solution
 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now