VS.NET 2010 (and above) Users Click Here   

HOMECONTACT PRODUCTS DOWNLOADS PURCHASE TESTIMONIALS FORUMS COMPANY CONTACT
Home
Products
Downloads
Purchase
Licensing
Licensing FAQ
Software Updates
Support Forums
Testimonials
Feature Requests
Guarantee
About Us
Contact Us
Hosting Companies
Privacy Policy
   
Shopping Cart



Parsing Cell Phone (mobile) generated emails

by Dave 9. November 2010 07:29

I have a number of customers who need to parse cell phone (mobile) generated emails. These emails come in configurations I never dreamed possible. Although they are technically Mime compliant, their individual parts can be ordered strangely, or have unique headers that normally aren't found in a standard message.

I was sent an email request from a developer (Thanks Cara!) about parsing a T-Mobile generated message. She needed to find and extract the text body part. If a text body part wasn't found, then the html body part was to be found, and converted to plain text. The other requirement was to find all of the images, and download (save) them to a directory.

Below is a code example is something I whipped up for her. She used it as a basis to get started with her application. Error checking has been left out, to keep the code short, workable, but complete.

As always, if anyone has any questions or comments, please let me know.

Thanks!
Dave Wanta

string path =  "t_mobile.eml";

MimeMessage m = MimeMessage.ParseFile( path );

MimePart textPart = m.TextMimePart;
if( textPart == null )
{
	//try and find it, by finding the first inline text part
	MimePartCollection inlineParts = m.InLineParts;
	if( ( inlineParts != null ) && ( inlineParts.Count >0 ) )
	{
		foreach( MimePart part in inlineParts )
		{
			if( (part.Name != null ) && ( part.Name.EndsWith(".txt") ) && ( part.ContentTypeString == "text/plain") )
			{
				//then we found our part
				textPart = part;
				break;
			}
		}
	}
}

string plainText = string.Empty;
if( textPart != null )
{
	plainText = textPart.DecodedText();
}

if( plainText.Length == 0 )
{
	//find the first html part.
	//get the plain text from the html part

	foreach( MimePart part in m.RetrieveAllParts() )
	{
		if( (part.ContentTypeString != null ) && ( part.ContentTypeString.ToLower() == "text/html") )
		{
			string html = part.DecodedText();
			//convert the Html to plain formatted text
			plainText = Utility.ConvertHtmlToText( html).Trim();
			break;
		}
	}
}

//extract all images, and save them to the temp directory
foreach( MimePart part in m.RetrieveAllParts() )
{
	if( part.IsImage() )
	{
		//normally, would need to check for images that already have the same filename
		//and rename accordingly
		//in this example, just save the images to the temp directory
		part.Save("c:\\temp\\");
	}
}

Comments are closed

 

 

Testimonial

Great stuff, great support. "

Mike | Algarve

Read more testimonials
ListNanny aspNetDNS aspNetEmail aspNetPOP3 aspNetMX IPMuncher aspNetMIME aspNetPING aspNetTraceRoute aspNetIMAP aspNetMHT