Wednesday, March 31, 2010

Read mail of Inbox using IMAP

In one of my project i need to read all emails and if specified formatted mail is found then Use it.

Below is code to read.

public string IMAP_COMMAND_PREFIX = "IMAP00";
    public int IMAP_COMMAND_COUNT = 0;
    public string IMPAP_SUCCESS_RESPONSE = " OK";
    public string IMAP_NO_RESPONSE = " NO";
    public string IMAP_BAD_RESPONSE = " BAD";
    public string IMAP_COMMAND_EOL = "\r\n";
    public string IMAP_SEARCH_RESPONSE = "* SEARCH ";
    public string IMAP_UNSEEN_FLAG = "unseen";
    public string IMAP_CAPABILITY_COMMAND = "CAPABILITY";
    public string IMAP_LOGIN_COMMAND = "LOGIN";
    public string IMAP_LOGOUT_COMMAND = "LOGOUT";
    public string IMAP_SELECT_COMMAND = "SELECT INBOX";
    public string IMAP_SEARCH_COMMAND = "SEARCH";
    public string IMAP_UIDFETCH_COMMAND = "UID FETCH";
    public string IMAP_BODYSTRUCTURE_COMMAND = "BODYSTRUCTURE";
 protected void Page_Load(object sender, EventArgs e)
 {
  try {
   TcpClient tcpclient = new TcpClient(); // create an instance of TcpClient
   string strCommand = string.Empty;
   tcpclient.Connect("host name", 143); // HOST NAME IAMP SERVER and port number 143 for POP 
   Stream sslstream = tcpclient.GetStream();
   System.IO.StreamWriter sw = new StreamWriter(sslstream); // Asssigned the writer to stream
   System.IO.StreamReader reader = new StreamReader(sslstream); // Assigned reader to stream
   strCommand = IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + " " + IMAP_CAPABILITY_COMMAND + IMAP_COMMAND_EOL;
    ReadResonse(reader, strCommand, sw);
    strCommand = IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + " " + IMAP_LOGIN_COMMAND + " " + "EmailAddressID" + " " + "Email Password";
     ReadResonse(reader, strCommand, sw);
     strCommand = IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + " " + IMAP_SELECT_COMMAND + IMAP_COMMAND_EOL;
      ReadResonse(reader, strCommand, sw);
      strCommand = IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + " " + IMAP_SEARCH_COMMAND + " " + IMAP_UNSEEN_FLAG + IMAP_COMMAND_EOL;
      ReadResonse(reader, strCommand, sw);
      strCommand = IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + " " + IMAP_SEARCH_COMMAND + " " + IMAP_LOGOUT_COMMAND + IMAP_COMMAND_EOL;
      ReadResonse(reader, strCommand, sw);

        }
        catch (Exception ex)
        {
        }

    }
    public void ReadResonse(StreamReader reader, string strCommand, StreamWriter swriter)
    {
        try
        {
            swriter.WriteLine(strCommand);
            swriter.Flush();
            bool IsLastSuccess = false;
            string strResponse = string.Empty;
            while ((strResponse = reader.ReadLine()) != null)
            {

                if (strResponse.StartsWith(IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + IMPAP_SUCCESS_RESPONSE)) // find the success character in line
                {
                    break;
                }
                else if (strResponse.StartsWith(IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + IMAP_NO_RESPONSE)) // find the failure character in line
                {

                    break;
                }
                else if (strResponse.StartsWith(IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + IMAP_BAD_RESPONSE)) // find the failure character in line
                {
                    break;
                }
                else if (strResponse.StartsWith(IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + IMAP_BAD_RESPONSE)) // find the failure character in line
                {
                    break;
                }
                else if (strResponse.StartsWith(IMAP_SEARCH_RESPONSE)) // find the unread mail
                {
                    string strTempUnread = strResponse.Substring(strResponse.IndexOf(IMAP_SEARCH_RESPONSE) + IMAP_SEARCH_RESPONSE.Length);
                    string[] strArray = strTempUnread.Split(" ".ToCharArray());
                    string strSearch = string.Empty;
                    while ((strSearch = reader.ReadLine()) != null)
                    {
                        if (strSearch.StartsWith(IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + IMPAP_SUCCESS_RESPONSE))
                        {
                            IsLastSuccess = true;
                            break;
                        }
                    }

                    for (int i = 0; i < strArray.Length; i++)
                    {

                           //read header of email
                        swriter.WriteLine(IMAP_COMMAND_PREFIX + i.ToString() + " " + "FETCH " + strArray[i].ToString() + " BODY[HEADER.fields (from)]\r\n"); // this will retrive your first email 
                        swriter.Flush();
                        string strTemp = string.Empty;
                        while ((strTemp = reader.ReadLine()) != null)
                        {
                            if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMPAP_SUCCESS_RESPONSE))
                            {
                                break;
                            }
                            else if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMAP_NO_RESPONSE)) // find the failure character in line
                            {

                                break;
                            }
                            else if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMAP_BAD_RESPONSE)) // find the failure character in line
                            {
                                break;
                            }
                            else if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMAP_BAD_RESPONSE)) // find the failure character in line
                            {
                                break;
                            }                           

                        }

// read body of email
                        swriter.WriteLine(IMAP_COMMAND_PREFIX + i.ToString() + " " + " FETCH " + strArray[i].ToString() + " BODY[TEXT]\r\n"); // this will retrive your first email 
                        swriter.Flush();
                        while ((strTemp = reader.ReadLine()) != null)
                        {
                            if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMPAP_SUCCESS_RESPONSE))
                            {
                                break;
                            }
                            else if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMAP_NO_RESPONSE)) // find the failure character in line
                            {

                                break;
                            }
                            else if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMAP_BAD_RESPONSE)) // find the failure character in line
                            {
                                break;
                            }
                            else if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMAP_BAD_RESPONSE)) // find the failure character in line
                            {
                                break;
                            }
                        }

             }
}
   if (IsLastSuccess == true)
                    break;
 }

            IMAP_COMMAND_COUNT++;
        }
        catch (Exception ex)
        {
            log4net.ILog logger = log4net.LogManager.GetLogger("File");
            logger.Error("Lead Convertor + ReadResonse :" + ex.Message);
        }
    }
}


Beloow link for IMAP Commands http://bobpeers.com/technical/telnet_imap.php
Happy coding :-)

Wednesday, March 24, 2010

Create Excel file also use various formulas with in it

In one project has requirement that need to create excel file through coding and need to use various excel formals for it.Below is code for it.


FileStream fs = new FileStream(@"D:\Test\TestExcel.xls", FileMode.Create);
        StreamWriter sr = new StreamWriter(fs);
        Response.ContentType = "application/ms-excel";
        StringBuilder sb = new StringBuilder();
        sb.Append("<table>");
        sb.Append("<tr><td><font face='Arial' color='black' size='2'><b>Name</b></font></td>");
        sb.Append("<td><font face='Arial' color='black' size='2'><b>Amount</b></font></td></tr>");
        for (int i = 0; i < 10; i++)
        {
            sb.Append("<tr>");
            sb.Append("<td align='center' vertical-align='middle'>" + "<font face='Arial' color='black' size='2'>Name12</font></td>");
            sb.Append("<td align='center' vertical-align='middle'>" + "<font face='Arial' color='black' size='2'>" + i.ToString() + "</font></td>");
            sb.Append("</tr>");

        }
        sb.Append("<tr>");
        sb.Append("<td>Total</td>");
        sb.Append("<td>=SUM(B1:B11)</td></tr>");
        sb.Append("</table>");
        sr.WriteLine(sb.ToString());
        sr.Close();
        fs.Dispose();

Response.Clear();
Response.AddHeader("content-disposition", @"attachment; filename=Test.xls");
Response.Write(sb.ToString());


Happy coding ..

Tuesday, March 23, 2010

Syntax highlighter

Blogger needs some kind of syntax-highlighting plug-in before posting source code on a blog.

Today i am looking for it and i get great post through which i have learned syntax-highlighting.

Here are the steps:-

1) In th back end click on "NEW POST" or "CUSTOMIZE" then Go to "Layout" tab and click on "EDIT HTML" sub-tab.


2) Go to http://syntaxhighlighter.googlecode.com/svn/trunk/Styles/SyntaxHighlighter.css, then perform a "select all" and "copy". The css information is now in the clipboard.


3) Paste the css information at the end of the css section of your blogger html template before </b:skin>

4)Before the </head> tag, paste the following:

<!-- Add-in CSS for syntax highlighting -->
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shCore.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCpp.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCSharp.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCss.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushDelphi.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushJava.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushJScript.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushPhp.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushPython.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushRuby.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushSql.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushVb.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushXml.js' type='text/javascript'></script>

remove lines for languages you'll never use


5) Before the </body> tag, insert the following:
<!-- Add-in Script for syntax highlighting -->
<script language='javascript'>
dp.SyntaxHighlighter.BloggerMode();
dp.SyntaxHighlighter.HighlightAll('code');
</script>


6) Use the "Preview" button to make sure your website is correct, then click "Save Template".

7) When composing a blog entry that contains source code, click the "Edit Html" tab and put your source code (with html-escaped characters) between these tags:

<pre name="code" class="cpp">
...Your html-escaped code goes here...
</pre>


substitute cpp with if you are using below languages

Language Aliases
C++ cpp, c, c++
C# c#, c-sharp, csharp
CSS css
Delphi delphi, pascal
Java java
Java Script js, jscript, javascript
PHP php
Python py, python
Ruby rb, ruby, rails, ror
Sql sql
VB vb, vb.net
XML/HTML xml, html, xhtml, xslt


You can do html-escaping by this link: http://www.accessify.com/tools-and-wizards/developer-tools/quick-escape/default.php


Source: http://heisencoder.net/2009/01/adding-syntax-highlighting-to-blogger.html

Dell laptop to switch functionality of function key

This drove me crazy for about 2 hours today. I swear I saw something in the control panel to switch between function keys and multimedia keys but alas I couldn't find it. Here's what you have to do: As the system in booting hit F2 to go to Setup. From there click 'System Configuration' then 'Function Key Behavior'. Switch to 'Fuction Key First' then hit the 'Apply' box, then 'Exit'. Now the function keys will work normaly and you can hold 'Fn' to use the multimedia fuctions.

Seriously, I searched through offline help for an hour and a half, then these forums for half an hour and I'm amazed at how hard it is to find out how to do something this simple. I'm just glad I've been messing around with computers since the days of DOS so I thought to look in the BIOS.