You must Sign In to post a response.
  • Category: .NET

    How To Send Mail to Selected mail addresses

    Hello Developers,

    I am having 100 mail id's on my database table. if i select 50 mail id's na the mail will send to 50 mail is's. I am tried but i did not get exactly output.So please help me for i am done this operation.
    Give me ideas or refer some link for me.
    thanking you,

    Paul.S
  • #762980

    Hello Paul Raj,

    Refer the below code :

    HTML Markup :

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server" CellPadding="4"
    Font-Names="Segoe UI" ForeColor="#333333">
    <RowStyle BackColor="#EFF3FB" />
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="True" />
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#2461BF" />
    <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
    <br />
    <asp:Button ID="ButtonSendMail" runat="server" Text="Send Bulk Mails"
    BorderStyle="Solid" Font-Names="Segoe UI" onclick="ButtonSendMail_Click"
    BorderWidth="2px" />
    </div>
    </form>
    </body>
    </html>


    Code - behind :

    using System.Data;
    using System.Data.SqlClient;
    using System.Net;
    using System.Net.Mail;

    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
    SqlCommand cmd;
    string empid, empname, designation, email;

    public void MsgBox(String ex, Page pg, Object obj) //Dynamic message box
    {
    string s = "<SCRIPT language='javascript'>alert('" + ex.Replace("\r\n", "\\n").Replace("'", "") + "'); </SCRIPT>";
    Type cstype = obj.GetType();
    ClientScriptManager cs = pg.ClientScript;
    cs.RegisterClientScriptBlock(cstype, s, s.ToString());
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    try
    {
    DataTable dt = new DataTable();
    con.Open();
    SqlDataAdapter sda = new SqlDataAdapter("SELECT EmpID AS 'Emp ID', EmpName AS 'Emp Name', Designation, Email FROM EmpDetails", con);
    sda.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }
    catch (Exception ex)
    {
    Response.Write(ex.Message);
    }
    }
    }

    public void SendMail()
    {
    SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
    smtpClient.Port = 587;
    smtpClient.Credentials = new NetworkCredential("sender@gmail.com", "password");
    smtpClient.EnableSsl = true;

    MailMessage mailmesssage = new MailMessage();
    mailmesssage.From = new MailAddress("sender@gmail.com", "Paul Raj");
    mailmesssage.To.Add(email);

    mailmesssage.Subject = "Test Mail";
    TextBox TxtEmail = new TextBox();
    TxtEmail.Text = "Please verify your personal details and revert to this email if any mistake is there." + Environment.NewLine + Environment.NewLine;
    TxtEmail.Text += "Employee ID: " + empid + Environment.NewLine;
    TxtEmail.Text += "Employee Name: " + empname + Environment.NewLine;
    TxtEmail.Text += "Designation: " + designation + Environment.NewLine;
    TxtEmail.Text += "Email ID: " + email + Environment.NewLine;

    mailmesssage.Body = TxtEmail.Text;
    try
    {
    smtpClient.Send(mailmesssage);
    MsgBox("Mail sent successfully to " + email + ".", this.Page, this);
    }
    catch (Exception ex)
    {
    MsgBox("An error occured." + Environment.NewLine + "Error details: " + ex.Message + "", this.Page, this);
    }
    }

    protected void ButtonSendMail_Click(object sender, EventArgs e)
    {
    int i = 0;
    foreach (GridViewRow row in GridView1.Rows)
    {
    if ((row.FindControl("CheckBox1") as CheckBox).Checked)
    {
    empid = GridView1.Rows[i].Cells[1].Text.ToString();
    empname = GridView1.Rows[i].Cells[2].Text.ToString();
    designation = GridView1.Rows[i].Cells[3].Text.ToString();
    email = GridView1.Rows[i].Cells[4].Text.ToString();
    SendMail();
    }
    i++;
    }
    }


    See the attached image. It's an output i have got using above mentioned code.


    Hope this is what you are looking for.


    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment

  • #762982

    Hi,

    Please check below code and the logic I have used to send mail for multiple people.

    I am not sure where you have saved the email id. If it is in the database then you just need to loop through the email id to send the email.
    Let me know if you need any assistance in this regard.

    string[] strEmail =
    {
    "first@yahoo.com",
    "second@gmail.com",
    "third@hotmail.com"
    "fourth@hotmail.com"
    };

    foreach (string value in strEmail)
    {

    SendEmail(string value)
    }

    private void SendEmail(string emailID)
    {
    string smtpServer = System.Configuration.ConfigurationSettings.AppSettings["SMTPServer"].ToString();
    string fromEmailAddress = System.Configuration.ConfigurationSettings.AppSettings["FromEmailAddress"].ToString();
    string toEmailAddress = System.Configuration.ConfigurationSettings.AppSettings["ToEmailAddress"].ToString();
    MailMessage EMail = new MailMessage();
    EMail.From = fromEmailAddress;
    EMail.To = emailID;
    EMail.Subject = strsubject;
    EMail.Body = "<html><head></head><body>"+body+" <br><br><hr length='100%'><br>*******This is an auto generated mail. Please do not reply to this message.******</body></html>";
    EMail.BodyFormat = System.Web.Mail.MailFormat.Html;
    SmtpMail.SmtpServer = smtpServer;
    EMail.BodyFormat = System.Web.Mail.MailFormat.Html;
    try
    {
    SmtpMail.Send (EMail);
    }
    catch(Exception ex)
    {

    }



    Regards,
    Asheej T K
    Microsoft MVP[ASP.NET/IIS]
    DotNetSpider MVM

  • #762990
    Thank you very much mr.Lalan .
    I will try and i will reply you .

    regards with
    Paul.S

  • #762993
    Hi,

    check This It helps you.....this link having example

    http://www.aspsnippets.com/Articles/Send-mail-to-multiple-Users-Recipients-in-ASPNet-using-C-and-VBNet.aspx


    Regards,
    Ajit More

  • #762995
    Hello Ajit More,

    Instead of providing external links you should provide the sample code or the way how can they resolve their problem. That will be much better.

    If you will provide external links they will redirect to that links and will not help DNS.


    Hope you understand.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762998
    Hi,

    Check the following links, for Sending Mail to Selected mail addresses

    http://jqueryasp.net/send-mail-to-multiple-users-recipients-in-asp-net/
    http://www.c-sharpcorner.com/UploadFile/0c1bb2/sending-email-to-multiple-recipeint-using-Asp-Net/
    http://www.tugberkugurlu.com/archive/sending-e-mail-to-all-list-of-membership-users-with-asp-net-using-built-in-membership-api
    http://community.sitepoint.com/t/help-sending-email-using-asp-net-and-c-to-multiple-recipients/6053

    Thanks & Regards,
    Mahesh Durgam

  • #763005
    Thanks for your all valuable reply friends.

    Paul.S

  • #763006
    Hello Paul Raj,

    The solution i have provided you. Did it work for you??

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #763065
    An very Sorry for my delay reply Mr.Lalan . bcoz afetr that ny last post, now only am login in dotnet spider.

    yes Working fine , but in local mail is not sent.

    An error occured.
    Error details: Server does not support secure connections.

    i thing this is may be in smtpclient,or mail address, some any problem

    then if i hosted it on live i must reply you Mr.Lalan.

    Thanking with

    Paul.S

  • #763067
    Hello Paul Raj,

    The code i have provided you which is working fine on website which is hosted on bigrocks.com

    This code will work for you sure.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #763069
    yes Mr.Lalan the code which you provided to me working Sure.

    Thanks with
    Paul.S


Sign In to post your comments