Mass Email and Stringbuilder

Discussion in 'ASP .Net' started by =?Utf-8?B?SSBhbSBTYW0=?=, Apr 24, 2005.

  1. Ok I don't know what is the problem with my code But I am trying to build a
    newsletter that gathers parameters from 3 textbox controls and a Listbox
    control. The form then queries the event table based on 2 of the textboxes
    one for the start date of the events required and one for the last date of
    the events required. The 3rd textbox works fine. It passes the text from the
    textbox to the stringbuilder I built on the codebehind just fine. As well the
    Listbox works as expected. The problem I am having is with the Events table
    described earlier.

    I set an event to iterate through the rows in the Event Table and stuff the
    information into a String. This string is then appended to the StringBuilder
    to add to the email message.

    The Problem:

    WHen the I iterate through the Event Table in the dataset it doesn't stop
    reading one row when there is one row to read. It keeps reading the same row
    over and over again. I can't explain it. I think it repeatadly loops
    through the data for every email address table and stuffs the information
    into one email.

    Below is the Codebehind:

    public class EventBroadcast : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Table EBroadcast;
    protected System.Web.UI.WebControls.LinkButton ImageAttach;
    protected System.Web.UI.WebControls.ListBox IMGList;
    protected System.Web.UI.WebControls.TextBox StartDate;
    protected System.Web.UI.WebControls.TextBox Message;
    protected System.Web.UI.WebControls.TextBox EndDate;

    // Event Gathering.
    protected System.Data.SqlClient.SqlConnection clubconn;
    protected System.Data.SqlClient.SqlDataAdapter clubadapt;
    protected System.Data.DataSet dsClub;
    protected System.Text.StringBuilder MailString;
    protected System.Web.Mail.MailMessage MailSite;
    protected System.Data.SqlClient.SqlCommand clubcmd;
    protected System.Data.SqlClient.SqlDataReader clubDR;

    protected System.Data.SqlClient.SqlDataAdapter clubadapt2;
    protected System.Data.DataSet dsClub2;
    string strEventRelease;

    public string strclubconn
    {
    get{return ConfigurationSettings.AppSettings["ConnectionString"];}
    }
    public string EMessage
    {
    get{return Message.Text.ToString().Replace("\n","<br>");}
    }
    public string stringpath
    {
    get{return Server.MapPath(@"..\BroadcastIMG\");}
    }
    private void Page_Load(object sender, System.EventArgs e)
    {
    ImageAttach.Attributes.Add("onClick","javascript:window.open('../Secured/EmailIMG.aspx','imgWindow','width=700, height=275');");
    if(!Page.IsPostBack)
    {
    GetDirPath(stringpath);
    }
    }
    public void GetDirPath(string path)
    {
    string[] ImgList=Directory.GetFiles(path);
    foreach(string Filename in ImgList)
    {
    string filename=Path.GetFileName(Filename);
    IMGList.Items.Add(filename);
    IMGList.DataValueField=path + filename;
    IMGList.SelectedIndex=-1;
    }
    }
    protected void Button_Click(object sender, System.EventArgs e)
    {
    DateTime StDate=System.Convert.ToDateTime(StartDate.Text);
    DateTime EnDate=System.Convert.ToDateTime(EndDate.Text);
    BindData(StDate,EnDate);

    string strEmailSelect="SELECT Test.* FROM Test";
    clubconn=new SqlConnection(strclubconn);
    clubconn.Open();
    dsClub2=new DataSet();
    clubadapt2=new SqlDataAdapter(strEmailSelect,clubconn);
    clubadapt2.Fill(dsClub2,"Email");
    clubconn.Close();
    Session["MailBody"]=MailString.ToString();
    foreach(DataRow dr1 in dsClub2.Tables["Email"].Rows)
    {
    BindData(StDate,EnDate);
    MailSite=new MailMessage();
    MailSite.From="";
    MailSite.To=dr1["Email"].ToString();
    MailSite.Cc="";
    MailSite.Subject="News Release";
    MailSite.BodyFormat=MailFormat.Html;
    MailSite.Body=MailString.ToString();
    SmtpMail.SmtpServer="smtp.paconline.net";
    SmtpMail.Send(MailSite);
    }
    Response.Redirect("../Secured/MassConfirm.aspx");
    }
    public string EventList(System.DateTime SDate, System.DateTime EDate)
    {
    string strEventSelect="SELECT EVNR_tbl.* FROM EVNR_tbl, img_tbl WHERE
    EVNR_tbl._Date BETWEEN '" + SDate.ToString("yyyy/MM/dd") + "' AND '" +
    EDate.ToString("yyyy/MM/dd") + "' ORDER BY EVNR_tbl._Date ASC";

    clubconn=new SqlConnection(strclubconn);
    clubconn.Open();
    dsClub=new DataSet();
    clubadapt=new SqlDataAdapter(strEventSelect,clubconn);
    clubadapt.FillSchema(dsClub,SchemaType.Source,"EVNR_tbl");
    clubadapt.Fill(dsClub,"EVNR");
    foreach(DataRow dr in dsClub.Tables["EVNR"].Rows)
    {
    strEventRelease += "<br><br>" + System.Convert.ToDateTime(dr["_Date"]) +
    " " + dr["EventTitle"] + "<br>";
    strEventRelease +=
    dr["Description"].ToString().Replace("\n","<br>").Trim() + "<br>";
    strEventRelease += "For details press <a
    href='http://www.sonar.bc.ca/EventCalendar.aspx'> here </a>" + "<br>";
    }
    return strEventRelease;
    }
    public System.Text.StringBuilder BindData(System.DateTime SDate,
    System.DateTime EDate)
    {
    EventList(SDate,EDate);
    MailString=new System.Text.StringBuilder(99000);
    // Set Email Body.
    MailString.Append("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0
    Transitional//EN' >");
    MailString.Append("<HTML>");
    MailString.Append("<HEAD>");
    MailString.Append("<meta name='GENERATOR' Content='Microsoft Visual
    Studio .NET 7.1'>");
    MailString.Append("<meta name='CODE_LANGUAGE' Content='C#'>");
    MailString.Append("<meta name='vs_defaultClientScript'
    content='JavaScript'>");
    MailString.Append("<meta name='vs_targetSchema'
    content='http://schemas.microsoft.com/intellisense/ie5'>");
    MailString.Append("</HEAD>");
    MailString.Append("<body MS_POSITIONING='GridLayout' bgColor='black'>");
    MailString.Append("<form id='Form1' method='post' runat='server'>");
    MailString.Append("<table borderColor='#ffffff' cellSpacing='0'
    cellPadding='0' width='500' align='center' border='1'>");
    MailString.Append("<tr>");
    MailString.Append("<td>");
    MailString.Append("<table cellSpacing='0' cellPadding='0' width='100%'
    border='0'>");
    MailString.Append("<tr>");
    MailString.Append("<td colspan='2'><IMG
    SRC='http://www.sonar.bc.ca/images/Header.jpg'></td>");
    MailString.Append("</tr>");
    MailString.Append("<tr>");
    MailString.Append("<td colspan='2' style='HEIGHT: 48px'>");
    MailString.Append("<table cellpadding='10' cellspacing='0' border='0'
    width='100%'>");
    MailString.Append("<tr bgcolor='#000033'>");
    MailString.Append("<td style='FONT-SIZE: 10px; COLOR: white; FONT-FAMILY:
    Verdana'>" + EMessage + "</td>");
    MailString.Append("</tr>");
    MailString.Append("</table>");
    MailString.Append("</td>");
    MailString.Append("</tr>");
    MailString.Append("<tr>");
    MailString.Append("<td>");
    MailString.Append("<table cellpadding='10' cellspacing='0' border='0'
    width='100%'>");
    MailString.Append("<tr>");
    MailString.Append("<td style='FONT-SIZE: 10px; WIDTH: 251px; COLOR:
    white; FONT-FAMILY: Verdana' vAlign='Top'>");
    MailString.Append(strEventRelease);
    MailString.Append("</td>");
    MailString.Append("<td style='FONT-SIZE: 10px; COLOR: white; FONT-FAMILY:
    Verdana' vAlign='top'>");
    MailString.Append("<p>Promotions</p>");
    foreach(ListItem i in IMGList.Items)
    {
    if(i.Selected)
    {
    MailString.Append("<img src='http://www.sonar.bc.ca/BroadcastIMG/" +
    i.Text + "'" + ">" +"<br>");
    MailString.Append("<br><hr noshade='true'><br>");
    }
    }
    MailString.Append("</td>");
    MailString.Append("</tr>");
    MailString.Append("</table>");
    MailString.Append("</td>");
    MailString.Append("</tr>");
    MailString.Append("</table>");
    MailString.Append("</td>");
    MailString.Append("</tr>");
    MailString.Append("</table>");
    MailString.Append("</form>");
    MailString.Append("</body>");
    MailString.Append("</HTML>");

    return MailString;
    }
    }
    I know this is a lot to read but, for the life of me, I can't figure out
    what is wrong. Please someone help.
     
    =?Utf-8?B?SSBhbSBTYW0=?=, Apr 24, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve C. Orr, MCSD

    How can i make a email mass sender with asp.net ?

    Steve C. Orr, MCSD, Aug 20, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    811
    Steve C. Orr, MCSD
    Aug 21, 2003
  2. =?Utf-8?B?cmFtYXRh?=

    Strange StringBuilder behaviour when composing Email

    =?Utf-8?B?cmFtYXRh?=, May 12, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,116
    =?Utf-8?B?cmFtYXRh?=
    May 12, 2005
  3. PokerMan

    Mass email newshot

    PokerMan, Oct 12, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    555
    PokerMan
    Oct 12, 2007
  4. Steve C. Orr, MCSD

    How can i make a email mass sender with asp.net ?

    Steve C. Orr, MCSD, Aug 20, 2003, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    126
    Steve C. Orr, MCSD
    Aug 21, 2003
  5. cameron

    CDO mass email

    cameron, Aug 5, 2003, in forum: ASP General
    Replies:
    2
    Views:
    123
    Parker Zhang [MSFT]
    Aug 7, 2003
Loading...

Share This Page