HOW TO SEND MUTIPLE EMAILS AT ONCE IN GMAIL WITH ATTACHMENT IN ASP.NET C#

in programming •  6 years ago  (edited)

HERE IN THIS .Net Tutorial WE WILL SEND MUTIPLE EMAILS AT ONCE IN GMAIL WITH ATTACHMENT IN ASP.NET C#



Step 1:- WebForm2.aspx.cs


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication12.WebForm2" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body style="width: 588px; height: 186px">

    <form id="form1" runat="server">

        <div>

         <table style="width:100%;">

<tr>

<td class="auto-style1">

<asp:Label ID="Label1" runat="server" Text="Email Id :-"></asp:Label>

</td>

<td>

<asp:TextBox ID="TextBox1" runat="server" Width="450px"></asp:TextBox>

</td>

<td>&nbsp;</td>

</tr>

<tr>

<td class="auto-style1">

<asp:Label ID="Label4" runat="server">Subject :-</asp:Label>

</td>

<td>

<asp:TextBox ID="TextBox2" runat="server" Width="447px"></asp:TextBox>

</td>

<td>&nbsp;</td>

</tr>

<tr>

<td class="auto-style1">

<asp:Label ID="Label2" runat="server" Text="Text :-"></asp:Label>

</td>

<td>

<asp:TextBox ID="TextBox3" runat="server" Height="77px" TextMode="MultiLine" Width="446px"></asp:TextBox>

</td>

<td>&nbsp;</td>

</tr>

<tr>

<td class="auto-style1">

<asp:Label ID="Label3" runat="server" Text="Attachment :- "></asp:Label>

</td>

<td>

<asp:FileUpload ID="FileUpload1" runat="server" />

</td>

<td>&nbsp;</td>

</tr>

<tr>

<td class="auto-style1">&nbsp;</td>

<td>

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />

</td>

<td>&nbsp;</td>

</tr>

<tr>

<td class="auto-style1">&nbsp;</td>

<td>

<asp:Label ID="Label5" runat="server" ForeColor="#33CC33"></asp:Label>

</td>

<td>&nbsp;</td>

</tr>

</table>

        </div>

    </form>

</body>

</html>


Step 2 :- Create EmailModels In Side Folder Models


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;


namespace WebApplication12.Models

{

public class EmailModels

{

public int id { get; set; }


public string email { get; set; }

}

}


Step 3 :- Create SqlDB.cs In Side Folder Models For All Database Connection


using System;

using System.Collections;

using System.Collections.Generic;

using System.Data;

using System.Data.SqlClient;

using System.Linq;

using System.Web;


namespace WebApplication12.Models

{

public class SqlDB

{

public const string CONST_USER_ID = "";


public string connStr = "Data Source=.;Initial Catalog=email;Persist Security Info=True;User ID=sa;Password=abc@123";

private SqlTransaction sqlTrans;

private SqlConnection sqlConn = null;



public SqlDB()

{

this.connStr = "Data Source=.;Initial Catalog=email;Persist Security Info=True;User ID=sa;Password=abc@123";


}


public void BeginTransaction()

{

sqlTrans = dbConnection.BeginTransaction();

}


public void CommitTransaction()

{

sqlTrans.Commit();

}


public void RollbackTransaction()

{

sqlTrans.Rollback();

}


public void CloseConnection()

{

dbConnection.Close();

}


private SqlConnection dbConnection

{

get

{


if (sqlConn == null)

{

sqlConn = new SqlConnection();

try

{

if (sqlConn.State == ConnectionState.Open)

sqlConn.Close();


sqlConn.ConnectionString = connStr;


if (sqlConn.State == ConnectionState.Closed)

{

sqlConn.Open();

}

}

catch (Exception)

{


}

}


return sqlConn;

}

}


public SqlDataReader getSqlDataReader(string strQuery, ArrayList alParams)

{

try

{

using (var con = new SqlConnection(connStr))

{

if (con.State == ConnectionState.Closed)

{

con.Open();

}

SqlCommand objCommand = new SqlCommand();

objCommand = new SqlCommand(strQuery, con, sqlTrans);

objCommand.CommandTimeout = 0;

objCommand.CommandText = strQuery;


foreach (SqlParameter param in alParams)

{

objCommand.Parameters.Add(param);

}


SqlDataReader sqlDR = objCommand.ExecuteReader();

objCommand.Parameters.Clear();

return sqlDR;

}

}

catch

{

return null;

}


}

public DataTable getDataTable(string strQuery, ArrayList alParams)

{

try

{

using (var con = new SqlConnection(connStr))

{

if (con.State == ConnectionState.Closed)

{

con.Open();

}

SqlCommand objCommand = new SqlCommand();

objCommand = new SqlCommand(strQuery, con, sqlTrans);

objCommand.CommandTimeout = 0;

objCommand.CommandText = strQuery;

objCommand.Parameters.Clear();

foreach (SqlParameter param in alParams)

{

objCommand.Parameters.Add(param);

}


SqlDataAdapter daSQL = new SqlDataAdapter(objCommand);

DataTable dt = new DataTable();


daSQL.Fill(dt);

objCommand.Parameters.Clear();

return dt;

}

}

catch (Exception)

{

return null;

}


}


public string runExecuteQuery(string strQuery, ArrayList alParams)

{


try

{

using (var con = new SqlConnection(connStr))

{

if (con.State == ConnectionState.Closed)

{

con.Open();

}

SqlCommand objCommand = new SqlCommand(strQuery, con, sqlTrans);

objCommand.CommandText = strQuery;


foreach (SqlParameter param in alParams)

{

//if (String.IsNullOrEmpty(param.Value.ToString()))

//{

//    param.Value = DBNull.Value;

//    objCommand.Parameters.Add(param);

//}

//else

//{

//    objCommand.Parameters.Add(param);

//}

objCommand.Parameters.Add(param);


}


objCommand.ExecuteNonQuery();

objCommand.Parameters.Clear();

return "done";

}

}

catch (Exception ex)

{

return ex.Message;

}


}


//public DataTable getDataTable(string strQuery, ArrayList alParams)

//{

//    try

//    {

//        SqlCommand objCommand = new SqlCommand(strQuery, dbConnection, sqlTrans);

//        objCommand.CommandTimeout = 0;

//        objCommand.CommandText = strQuery;


//        foreach (SqlParameter param in alParams)

//        {

//            objCommand.Parameters.Add(param);

//        }


//        SqlDataAdapter daSQL = new SqlDataAdapter(objCommand);

//        DataTable dt = new DataTable();

//        daSQL.Fill(dt);

//        objCommand.Parameters.Clear();


//        return dt;

//    }

//    catch

//    {

//        return null;

//    }


//}



public string ExecuteStoreProcedure(string strQuery, ArrayList alParams)

{

try

{

using (var con = new SqlConnection(connStr))

{

if (con.State == ConnectionState.Closed)

{

con.Open();

}

SqlCommand objCommand = new SqlCommand(strQuery, con, sqlTrans);

objCommand.CommandText = strQuery;

objCommand.CommandType = CommandType.StoredProcedure;

objCommand.CommandTimeout = 0;

foreach (SqlParameter param in alParams)

{

objCommand.Parameters.Add(param);

}


objCommand.ExecuteNonQuery();


objCommand.Parameters.Clear();


return "done";

}


}

catch (Exception ex)

{

return ex.Message;

}

finally

{


}

}

}

}


Step 4 :- Create BusinessLogic.cs In Side Model Folder


using System;

using System.Collections;

using System.Collections.Generic;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using System.Linq;

using System.Web;


namespace WebApplication12.Models

{

public class BusinessLogic

{

private string strSql = "";

private ArrayList alSQLParams = new ArrayList();

private SqlDateTime sqldatenull = SqlDateTime.Null;


public List<EmailModels> getemailDetail()

{

alSQLParams.Clear();

List<EmailModels> emaildetail = new List<EmailModels>();

   strSql = "select * from emaillist with (nolock) order by id ";


DataTable dt = new SqlDB().getDataTable(strSql, alSQLParams);


if (dt != null)

{

for (int i = 0; dt.Rows.Count > i; i++)

{

EmailModels emailmenu = new EmailModels();

emailmenu.id = Convert.ToInt16(dt.Rows[i]["id"]);

emailmenu.email = dt.Rows[i]["email"].ToString();

emaildetail.Add(emailmenu);

}

}

return emaildetail;

}

}

}


Step 5:- C# Code


using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Net;

using System.Net.Mail;

using System.Text;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using WebApplication12.Models;


namespace WebApplication12

{

public partial class WebForm2 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{


}


protected void Button1_Click(object sender, EventArgs e)

{

var emailid = new BusinessLogic().getemailDetail();


foreach (var item in emailid)

{

using (MailMessage mm = new MailMessage(TextBox1.Text.ToString(), item.email.ToString()))

{

mm.Subject = TextBox2.Text.ToString();

mm.Body = TextBox3.ToString();


if (FileUpload1.HasFile)

{

string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

FileUpload1.SaveAs(Server.MapPath("Files/"+fileName));

mm.Attachments.Add(new Attachment(Server.MapPath("Files/" + fileName)));

}

mm.BodyEncoding = Encoding.UTF8;

mm.IsBodyHtml = true;


using (SmtpClient smtp = new SmtpClient())

{

smtp.Host = "smtp.gmail.com";

smtp.EnableSsl = true;

NetworkCredential NetWorkCred = new NetworkCredential("[email protected]","abc");

smtp.UseDefaultCredentials = true;

smtp.Credentials = NetWorkCred;

smtp.Port = 587;

smtp.Send(mm);

}

}

}


Label5.Text = "Email Send.........................";

}

}

}


 If You Haven't Subscribers My Channel Than Please Subscribers It And Even Share It Link Of
My Channnel :- https://www.youtube.com/channel/UCdIDIGUh3rL9A_Yt_tbZmkg Please Go And Subscribers It 

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

If You Haven't Subscribers My Channel Than Please Subscribers It And Even Share It Link Of
My Channnel :- https://www.youtube.com/channel/UCdIDIGUh3rL9A_Yt_tbZmkg Please Go And Subscribers It