Monday, October 27, 2008

Mass Email System Using Google Account and Email address from Access database.

Mass Email System Using Google Account and Email address from Access database.
 
Getting email ids from access database, using google smtp to send message. 

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

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table border="1" style="width: 80%">
            <tr>
                <td>
                    &nbsp;
                </td>
                <td style=" text-align: center;">
                    &nbsp;<asp:Label ID="Label3" runat="server" Font-Bold="True" Text="Automatic Email System"
                        Width="224px"></asp:Label></td>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td style=" text-align: left">
                    &nbsp; &nbsp;
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td style=" text-align: right">
                    <asp:Label ID="Label1" runat="server" Text="Subject:"></asp:Label></td>
                <td>
        <asp:TextBox ID="TxtSubject" runat="server" Width="282px"></asp:TextBox></td>
                <td>
                </td>
            </tr>
            <tr>
                <td style=" height: 246px; text-align: right">
                    <asp:Label ID="Label2" runat="server" Text="Message:"></asp:Label></td>
                <td style=" height: 246px">
                    <asp:TextBox ID="Txtmsg" runat="server" Height="309px" TextMode="MultiLine"
            Width="586px"></asp:TextBox>
                </td>
                <td style=" height: 246px">
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;<asp:Button ID="Button1" runat="server" Text="Send" OnClick="Button1_Click" />
        <asp:Label ID="lblMsg" runat="server" Width="246px"></asp:Label></td>
                <td>
                </td>
            </tr>
        </table>
        &nbsp;<br />
        &nbsp;<br />
        <br />
        <br />
        <br />
        <br />
      </div>
    </form>
</body>
</html>

 

 

code Behind
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net;
using System.Net.Mail;
using System.Data.OleDb;

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

        string strcomp = Txtmsg.Text;
        Session["Comment"] = strcomp;
        string txtsub = TxtSubject.Text;
        Session["txtSubject"] = txtsub.ToString();
    }


    void sendemail(String EmailAdd)
    {
        SmtpClient client = new SmtpClient();
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.EnableSsl = true;
        client.Host = "smtp.gmail.com";
        client.Port = 587;

        // setup Smtp authentication
       System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("YourGmailID@gmail.com", "YourPassword");
       


        client.UseDefaultCredentials = false;
        client.Credentials = credentials;

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("YourGmailID@gmail.com");
        //UserID
        //msg.From = new MailAddress(UserID.Text);
        msg.To.Add(new MailAddress(EmailAdd.ToString()));


        msg.Subject = Session["txtSubject"].ToString();
        msg.IsBodyHtml = true;
        msg.Body = string.Format("<html><head></head><body><b>" + Session["Comment"].ToString() + "</b></body>");

        try
        {
            client.Send(msg);
            lblMsg.Text = "Message sent.";
        }
        catch (Exception ex)
        {
            // lblMsg.ForeColor = Color.Red;
            lblMsg.Text = "Error occured while sending your message." + ex.Message;
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TxtSubject.Text != "")
        {
            ReadDB();
        }
        else
            lblMsg.Text = "Error Subject Blanks";
       
    }

    void ReadDB()
    {
        string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source=c:\\database\\xxxxx.mdb";
        OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
        OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();
        string Strsql = "SELECT ID, EmailID FROM EmailAdd";
        myOleDbCommand.CommandText = Strsql;
        myOleDbConnection.Open();
        OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader();
        myOleDbDataReader.Read();

        while (myOleDbDataReader.Read())
        {
            sendemail(myOleDbDataReader["EmailID"].ToString());
        }
    }
}

No comments: