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;
}
}
}
<body bgcolor="#ccccff">
<form id="form1" runat="server">
<div>
<table>
<tr>
<td class="style5"> </td>
<td> </td>
<td class="style1">
</td>
<td> </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>