Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System.Net;
using System.Net.Mail;
using System.IO;

 protected void btnSendMessage_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("SendEmail1", con);
        cmd.CommandType = CommandType.StoredProcedure;
     
        cmd.Parameters.AddWithValue("@oper","%@gmail%");
        SqlDataReader dr;
        try
        {
        con.Open();
        dr = cmd.ExecuteReader();
        while (dr.Read())
            {
                txtGmail.Text += dr["EmailId"].ToString() + ",";
                txtGmailuser.Text += dr["FirstName"].ToString() + ",";
            }
            dr.Close();
        }
        catch (Exception ex)
        {
            lblStatus.Text = ex.Message;
        }
        con.Close();
        cmd.Parameters.RemoveAt("@oper");
        cmd.Parameters.AddWithValue("@oper", "%@yahoo%");
        try
        {
            con.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                txtYahoo.Text += dr["EmailId"].ToString() + ",";
                txtyahoouser.Text += dr["FirstName"].ToString() + ",";
            }
            dr.Close();
        }
        catch (Exception ex)
        {
            lblStatus.Text = ex.Message;
        }
        con.Close();
        string[] gEmail = txtGmail.Text.Split(',');
        string[] gmailuser = txtGmailuser.Text.Split(',');
        int n = gEmail.Length-1;
        int i;
        for (i = 0; i < n; i++)
        {
            string bodygoogle = "Dear" + gmailuser[i]+"," + txtBody.Text ;
            MailMessage msGmail = new MailMessage("test@prayoglabs.com", gEmail[i], txtSubject.Text  ,bodygoogle );
            msGmail.IsBodyHtml = false;
            NetworkCredential nc = new NetworkCredential("test@prayoglabs.com", "prayoglabs@123");
            SmtpClient scGmail = new SmtpClient("mail.google.com", 587);
            scGmail.EnableSsl = true  ;
            scGmail.UseDefaultCredentials = false;
            scGmail.Credentials = nc;
            try
            {
                scGmail.Send(msGmail);
                lblStatus.Text = "sent successfully" + gmailuser[i];
            }
            catch (Exception ex)
            {
                lblStatus.Text += "not send to the user" + gmailuser[i] + "due to the" + ex.Message;
            }
        }
        string[] yahoomail = txtYahoo.Text.Split(',');
        string[] yahoouser = txtyahoouser.Text.Split(',');
        int m = yahoomail.Length - 1;
        int j = 0;
        for (j = 0; j < m; j++)
        {
            string bodyyahho = "dear" + yahoouser[j] + txtSubject;
            MailMessage msYahoo = new MailMessage("test@prayoglabs.com", yahoomail[j], txtSubject.Text, bodyyahho);
            msYahoo.IsBodyHtml = false;
            SmtpClient scyahoo = new SmtpClient("mail.yahoo.com", 465);
            scyahoo.EnableSsl = true;
            scyahoo.UseDefaultCredentials = false;
            NetworkCredential nc = new NetworkCredential("test@prayoglabs.com", "prayoglabs@123");
            scyahoo.Credentials = nc;
            try
            {
                scyahoo.Send(msYahoo);
                lblStatus.Text += "sent successfully to" + yahoomail[i];
            }
            catch (Exception ex)
            {
                lblStatus.Text = "not send to the user" + yahoomail[i] + "due to the" + ex.Message;
            }
        }
    }

XML
<body bgcolor="#ccccff">
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td class="style5">&nbsp;</td>
    <td>&nbsp;</td>
    <td class="style1">
        &nbsp;</td>
     <td>&nbsp;</td>

    </tr>
    </table>
    <table>
    <tr><td class="style6">Subject</td><td><asp:TextBox ID="txtSubject" runat="server"
            Width="391px"></asp:TextBox></td></tr>
    </table>
    <table>
    <tr>
    <td class="style3">Body</td><td>
        <asp:TextBox ID="txtBody" runat="server" Height="181px"
            Width="403px" TextMode="MultiLine" ></asp:TextBox></td>
            <td><asp:TextBox ID="txtGmail" runat="server" TextMode="MultiLine" Visible="false"></asp:TextBox>
                <br />
                <asp:TextBox ID="txtYahoo" runat="server" TextMode="MultiLine" Visible="False"
                    Width="179px"></asp:TextBox>
                <br />
                <asp:TextBox ID="txtGmailuser" runat="server" Height="35px"
                    TextMode="MultiLine" Width="176px" Wrap="False" Visible="False"></asp:TextBox>
                <br />
                <asp:TextBox ID="txtyahoouser" runat="server"
                    Width="174px" Visible="False"></asp:TextBox>
        </td>
    </tr>
    </table>
    <table>
    <tr>
    <td class="style2"></td>
    <td><asp:Button ID="btnSendMessage" Text="Send Messages" runat="server"
            onclick="btnSendMessage_Click"/></td>
    </tr>
    </table>
    </div>
    <asp:Label ID="lblStatus" runat="server" Text="Label" ForeColor="#FF3300"></asp:Label>
    </form>
</body>
Posted
Updated 2-Jun-11 10:01am
v2
Comments
fjdiewornncalwe 2-Jun-11 16:03pm    
This really looks like a tip/trick and not a question.
If you have a question, or you have an issue with this code, what is it?

A simple code dump, with no indication of what problems you are having, where you think the errors lies and with nothing to indicate what approaches you have taken to solve the problem will not result in much by way of help.

Provide more information and people will be more likely to help.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900