This is my C# code, it works in that it creates a new table called Users
and uses the stored procedure to get values from that table, then sends an email to all users in the table. I am new to c# and need to know how to change this code to get data from an "existing" table called Users
, instead of creating it and getting information from the newly created table.
public partial class emailgroup : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SendBulkEmail(object sender, EventArgs e)
{
DataTable dtUsers = new DataTable();
string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("Users_GetEmails", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dtUsers);
string subject= txtSubject.Text; // Text in Subject
string body = txtBody.Text; // Text in Message Body
//Using Parallel Multi-Threading send multiple bulk email.
Parallel.ForEach(dtUsers.AsEnumerable(), row =>
{
SendEmail(row["Email"].ToString(), subject, string.Format(body, row["UserName"]));
});
{
Response.Redirect("login.aspx");
}
}
}
}
}
private bool SendEmail(string recipient, string subject, string body)
{
MailMessage mm = new MailMessage("gmailusername", recipient); //(put in gmail username between the "")
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "gmailusername"; //gmailusername (put in "")
NetworkCred.Password = "gmailpassword"; //gmailpassword (put in "")
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
return true;
}
protected void txtBody_TextChanged(object sender, EventArgs e)
{
}
}
This is my stored procedure.
-- Users_GetEmails
CREATE PROCEDURE Users_GetEmails
AS
BEGIN
SET NOCOUNT ON;
SELECT UserName, Email FROM Users
END
The below doesnt create new users table. It is creating new datatable and fetch info from database via "Users_GetEmails". Then it fills that datatable. You are sending Email with that information from that datatable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.