Showing posts with label Asp.net Mail. Show all posts
Showing posts with label Asp.net Mail. Show all posts

Wednesday, 26 February 2014

Send Email to all user's email Id in the Database in Asp.net


Description:- Hello Friends Today we will learn that how to send email to all the user's email id in the database on Button click event.First of all we will create a button on design page.On the onclick event give a name to the event whatever you want.Second thing you have to make a Table in the database that is containing Column for email in which the email id for all the user are stored.i will also provide you snapshots for creating your Table in the database.the snapshot for database are given below.
Database Table:- Design Table according to your Data that you want to store in the database.
After Creating add the data to the table.Now the code for Design page is below.
Demo:-


Design Code:-

Code Behind:-
 protected void SendMail_Click(object sender, EventArgs e)
{
    cmd3 = new SqlCommand("select email from tbluser", con);
        string email;
        con.Open();
       SqlDataReader dr=cmd3.ExecuteReader();
       while(dr.Read())
       { 
           email=dr[0].ToString();
           MailMessage myMsg = new MailMessage();
           myMsg.From = new MailAddress("*****@gmail.com");
           myMsg.To.Add(email);
           myMsg.Subject = "This is Reminder Mail to you";
           myMsg.IsBodyHtml = true;
           string currentyear = DateTime.Now.Year.ToString();


           myMsg.Body = "Dear Pay your Membership Fee.If Already Paid Ignore this mail";



           myMsg.CC.Add("*********@gmail.com");
           // your remote SMTP server IP.
           SmtpClient smtp = new SmtpClient();
           smtp.Host = "smtp.gmail.com";
           smtp.Port = 587;
           smtp.Credentials = new System.Net.NetworkCredential("*******@gmail.com", "Password");
           smtp.EnableSsl = true;
           smtp.Send(myMsg);

           lblmail.Text = "Mail Sent";
       }
}

Wednesday, 15 May 2013

How to Send Gridview in Email in Asp.Net Using C#, VB.NET

Demo-:
                         

Design Code-:



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendGridEmail.aspx.cs" Inherits="SendGridviewWithEmail" %>
<!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 id="Head1">
<title>Send Mail Gridview Asp.net </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvUserInfo" runat="server">
<HeaderStyle BackColor="#6699FF" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div><br /><br />
<asp:Button ID="btnSendMail" runat="server" Text="Send Gridview As Mail" onclick="btnSendMail_Click" />
</form>
</body>
</html>


Asp.net Code Behind-:


using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Net.Mail;
using System.Text;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class SendGridEmail: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridview();
        }
    }
    // Bind Databse with Gridview
    protected void BindGridview()
    {
        SqlConnection con = new SqlConnection("Data Source=IT-HP\\sqlexpress;Initial Catalog=Employee;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT TOP 10 UserName,FirstName,LastName,Location FROM UserInformation", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        gvUserInfo.DataSource = ds;
        gvUserInfo.DataBind();
    }
    protected void btnSendMail_Click(object sender, EventArgs e)
    {
        SendHTMLMail();
    }
    // Get HTML File and replace HTML File values with values and send mail
    public void SendHTMLMail()
    {
        MailMessage Msg = new MailMessage();
        MailAddress fromMail = new MailAddress("rahulupadhyay010@gmail.com");
        // Sender e-mail address.
        Msg.From = fromMail;
        // Recipient e-mail address.
        Msg.To.Add(new MailAddress("Aspdotnetstudio@gmail.com"));
        // Subject of e-mail
        Msg.Subject = "Send Gridivew in EMail";
        Msg.Body += "Please check below data <br/><br/>";
        Msg.Body += GetGridviewData(gvUserInfo);
        Msg.IsBodyHtml = true;
        string sSmtpServer = "";
        sSmtpServer = "smtp.gmail.com";
        SmtpClient a = new SmtpClient();
        a.Host = sSmtpServer;
        a.EnableSsl = true;
        a.Send(Msg);
    }
    // This Method is used to render gridview control
    public string GetGridviewData(GridView gv)
    {
        StringBuilder strBuilder = new StringBuilder();
        StringWriter strWriter = new StringWriter(strBuilder);
        HtmlTextWriter htw = new HtmlTextWriter(strWriter);
        gv.RenderControl(htw);
        return strBuilder.ToString();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
}


VB.net Code-:


Imports System.Web
Imports System.Data.SqlClient
Imports System.Data
Imports System.Net.Mail
Imports System.Text
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls
Partial Class VBSendGridviewMail
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindGridview()
        End If
    End Sub
    ' This method is used to bind gridview from database
    Protected Sub BindGridview()
        Dim con As New SqlConnection("Data Source=IT-HP\\sqlexpress;Initial Catalog=Employee;Integrated Security=True")
        con.Open()
        Dim cmd As New SqlCommand("SELECT TOP 10 UserName,FirstName,LastName,Location FROM UserInformation", con)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds)
        gvUserInfo.DataSource = ds
        gvUserInfo.DataBind()
    End Sub
    Protected Sub btnSendMail_Click(ByVal sender As Object, ByVal e As EventArgs)
        SendHTMLMail()
    End Sub
    ' Method Which is used to Get HTML File and replace HTML File values with dynamic values and send mail
    Public Sub SendHTMLMail()
        Dim Msg As New MailMessage()
        Dim fromMail As New MailAddress("rahulupadhyay010@gmail.com")
        ' Sender e-mail address.
        Msg.From = fromMail
        ' Recipient e-mail address.
        Msg.[To].Add(New MailAddress("Aspdotnetstudio@gmail.com"))
        ' Subject of e-mail
        Msg.Subject = "Send Gridivew in EMail"
        Msg.Body += "Please check below data <br/><br/>"
        Msg.Body += GetGridviewData(gvUserInfo)
        Msg.IsBodyHtml = True
        Dim sSmtpServer As String = ""
        sSmtpServer = "smtp.gmail.com"
        Dim a As New SmtpClient()
        a.Host = sSmtpServer
        a.EnableSsl = True
        a.Send(Msg)
    End Sub
    ' This Method is used to render gridview control
    Public Function GetGridviewData(ByVal gv As GridView) As String
        Dim strBuilder As New StringBuilder()
        Dim strWriter As New StringWriter(strBuilder)
        Dim htw As New HtmlTextWriter(strWriter)
        gv.RenderControl(htw)
        Return strBuilder.ToString()
    End Function
    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
        ' Verifies that the control is rendered
    End Sub
End Class



Thursday, 9 May 2013

Send Mail Using Contact Us Form in Asp.Net

Design Page-:


<%@ 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 id="Head1" runat="server">
<title>Contact Us In Asp.net</title>
<style type="text/css">
.Button
{
background-color :#0066CC;
color: #FFFFFF;
font-weight: bold;
margin-right: 2px;
padding: 4px 20px 4px 21px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<fieldset style="height: 242px; width: 470px">
<legend>Contact Us</legend>
<table cellspacing="2" cellpadding="2" border="0">


<tr><td>Name:-</td><td><asp:TextBox ID="txtName" runat="server" /></td></tr>


<tr><td>Email:-</td><td><asp:TextBox ID="txtEmail" runat="server" /></td></tr>

<tr><td>Subject:-</td><td><asp:TextBox ID="txtSubject" runat="server" /></td></tr>

<tr><td valign="top">Query:-</td>

<td> <asp:TextBox ID="txtMessage" Rows="5" Columns="40" TextMode="MultiLine" runat="server"/></td></tr>
<tr>
<td><asp:button ID="btnSubmitt" Text="Submit"  runat="server" onclick="btn_Click" CssClass="Button"/></td></tr>
<tr><td colspan="2" style=" color:red"><asp:Label ID="lbltxt" runat="server"/></td></tr>
</table>
</fieldset>
</form>
</body>
</html>

Codebehind-:


using System;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btn_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage Msg = new MailMessage(); // Sender e-mail.
            Msg.From = new MailAddress(txtEmail.Text); // Recipient e-mail.
           
            Msg.To.Add("rahulupadhyay010@gmail.com");
            Msg.Subject = txtSubject.Text;
            Msg.Body = txtMessage.Text;
           
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
                             smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("gmailid@gmail.com", "password");
                                smtp.EnableSsl = true;
            smtp.Send(Msg);
         
            lbltxt.Text = "Thanks for Contacting ";
         
            txtName.Text = "";
            txtSubject.Text = "";
            txtMessage.Text = "";
            txtEmail.Text = "";
        }
        catch (Exception ex)
        {
            Console.WriteLine("{0} Exception caught.", ex);
        }    }}


Demo-: