Thursday, October 6, 2011

ASP.net - Google Buzz Post Message from ASP.net

In this article i will show you how to post message on Google Buzz from Asp.net.


Step 1
Google Buzz require a valid gmail account. You need to decide, from which gmail account you would like to post google buzz. For this example, i would be posting google buzz from my gmail account.


Step 2
Enable Google Buzz posting by sending email. Please follow following to do so.

  • Log into your Gmail Account.
  • Open Google Buzz.
  • Click on Connected Sites link as shown in figure.

Click on Image for Better View

  • Click on Add button associated with "Post via buzz@gmail"

Click on Image for Better View
  • Click on Save Button

Step 3
Create a Web Application and give solution name as SolGoogleBuzz.

Step 4
Create a Form in .aspx page for sending message to Google Buzz,it is look like this

<div style="text-align:center">
            <h3 >Google Buzz Post Message from ASP.net</h3>
            <table border="0" cellpadding="5" cellspacing="5" align="center" width="50%">
                <tr align="left">
                    <td>Gmail MailID</td>
                    <td><asp:TextBox ID="txtGmailMailID" runat="server" Height="23px" Width="258px"></asp:TextBox></td>
                </tr>
                 <tr align="left">
                    <td>Gmail Password</td>
                    <td>
                        <asp:TextBox ID="txtGmailPassword" runat="server" Height="23px" Width="258px" TextMode="Password"></asp:TextBox>
                     </td>
                </tr>
                <tr align="left">
                    <td>Post Message</td>
                    <td>
                        <asp:TextBox ID="txtPostMessage" runat="server" Height="83px" 
                            TextMode="MultiLine" Width="260px" ></asp:TextBox>
                    </td>
                </tr>
                <tr align="center">
                    <td colspan="2" valign="middle">
                        <asp:Button ID="btnPost" runat="server" Text="Post on Google Buzz" 
                            onclick="btnPost_Click" />
                    </td>
                </tr>
                <tr align="center">
                    <td colspan="2" valign="middle">
                        <asp:Label ID="lblSuccessMessage" runat="server"></asp:Label>
                    </td>
                </tr>
            </table>
    </div>


Click on Image for Better View


Step 5
In Code Behind write a function  to send message to the Google Buzz,it is look like this
#region Methods
    /// 
    ///  Post Message on Google Buzz
    /// 
    /// 
Specify the Gmail Id    /// 
Specify the Gmail Password    /// 
Post a Message    /// Boolean
    private Boolean PostBuzzMessage(String GMailID, String GMailPassword, String Message)
    {
        Boolean Flag = false;
        try
        {
            //Create Mail Message Object with content that you want to send with mail.
            System.Net.Mail.MailMessage GMailMessage = new System.Net.Mail.MailMessage(GMailID, "buzz@gmail.com",Message,String.Empty);

            GMailMessage.IsBodyHtml = false;

            //Proper Authentication Details need to be passed when sending email from gmail
            System.Net.NetworkCredential GMailAuthentication = new System.Net.NetworkCredential(GMailID,GMailPassword);

            //Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
            //For different server like yahoo this details changes and you can
            //get it from respective server.
            System.Net.Mail.SmtpClient GMailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);

            //Enable SSL
            GMailClient.EnableSsl = true;

            GMailClient.UseDefaultCredentials = false;

            GMailClient.Credentials = GMailAuthentication;

            // Send Message 
            GMailClient.Send(GMailMessage);

            Flag = true;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message); 
        }

        return Flag;
    }

    #endregion

Step 6
Call a above function on Button click event,it is look like this
protected void btnPost_Click(object sender, EventArgs e)
    {
        try
        {
            if (PostBuzzMessage(txtGmailMailID.Text.Trim(), txtGmailPassword.Text.Trim(), txtPostMessage.Text.Trim()))
            {
                lblSuccessMessage.Text = "Succesfully Post Message on Google Buzz";
            }
            else
            {
                lblSuccessMessage.Text = "OOps....We got an ERROR";
            }
        }
        catch (Exception)
        { }
    }

Step 7
Run the project and send message,it is look like this
Click on Image for Better View


Step 8
Now Check your Buzz to see message,it is look like this


Click on Image for Better View




Full Code


1. .Aspx Code

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align:center">
            <h3 >Google Buzz Post Message from ASP.net</h3>
            <table border="0" cellpadding="5" cellspacing="5" align="center" width="50%">
                <tr align="left">
                    <td>Gmail MailID</td>
                    <td><asp:TextBox ID="txtGmailMailID" runat="server" Height="23px" Width="258px"></asp:TextBox></td>
                </tr>
                 <tr align="left">
                    <td>Gmail Password</td>
                    <td>
                        <asp:TextBox ID="txtGmailPassword" runat="server" Height="23px" Width="258px" TextMode="Password"></asp:TextBox>
                     </td>
                </tr>
                <tr align="left">
                    <td>Post Message</td>
                    <td>
                        <asp:TextBox ID="txtPostMessage" runat="server" Height="83px" 
                            TextMode="MultiLine" Width="260px" ></asp:TextBox>
                    </td>
                </tr>
                <tr align="center">
                    <td colspan="2" valign="middle">
                        <asp:Button ID="btnPost" runat="server" Text="Post on Google Buzz" 
                            onclick="btnPost_Click" />
                    </td>
                </tr>
                <tr align="center">
                    <td colspan="2" valign="middle">
                        <asp:Label ID="lblSuccessMessage" runat="server"></asp:Label>
                    </td>
                </tr>
            </table>
    </div>
    </form>
</body>
</html>

2. .Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    #region Methods
    /// 
    ///  Post Message on Google Buzz
    /// 
    /// 
Specify the Gmail Id    /// 
Specify the Gmail Password    /// 
Post a Message    /// Boolean
    private Boolean PostBuzzMessage(String GMailID, String GMailPassword, String Message)
    {
        Boolean Flag = false;
        try
        {
            //Create Mail Message Object with content that you want to send with mail.
            System.Net.Mail.MailMessage GMailMessage = new System.Net.Mail.MailMessage(GMailID, "buzz@gmail.com",Message,String.Empty);

            GMailMessage.IsBodyHtml = false;

            //Proper Authentication Details need to be passed when sending email from gmail
            System.Net.NetworkCredential GMailAuthentication = new System.Net.NetworkCredential(GMailID,GMailPassword);

            //Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
            //For different server like yahoo this details changes and you can
            //get it from respective server.
            System.Net.Mail.SmtpClient GMailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);

            //Enable SSL
            GMailClient.EnableSsl = true;

            GMailClient.UseDefaultCredentials = false;

            GMailClient.Credentials = GMailAuthentication;

            // Send Message 
            GMailClient.Send(GMailMessage);

            Flag = true;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message); 
        }

        return Flag;
    }

    #endregion

    protected void btnPost_Click(object sender, EventArgs e)
    {
        try
        {
            if (PostBuzzMessage(txtGmailMailID.Text.Trim(), txtGmailPassword.Text.Trim(), txtPostMessage.Text.Trim()))
            {
                lblSuccessMessage.Text = "Succesfully Post Message on Google Buzz";
            }
            else
            {
                lblSuccessMessage.Text = "OOps....We got an ERROR";
            }
        }
        catch (Exception)
        { }
    }
}

Download
Download Source Code

No comments:

Post a Comment