Solved

C# code example where an email is sent to multiple recipients where the email address of the recipients comes from a stored proc

Posted on 2014-11-12
5
52 Views
Last Modified: 2016-06-15
I have an applicaion where the  an email is sent out to multiple recipients of a course when somthing about that course  has changed. I have a stored proc that gets the email address based off of the course id. I then send the email out reflecting the changes made to the course in an html formatted email.  The issue that I am running into is trying to get the email to go out to the multiple recipients  instead of the  last attended's email address found from the stored proc.
My code is listed below:
//Send email of course cancellation
                        StoredProcConnect sprocRoster = new StoredProcConnect("sp_GetRosterByCourseID");
                        sprocRoster.AddParameters("COURSEID");
                        sprocRoster.AddValues(id);
                        DataSet ds = sprocRoster.Select();
                        if (ds == null)
                            Response.Redirect("ErrorMsg.aspx?error=Problem Getting Roster Info.  Tech team has been notified, please try again");

                                MailAddressCollection coll = new MailAddressCollection();                     
   if (ds.Tables[0].Rows.Count > 0)
                        {
                               string To = string.Empty;
                            foreach (DataRow row in ds.Tables[0].Rows)
                            {
                                msg.To.Add(row["email"].ToString() + "@COMPANY.com");
                                To = (row["email"].ToString() + "@COMPANY.com");
                            }

                            //toAddress = new Array(To);

                            coll.Add(To);

                            msg.From = new MailAddress("ClassChange@allstate.com");
                            msg.Subject = "An update to " + courseName + " has been made!";

                            // SendEmail();
                            if (chkCourseName.Checked == true)
                            {
                              var Body = this.PopulateBody(courseName,
                              courseName + "  Has been changed!",
                              "A new entry will be sent to your Outlook Calendar."
                              + "Please remove the old entry and accept the new entry." + name, "Sorry for any inconvenience that this change may have occured. " + "<br/>" + lblPresenter + "<br/>");
                              this.SendHtmlFormattedEmail(To, "An update to " + courseName + " has been made!", Body);
                            }
                         }

private string PopulateBody(string courseName, string title, string url, string description)
        {
            string body = string.Empty;
            using (StreamReader reader = new StreamReader(Server.MapPath("~/HtmlPage1.html")))
            {
                body = reader.ReadToEnd();
            }
            body = body.Replace("{UserName}", courseName);
            body = body.Replace("{Title}", title);
            body = body.Replace("{Url}", url);
            body = body.Replace("{Description}", description);
            return body;
        }

        private void SendHtmlFormattedEmail(string recepientEmail, string subject, string Body)
        {
            using (MailMessage mailMessage = new MailMessage())
            {
                mailMessage.From = new MailAddress("Responses@COMPANY.com");
                mailMessage.Subject = subject;
                mailMessage.Body = Body;
                mailMessage.IsBodyHtml = true;
                mailMessage.To.Add(new MailAddress(recepientEmail));
                Smtp info goes here
            }
        }

Open in new window

0
Comment
Question by:newjeep19
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 81

Expert Comment

by:David Johnson, CD, MVP
ID: 40438610
do you want to send 1 email with BCC or a bunch of individual emails?

get your collection from the stored procedure into an array
one email for each returned address
 for (int counter=0; counter < array.count; counter++)
{
email.address = array[counter]
// rest of email
// email send routine
}
BCC
 for (int counter=0; counter < array.count; counter++) { bcc.address = bccaddress + "," +array[counter] }
// rest of email
// email send routine
0
 
LVL 6

Expert Comment

by:Mandeep Singh
ID: 40439149
0
 
LVL 5

Accepted Solution

by:
ashokpumca earned 500 total points
ID: 40463822
Hi,
Try the code below

                            SqlDataAdapter adpSamp = new SqlDataAdapter("Your Stored Procduere", con);
                            adpSamp.SelectCommand.CommandType = CommandType.StoredProcedure;
                            DataSet dsSamp = new DataSet();
                            adpSamp.Fill(dsSamp);
                            if (dsSamp.Tables[0].Rows.Count > 0)
                            {
                                MailMessage mail = new MailMessage();

                                for (int j = 0; j < dsSamp.Tables[0].Rows.Count; j++)
                                {
                                   
                                    String email = (dsIssue.Tables[0].Rows[j]["emailid"]).ToString();

                                    mail.To.Add(email);
                                    mail.Subject = "Mail Subject";
                                    mail.From = new MailAddress("Email ID of sender");

                                    mail.IsBodyHtml = true;
                                    mail.Priority = MailPriority.High;
                                    mail.Body = Your HTML Mail Format

                                    SmtpClient smtp = new SmtpClient("YOUR SMTP CLIENT");
                                    try
                                    {
                                        smtp.Send(mail);
                                    }
                                    catch (Exception ex)
                                    {
                                        LblMesg.Text = ex.Message.ToString();
                                    }
                                }
                            }
0
 

Author Comment

by:newjeep19
ID: 40547441
worked
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question