i am implementing send subject and message as a mail using radobuttonlist.
i am getting the output but only thing is email receives at multiple times.
may be it happens because of for loop.how to overcome with this?
Table barter_message:
msg_id Subject message sender sent_to date
1 xyz xyzz 50 Admins only 19/07/2018
2 kk kkk 50 Users only 19/07/2018
3 hh tt 50 All 19/07/2018
4...
5...
Table barter_msg_sent:
s_mid msg_id user_id read
1 1 1 0
2 2 13 1
3 3 24 1
4 4 26 1
5..
6..
Table barter_userlog
user_id username password designation email_id usertype status
1 mahi *** xyz mahi@expogr.com 1 1
2 varun *** ddd varun@expogp.com 0 1
3 chetan *** ddds chetan@expogp.com 1 1
4..
5..
protected void Button1_Click(object sender, EventArgs e)
{
Int32 currLogUserId = Convert.ToInt32(Session["session_barterUser_id"]);
int ccToUserID = currLogUserId;
if (Session["session_barterUser_id"] == null)
{
Response.Redirect("~/Login.aspx");
}
else
{
if (RadioButton1.Checked != true && RadioButton2.Checked != true && RadioButton3.Checked != true && RadioButton4.Checked!=true)
{
messagetosend.Text = "Check any Sender type!!";
return;
}
else
{
messagetosend.Text = "";
MySqlConnection connect = null;
string sent_to = "",readQury="";
string today = System.DateTime.Today.ToString("yyyy/MM/dd");
int user_id = Convert.ToInt32(Session["session_barterUser_id"]);
if (RadioButton1.Checked)
{
sent_to = "Admins Only";
}
else if (RadioButton2.Checked)
{
sent_to = "Users Only";
}
else if(RadioButton3.Checked)
{
sent_to = "All";
}
else if(RadioButton4.Checked)
{
sent_to = "All as a mail";
}
try
{
string connStr = ConfigurationManager.ConnectionStrings["BarterConnectionString"].ToString();
connect = new MySqlConnection(connStr);
string queryStr = "insert into barter_message(subject,message,sender,sent_to,date) values(?subject,?message,?sender,?sent_to,?date)";
connect.Open();
int i;
MySqlCommand command = new MySqlCommand(queryStr, connect);
command.Parameters.AddWithValue("?subject", txtsubj.Text);
command.Parameters.AddWithValue("?message", txtmessage.Text);
command.Parameters.AddWithValue("?sender", user_id);
command.Parameters.AddWithValue("?sent_to", sent_to);
command.Parameters.AddWithValue("?date", today);
command.ExecuteNonQuery();
command.CommandText = "Select LAST_INSERT_ID()";
string MID = command.ExecuteScalar().ToString();
string queryStrUserType = "SELECT user_id,usertype,email_id FROM barter_userlog ";
ClassDtBaseConnect clsDtResult = new ClassDtBaseConnect();
DataTable dt = clsDtResult.GetDataTable(queryStrUserType);
for ( i = 0; i <= dt.Rows.Count; i++)
{
if (sent_to =="Admins Only")
{
if (Convert.ToInt32(dt.Rows[i]["usertype"]) == 1)
{
readQury = "insert into barter_msg_sent(`msg_id`,`user_id`,`read`) values(?msgid,?uid,?read)";
MySqlCommand command2 = new MySqlCommand(readQury, connect);
command2.Parameters.AddWithValue("?msgid", MID);
command2.Parameters.AddWithValue("?uid", dt.Rows[i]["user_id"].ToString());
command2.Parameters.AddWithValue("?read", 0);
command2.ExecuteNonQuery();
}
}
else if (sent_to == "Users Only")
{
if (Convert.ToInt32(dt.Rows[i]["usertype"]) == 0)
{
readQury = "insert into barter_msg_sent(`msg_id`,`user_id`,`read`) values(?msgid,?uid,?read)";
MySqlCommand command2 = new MySqlCommand(readQury, connect);
command2.Parameters.AddWithValue("?msgid", MID);
command2.Parameters.AddWithValue("?uid", dt.Rows[i]["user_id"].ToString());
command2.Parameters.AddWithValue("?read", 0);
command2.ExecuteNonQuery();
string adm = dt.Rows[i]["user_id"].ToString();//testing
string ema = dt.Rows[i]["email_id"].ToString();//testing
SendMail sndMail = new SendMail();//testing
}
}
else if (sent_to == "All")
{
readQury = "insert into barter_msg_sent(`msg_id`,`user_id`,`read`) values(?msgid,?uid,?read)";
MySqlCommand command2 = new MySqlCommand(readQury, connect);
command2.Parameters.AddWithValue("?msgid", MID);
command2.Parameters.AddWithValue("?uid", dt.Rows[i]["user_id"].ToString());
command2.Parameters.AddWithValue("?read", 0);
command2.ExecuteNonQuery();
}
else if(sent_to=="All as a mail")
{
//ClassDtBaseConnect clsDtResultt = new ClassDtBaseConnect();
//DataTable dt2 = clsDtResultt.GetDataTable("SELECT email_id FROM barter_userlog WHERE user_id=" + user_id + "");
//string admin = dt2.Rows[0]["email"].ToString();
//readQury = "insert into barter_msg_sent(`msg_id`,`user_id`,`read`) values(?msgid,?uid,?read)";
//MySqlCommand command2 = new MySqlCommand(readQury, connect);
//command2.Parameters.AddWithValue("?msgid", MID);
//command2.Parameters.AddWithValue("?uid", dt.Rows[i]["user_id"].ToString());
//command2.Parameters.AddWithValue("?read", 0);
//command2.ExecuteNonQuery();
string message;
readQury = "insert into barter_msg_sent(`msg_id`,`user_id`,`read`) values(?msgid,?uid,?read)";
MySqlCommand command2 = new MySqlCommand(readQury, connect);
command2.Parameters.AddWithValue("?msgid", MID);
command2.Parameters.AddWithValue("?uid", dt.Rows[i]["user_id"].ToString());
command2.Parameters.AddWithValue("?read", 0);
command2.ExecuteNonQuery();
// string adm = dt.Rows[i]["user_id"].ToString();//testing
string ema = dt.Rows[i]["email_id"].ToString();//email
ClassDtBaseConnect clsDtResultt = new ClassDtBaseConnect();
DataTable dt2 = clsDtResultt.GetDataTable("SELECT email_id FROM barter_userlog WHERE user_id=" + user_id + "");
string admin = dt2.Rows[0]["email_id"].ToString();
message = "";
message = message + "<b>Hi,<br><br>Everyone," + " <br><br>";
message = message + "Subject =" + txtsubj.Text + "<br> " + "Message =" + txtmessage.Text + "<br>";
message = message + "<br><br>" + "Thanks and Regards. </b>";
SendMail sndMail = new SendMail();//testing
sndMail.sendMailAndCC("inq@ogpeafrica.com", admin, ema, message, "Barter Management", "BMS - Barter Message");
}
}
connect.Close();
Response.Write("<script type=\"text/javascript\">alert('Message Sent Successfully!!!');</script>");
}
catch(Exception ex)
{
connect.Close();
}
finally
{
connect.Close();
}
}
}
}
SendMail.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.Net.Mime;
/// <summary>
/// Summary description for SendMail
/// </summary>
public class SendMail
{
public SendMail()
{
//
// TODO: Add constructor logic here
//
}
public MailMessage Mailbodplain(string fromad, string toad, string toCC, string body, string header, string subjectcontent)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
try
{
string from = fromad;
string to = toad;
string toC = toCC;
mail.To.Add(to);
mail.CC.Add("mahi@expogr.com");
mail.CC.Add("chetan@expogp.com");
mail.CC.Add("royston@expogp.com");
// mail.CC.Add("tyson@expogp.com");
// mail.CC.Add("swati@expogp.com");
// mail.CC.Add("rachel.d@expogr.com");
// mail.CC.Add(" ranjita@expogp.com");
// mail.CC.Add("akshata@expogp.com");
// mail.CC.Add("rupa@expogp.com");
// mail.CC.Add(toC);
mail.From = new MailAddress(from, header, System.Text.Encoding.UTF8);
mail.Subject = subjectcontent;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = body;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.Normal;
}
catch (Exception ex)
{
throw;
}
return mail;
}
public void sendMailAndCC(string fromad, string toad, string toCC, string body, string header, string subjectcontent)
{
MailMessage usermail = Mailbodplain(fromad, toad, toad, body, header, subjectcontent);
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("inq@ogpeafrica.com", "expo2015");
client.Host = "mail.ogpeafrica.com";
client.Port = 25;
client.EnableSsl = false;
try
{
client.Send(usermail);
}
catch (Exception ex)
{
}
}
}