Gokul's Blog


Leave a comment

POP3 and BizTalk

(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; }


Leave a comment

Session Management in ASP .net

Create a helper class known as "SessionStoreHelper.cs".

public static string SiteRootUrl
    {
        get { return (string)HttpContext.Current.Application["SiteRootUrl"]; }
        set
        {
            if (value != null)
            {
                HttpContext.Current.Application["SiteRootUrl"] = value;
            }
            else
            {
                HttpContext.Current.Application.Remove("SiteRootUrl");
            }
        }
    }

public static System.Data.DataSet MySessionValue1
    {
        get { return (DataSet)HttpContext.Current.Session["MySessionValue1"]; }
        set
        {
            if (value != null)
            {
                HttpContext.Current.Session["MySessionValue1"] = value;
            }
            else
            {
                HttpContext.Current.Session.Remove("MySessionValue1");
            }
        }
    }

While we need to access the Session in code we can use it as follows

public string SiteRoot()
    {
        if (SessionStoreHelper.SiteRootUrl == null)
            SessionStoreHelper.SiteRootUrl = Utility.GetSiteRoot();

        return ((string)SessionStoreHelper.SiteRootUrl);
    }

.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; }


Leave a comment

Debugging with SP_who & Dbcc Inputbuffer (SPID)

When dealing with the problem of connection leaks SP_who and SP_who2 are very helpful to find the issue.

With some googling and writing some code was able to create some code which will help us to determine the problem causing procedure, functions, etc.

drop table #retTable
drop table #DbccOutput

create table #retTable(
    spid smallint,
    ecid smallint,
    status nchar(30),
    loginname nchar(128),
    hostname nchar(128),
    blk char(5),
    dbname nchar(128),
    cmd nchar(16),
    request_id INT
)

INSERT INTO #retTable EXEC sp_who
SELECT * FROM #retTable WHERE dbname = ‘MyTableName’

declare @iSpidMax varchar(6)
declare @sSql varchar(50)

Create Table #DbccOutput ( spid integer, EventInfo varchar(256) )
Create Table #DbccOutputTemp ( EventType varchar(256), Parameters integer, EventInfo varchar(256))
Declare MyCursor Cursor For
  Select Distinct spid From #retTable
Open MyCursor
Fetch Next From MyCursor Into @iSpidMax
While @@Fetch_Status = 0
Begin
  Set @sSql = ‘DBCC inputbuffer(‘ + Convert( varchar(3), @iSpidMax ) + ‘)’
  Insert Into #DbccOutputTemp Exec ( @sSql )
  Insert Into #DbccOutput ( spid, EventInfo )
    Select @iSpidMax As spid, EventInfo From #DbccOutputTemp
  Delete #DbccOutputTemp
  Fetch Next From MyCursor Into @iSpidMax
End
Close MyCursor
Deallocate MyCursor

select * from #DbccOutput
select distinct eventinfo from #DbccOutput