[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



Post a Comment

Post a Comment (0)

Previous Post Next Post