December 2011

Import contacts from Gmail in Asp.Net

Download Google Contact API from below link.
http://google-gdata.googlecode.com/files/Google%20Data%20API%20Setup%281.4.0.2%29.msi

Add reference to your project following DLL files.
Google.GData.Apps.dll, Google.GData.Client.dll, Google.GData.Contacts.dll, Google.GData.Extensions.dll

.Aspx page look like this

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>IMport Gmail Contacts</title>
</head>
<body>
<form id="frmGmailContacts" runat="server">
<div>
<table>
<tr>
<td>
UserName</td>
<td>
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnImport" runat="server" Text="Import"
onclick="btnImport_Click" />
</td>
</tr>
</table>
</div>
<div>
<asp:GridView ID="gdvContacts" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
 

Code behind page:

public static DataSet GetGmailContacts(string App_Name, string Uname,
string UPassword)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataColumn C2 = new DataColumn();
C2.DataType = Type.GetType("System.String");
C2.ColumnName = "EmailID";
dt.Columns.Add(C2);
RequestSettings rs = new RequestSettings(App_Name, Uname, UPassword);
rs.AutoPaging = true;
ContactsRequest cr = new ContactsRequest(rs);
Feed<Contact> f = cr.GetContacts();
foreach (Contact t in f.Entries)
{
foreach (EMail email in t.Emails)
{
DataRow dr1 = dt.NewRow();
dr1["EmailID"] = email.Address.ToString();
dt.Rows.Add(dr1);
}
}
ds.Tables.Add(dt);
return ds;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = GetGmailContacts("Import GMAIL Contacts",
txtUsername.Text, txtPassword.Text);
gdvContacts.DataSource = ds;
gdvContacts.DataBind();
}

How to unblock port from Windows Server 2008

Many developers have complain about port could not be unblocked although they added “INBOUND/OUTBOUND” rules in “Windows Firewall Advance Security” for “Apple Push Notification Service”, “Remotely Access SQL Server” etc.

Port should not be unblocked until it should be unblocked from “Local Policy Security”.

So, Please check your local policy security. For that, click on “start-> Run-> secpol.msc”.

Software Firewalls with Windows Server 2008

Two of the methods available for configuring IP Security (ipsec) is through a packet filter policy and/or the Security Configuration Wizard.

If you do not plan on using the software firewall we recommend that you setup your own rule set in the hardware firewall as described in these

The article explaining how to log into remote desktop can be found.

For the packet filter:

1.Step:

click on the ‘IP Filter’ on the destop and then click on ‘IP Security Policies on Local Computer’.

2.Step:

If you are not planning on using the Packet Filter policy right click on it and click ‘Unassign’.

3.Step:

If you are going to use the packet filter policy then right click on ‘Packet Filter’ and click on ‘Properties’.

Please check “Block All” option is checked or not? If its checked then unchecked, press “Apply” or “Ok” button. All ports are opened. Now you can check using telnet client for example:

  • Open Commpand prompt
  • If telent client is not installed please installed it using
    pkgmgr /iu:"TelnetClient"
  • After installation, type following command.
    telnet gateway.sandbox.push.apple.com 2195 
    

    e.g. telnet {host} {portnumber}


4.Step:

On this screen you can check the boxes next to the rules you want active or remove the ckechboxes from rules you do not want active.

So for example, if you want to allow ftp incoming, then check the box next to OPen FTP Incoming. Please be aware that the packet filter policy is only compatible with active ftp and not passive ftp.

Note: There are explicit denies at the bottom of the list for ports such as MS SQL labled Close MSDE (TCP/IP) because of the security risks associated with allowing direct access to MS SQL from anywhere on the internet. You will want to take other precautions to mitigate the risk to your data before opening these ports.

From Here you can also add, edit, or remove rules.

For the Security Configuration Wizard

1.Step:

Go to 'Start' --> 'Administrative Tools' --> 'Security Configuration Wizard'

2.Step:

You will want to create a new security policy. This is not a matter of opening ports closed by other software such as the packet filter policy or the hardware firewall. This is the new security policy and you are dictating what the configuration file you will create with this wizard will allow.

3.Step:

Unless you have joined this server to a domain controller leave the server name as the default U number. If you do not know what a domain controller is then you have not joined the server to a domain controller and you should leave the name as the default.

4.Step:

The first three pages of roles list the different installed roles, features, and options. Go through the lists and check the boxes next to the items you to allow and remove the checkboxes from the items you do not want allowd. Below is a screenshot of the Installed roles page.


5.Step:

Continue through the wizard picking the different options you want. We recommend that you name the file were the configuration settings will be saved, the date and time. Then when you edit the policy later, instead of saving over the existing file you once again name the file by date and time so you can distinguish between policies by date. So for example, if you wanted to go back to the policy that was in place last May, you can easily do so.

Convert any Office document to PDF using Open Office in C#.net

Things need to be installed

Download from here

  • Open Office
  • Open Office SDK

The required files listed below:

  • cli_basetypes.dll
  • cli_cppuhelper.dll
  • cli_oootypes.dll
  • cli_ure.dll
  • cli_uretypes.dll

If you have installed Open Office SDK, you will get these files under sdk\cli on you installed SDK folder.

After give reference of above DLL files import following name space in .cs page.

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using uno;
using uno.util;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;

This is the method we used for convert any doc to pdf.

public void ConvertToPdf(string inputFile, string outputFile)
        {
            if (ConvertExtensionToFilterType(Path.GetExtension(inputFile)) == null)
                throw new InvalidProgramException("Unknown file type for OpenOffice. File = " + inputFile);

            StartOpenOffice();

            //Get a ComponentContext
            var xLocalContext =
                Bootstrap.bootstrap();
            //Get MultiServiceFactory
            var xRemoteFactory =
                (XMultiServiceFactory)
                xLocalContext.getServiceManager();
            //Get a CompontLoader
            var aLoader =
                (XComponentLoader)xRemoteFactory.createInstance("com.sun.star.frame.Desktop");
            //Load the sourcefile

            XComponent xComponent = null;
            try
            {
                xComponent = InitDocument(aLoader,
                                          PathConverter(inputFile), "_blank");
                //Wait for loading
                while (xComponent == null)
                {
                    Thread.Sleep(1000);
                }

                // save/export the document
                SaveDocument(xComponent, inputFile, PathConverter(outputFile));
            }
            finally
            {
                if (xComponent != null) xComponent.dispose();
            }
        }

private static void StartOpenOffice()
        {
            var ps = Process.GetProcessesByName("soffice.exe");
            if (ps.Length != 0)
                throw new InvalidProgramException("OpenOffice not found.  Is OpenOffice installed?");
            if (ps.Length > 0)
                return;
            var p = new Process
            {
                StartInfo =
                {
                    Arguments = "-headless -nofirststartwizard",
                    FileName = "soffice.exe",
                    CreateNoWindow = true
                }
            };
            var result = p.Start();

            if (result == false)
                throw new InvalidProgramException("OpenOffice failed to start.");
        }


        private static XComponent InitDocument(XComponentLoader aLoader, string file, string target)
        {
            var openProps = new PropertyValue[1];
            openProps[0] = new PropertyValue { Name = "Hidden", Value = new Any(true) };

            var xComponent = aLoader.loadComponentFromURL(
                file, target, 0,
                openProps);

            return xComponent;
        }

        private static void SaveDocument(XComponent xComponent, string sourceFile, string destinationFile)
        {
            var propertyValues = new PropertyValue[2];
            // Setting the flag for overwriting
            propertyValues[1] = new PropertyValue { Name = "Overwrite", Value = new Any(true) };
            //// Setting the filter name
            propertyValues[0] = new PropertyValue
            {
                Name = "FilterName",
                Value = new Any(ConvertExtensionToFilterType(Path.GetExtension(sourceFile)))
            };
            ((XStorable)xComponent).storeToURL(destinationFile, propertyValues);
        }

        private static string PathConverter(string file)
        {
            if (string.IsNullOrEmpty(file))
                throw new NullReferenceException("Null or empty path passed to OpenOffice");

            return String.Format("file:///{0}", file.Replace(@"\", "/"));
        }

        public static string ConvertExtensionToFilterType(string extension)
        {
            switch (extension)
            {
                case ".doc":
                case ".docx":
                case ".txt":
                case ".rtf":
                case ".html":
                case ".htm":
                case ".xml":
                case ".odt":
                case ".wps":
                case ".wpd":
                    return "writer_pdf_Export";
                case ".xls":
                case ".xlsb":
                case ".xlsx":
                case ".ods":
                    return "calc_pdf_Export";
                case ".ppt":
                case ".pptx":
                case ".odp":
                    return "impress_pdf_Export";

                default:
                    return null;
            }
        }