Gokul's Blog

POP3 and BizTalk

Leave a comment

(original Publish date: April 16, 2008)
Abstract : Receive an email from an POP3 server and write the attachments in to a folder.

Solution 1: Use the BizTalk 2006 R2 POP3 adapter and an orchestration to achieve it.

The receive location has to be configured as follows.

[Image] of POP3 receive location

I was able to get the filename by getting the message partname while I was using Gmail POP3 as my POP3 server, when we switched to Windows POP3 server I was unable to get the Filename. The algorithm used by BizTalk POP3 adapter to get the filename is as follows

1. Take it from the Content-Description header.
2. If not found, then the part name is based on the Content-ID header.
3. If still not found, then generate a GUID for the part name.

Receive the email as a Multipart message and then loop through the parts. While looping through check whether the MIME.FileName property exists if not then its the body part :).

Please find the attached zip file containing the orchestration code. This orchestration sample will read an email and write the attachments one by one to a folder.

Solution 2: While I was exploring through various options to overcome the issue. I came across an interesting scenario. If you just need a file which contain the email with all the contents in a folder. .eml comes to the rescue. Read the email using a receive port and write the message using a send port with an  .eml extension. When a user double-click on this file it opens in outlook express and its very simple solution(15 minutes we can achieve this). This solution suits for environments where outlook express is allowed.

Solution 3: Use the Raw Message send it to a helper method there are a lot of third party .Net based MIME encoding tools. Manipulate the message as needed. A google search on .Net mime encoding returns a lot of tools. An excellent article from Roy Ashbrook explains how to use Regex to parse .eml files.

Here is the code for reference taken from the above blog.

using System;
using System.IO;
using System.Text.RegularExpressions;
 
namespace parse.eml
{
    class Email
    {
        string _path,_to,_from,_subject,_urls;
 
        public Email(string path)
            {
                _path = path;
                string fc = new StreamReader(path).ReadToEnd();
                _from = Regex.Matches(fc, "From: (.+)")[0].ToString();
                _to = Regex.Matches(fc, "To: (.+)")[0].ToString();
                _subject = Regex.Matches(fc, "Subject: (.+)")[0].ToString();
                _urls = string.Empty;
                foreach (Match m in Regex.Matches(fc,@"https?://([a-zA-Z\.]+)/"))
                {
                _urls += m.ToString() + ' ';
                }
            }
            
            public void show()
            {
                Console.WriteLine("{0}\n\t{1}\n\t{2}\n\t{3}\n\t{4}",_path, _to, _from, _subject, _urls);
            }
     }
        
    class Program
    {
        static void Main(string[] args)
        {
            foreach (string f in Directory.GetFiles(".", "*.eml"))
            {
            Email e = new Email(f);
            e.show();
            }
        }
    }
} //namespace
      
 
Reference articles:

http://www.biztalkgurus.com/forums/p/2217/4455.aspx#4455

http://www.biztalkgurus.com/files/folders/biztalk_2006_samples/entry15562.aspx

And also I would like to thank my friends/ Mandi(MS-support) for their guidance, in getting "solution 1" possible.

Solution 2 coming soon!

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s