[Source Code ] Asp.net code to send email (send mail with cc in asp.net)

Introduction

Asp.net code to send email


In this tutorial, I will show "Asp.net code to send email". The whole process will be done in the following steps. We will also see "how to send mail with CC in asp.net c#".


how to send mail with cc in asp.net, how to send simple mail in asp.net
Asp.net code to send email


1. Create an object of MailAddress class(For CC).
2. Create an object of MailMessage class(For Message).
3. Create an object of SmtpClient class.


how to send mail with CC in asp.net c#
send mail with CC in asp.net c#


.Aspx Page

<table>
        <tr>
            <td>From Email ID :</td>
            <td><asp:TextBox ID="txtfromemail" runat="server" Width="250px"></asp:TextBox></td>
        </tr>
          <tr>
            <td>Password :</td>
            <td><asp:TextBox ID="txtpassword" runat="server" Width="250px" TextMode="Password"></asp:TextBox></td>
        </tr>
        <tr>
            <td>To Email ID :</td>
            <td><asp:TextBox ID="txttoemail" runat="server" Width="250px"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Subject :</td>
            <td><asp:TextBox ID="txtsubject" runat="server" Width="250px"></asp:TextBox></td>
        </tr>
        <tr>
            <td style="vertical-align:top">Body :</td>
            <td><asp:TextBox ID="txtbody" runat="server" TextMode="MultiLine" Rows="8" Columns="50"></asp:TextBox> </td>
        </tr>
      
        <tr>
            <td></td>
            <td><asp:Button ID="btn_SendMail" Text="Mail Send" runat="server" OnClick="btn_SendMail_Click" /></td>
        </tr>
    </table>


.Cs page Source Code


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

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

        }

        protected void btn_SendMail_Click(object sender, EventArgs e)
        {
            MailAddress bcc = new MailAddress("[email protected]");
            using (MailMessage mm = new MailMessage(txtfromemail.Text, txttoemail.Text))
            {
                mm.Subject = txtsubject.Text;
                mm.Body = txtbody.Text;
                mm.CC.Add(bcc);
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential(txtfromemail.Text, txtpassword.Text);
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 25;
                smtp.Send(mm);
                ClientScript.RegisterStartupScript(GetType(), "alert""alert('Email sent.');"true);
            }
        }
    }
}


After done all these steps, may you face a problem like this "Authentication the Server Response was 5.5.1 Authentication required in Gmail"? In this case, follow the given steps.


1. Login to your Gmail account.
2. Visit this page https://accounts.google.com/DisplayUnlockCaptcha and click on button to allow access.
3. Visit this page https://www.google.com/settings/security/lesssecureapps and enable access for less secure apps.


Read our previous post



Read More

How to bind xml data to dropdownlist in asp.net - asp net dropdownlist xmldatasource

Asp.net dropdownlist xmldatasource: In this tutorial, I am going to show you "how to bind XML data to dropdownlist in asp.net"Mainly we fetch data from Database but sometimes as per requirement, we want to fetch data from other data source like XML(xmldatasource). Don't worry I am here to tell you how you can bind asp.net dropdown list from the XML file.

How to bind xml data to dropdownlist in asp.net - asp net dropdownlist xmldatasource
asp net dropdownlist xmldatasource


Steps to read XML file by tag name and BInd it with asp.net gridview
1. Add XML file (Visual C#).
2. Insert Data in the XML file with a specific tag. (See Below)
3. Give Server.MapPath correct.
4. Bind the dropdown with DataValueField and DataTextField.

Step 1:

Right Click on the project
-->Add
    -->Add New item
      -->Find XML File with Visual C# (See In Picture)


How to bind xml data to dropdownlist in asp.net, xml drop down
"XML drop down"


Countries.xml  (File Name)

<?xml version="1.0" encoding="utf-8" ?>
<countries>

  <country>
    <cid>1</cid>
    <cname>India</cname>
  </country>

  <country>
    <cid>2</cid>
    <cname>Pakistaan</cname>
  </country>

  <country>
    <cid>3</cid>
    <cname>USA</cname>
  </country>

  <country>
    <cid>4</cid>
    <cname>UK</cname>
  </country>

  <country>
    <cid>4</cid>
    <cname>Nepal</cname>
  </country>

</countries>




ASPX Form


<table>
        <tr>
            <td>Country :</td>
            <td>
                <asp:DropDownList ID="ddlcountry" runat="server"></asp:DropDownList></td>
        </tr>
 </table>

Code Behind File (Cs page)

       protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Fill_Country();
            }
        }
        public void Fill_Country()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("Countries.xml"));
            ddlcountry.DataValueField = "cid";
            ddlcountry.DataTextField = "cname";
            ddlcountry.DataSource = ds;
            ddlcountry.DataBind();
            ddlcountry.Items.Insert(0, new ListItem("--select--", "0"));
        }

 Thanks for reading. If you have any doubt please feel free to comment below.
Read More