Send mail in c#

In this section we are showing how to send mail using ASP.NET. Follow this process,

Create your webform application in visual studio and add a webform page and named it. Then add this following code in your .aspx view page.

<table style="width:100%">
    <tr>
        <td style="text-align: center">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Panel ID="Panel3" runat="server" Width="100%" style="font-weight: 700">
    <br />
    <br />
    To:&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
    <asp:DropDownList ID="DropDownList1" runat="server" CssClass="modaltext" DataSourceID="SqlDataSource1" DataTextField="email_id" DataValueField="email_id" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
    </asp:DropDownList>
    <br />
    No Of Recipes Added:
    <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    <br />
    &nbsp;Message:<br /> &nbsp;<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:cookingConnectionString %>" SelectCommand="SELECT DISTINCT [email_id] FROM [login]"></asp:SqlDataSource>
&nbsp;<asp:TextBox ID="txt_cmnt0" runat="server" Height="160px" TextMode="MultiLine"  CssClass="modaltext"></asp:TextBox>
    <br />
    <asp:Button ID="Button6" runat="server" Text="Send"  CssClass="modalupdate" OnClick="Button6_Click" />
    &nbsp;&nbsp;
    <br />
    <br />
    <br />
    </asp:Panel>




        </td>
    </tr>
</table>


After that add following coding in .aspx.cs page for sending mail.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class admin_mail_to_user : System.Web.UI.UserControl
{
    SqlConnection cn = new SqlConnection(WebConfigurationManager.ConnectionStrings["cook"].ToString());
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {
        cn.Open();
        if (!IsPostBack)
        {
            count();
        }
    }
    public void sendmail()
    {
        try
        {
            MailAddress mailfrom = new MailAddress("write your e-mail id");
            MailAddress mailto = new MailAddress(DropDownList1.Text);
            MailMessage newmsg = new MailMessage(mailfrom, mailto);
            newmsg.Subject = "Message";
            newmsg.Body =txt_cmnt0.Text;
            SmtpClient smtps = new SmtpClient("mail.myrecipesdotcom.in", 25);
            smtps.UseDefaultCredentials = true;
            smtps.Credentials = new NetworkCredential("write your e-mail id", "password");
            smtps.EnableSsl = false;
            smtps.Send(newmsg);
        }
        catch (Exception e1)
        {
            Response.Write("<script>alert('" + e1.Message + "')</script>");
        }
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        sendmail();
       
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        count();
    }

    private void count()
    {
        try
        {
            SqlCommand cm = new SqlCommand("select count(*) from recipes where email_id='" + DropDownList1.Text + "'", cn);
            int s = Convert.ToInt32(cm.ExecuteScalar());
            SqlCommand cm1 = new SqlCommand("select count(*) from ingridients where email_id='" + DropDownList1.Text + "'", cn);
            int s1 = Convert.ToInt32(cm1.ExecuteScalar());
            Literal1.Text = (s + s1).ToString();
        }
        catch (Exception e1)
        {
            Response.Write("<script>alert('" + e1.Message + "')</script>");
        }
    }
}


Post a Comment

0 Comments