Send Email With Checkboxes

Hey,

I am trying to figure something out, on this page:

http://kidsunlimited.co.uk/test.aspx

I have the following code:


    <asp:GridView ID="GridView1" runat="server" CellPadding="10" 
        ForeColor="#333333" AllowPaging="True" 
        AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
        PageSize="30" cssclass="GridViewStyle" 
        onrowdatabound="GridView1_RowDataBound">
        <FooterStyle CssClass="GridViewFooterStyle" />
        <RowStyle CssClass="GridViewRowStyle" />   
        <SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
        <PagerStyle CssClass="GridViewPagerStyle" />
        <AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
        <HeaderStyle CssClass="GridViewHeaderStyle" />
        <Columns>
            <asp:BoundField DataField="application_id" HeaderText="application_id" InsertVisible="False" ReadOnly="True" SortExpression="application_id" Visible="False" />
            <asp:BoundField DataField="fname" HeaderText="Forename" SortExpression="fname" />
            <asp:BoundField DataField="sname" HeaderText="Surname" SortExpression="sname" />
            <asp:BoundField DataField="cv" HeaderText="CV" SortExpression="cv" ItemStyle-CssClass="tdclass"/>
            <asp:BoundField DataField="status" HeaderText="Status" SortExpression="status" />
            <asp:BoundField DataField="date_addedd" HeaderText="Date" SortExpression="date_addedd" DataFormatString="{0:U}" HtmlEncode="false"/>
                
                <asp:TemplateField HeaderText="Forward">
                    <ItemStyle HorizontalAlign="Center" />
                        <ItemTemplate>
                           <asp:CheckBox ID="ApplSelector" runat="server" />
                        </ItemTemplate>
                </asp:TemplateField>    
                            
                <asp:TemplateField>
                    <ItemStyle HorizontalAlign="Center"/>
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink1" runat="server" Text='View application'></asp:HyperLink>
                        </ItemTemplate>
                </asp:TemplateField>
                
                <asp:TemplateField>
                    <ItemStyle HorizontalAlign="Center"/>
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink2" runat="server" Text='Edit status'></asp:HyperLink>
                        </ItemTemplate>
                </asp:TemplateField>
                
                <asp:TemplateField>
                    <ItemStyle HorizontalAlign="Center"/>
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink3" runat="server" Text='Delete'></asp:HyperLink>
                        </ItemTemplate>
                </asp:TemplateField>
        </Columns>
    </asp:GridView>
     
     <asp:Button ID="CheckAll" runat="server" Text="Check All" onclick="CheckAll_Click" />  
     &nbsp;
     <asp:Button ID="UncheckAll" runat="server" Text="Uncheck All" onclick="UncheckAll_Click" />
     &nbsp;
     <asp:Button ID="ForwardCVApplications" runat="server" Text="Forward Applications" onclick="ForwardCVApplications_Click" /> 
     
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:SQL2008_673062_kidsConnectionString %>" 
        SelectCommand="SELECT [application_id], [fname], [sname], [cv], [status], [date_addedd] FROM [tbl_apprenticeship_applications]">
    </asp:SqlDataSource>

And what i want to do is on “ForwardCVApplications_Click” i want to attach all the [cv] documents which are located on the server to an email. What i want is when the button is clicked it opens an email software such as Outlook and has the [cv] (whiever are selected via the checkbox) to be attached to the email.

The other bits of the email such as To, and Subject can be filled in manually. I currently have this in the .cs file:


    private void ToggleCheckState(bool checkState)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("ApplSelector");
            if (cb != null) cb.Checked = checkState;
        }
    }
    protected void ForwardCVApplications_Click(object sender, EventArgs e)
    {

    }
    protected void CheckAll_Click(object sender, EventArgs e)
    {
        ToggleCheckState(true); 
    }
    protected void UncheckAll_Click(object sender, EventArgs e)
    {
        ToggleCheckState(false); 
    }

Any ideas how i can accomplish this on the Button click?

Thanks

Try googling “Printing files with asp.net”. There should be quite a few examples

Ok, are there any tutorials, assistance i can look at for inspiration?

Thanks :confused:

Well, yes, you can print it, but remember, it will only print on a printer attached to the server. Not on the client machine.

Ok sorry for wasting your time :wink: but the client has been made to change his mind. Basically this was only the start of what they wanted and then it would have grown into something VERY big which was not feasible!

Therefore i now only need to PRINT those applications (The CV document) where the checkbox is clicked…

So i still need the checkboxes etc… I just need to print those applications that are checked.

The applications are held here:

http://kidsunlimi2.eweb102.discountasp.net/applications/

There are CV’s (Resime’s) in the form of Word .doc, .docx, ,rtf and pdf.

So a typical CV may be called “myCV.doc”

So can i on Button click send the selected checkbox data to be printed??

Thanks

Ok, well firstly this is 1 thing I see:

string contentID = Path.GetFileName(attachmentPath).Replace(“.”, “”) + “@zofm”;
seems to do nothing, as the attachmentPath is empty in ur code.

  1. attachmentPath = “http://kidsunlimi2.eweb102.discountasp.net/applications/”;

That is not a correct path for an attachment. An attachment path should be somelike like: “D:\inetpub\wwwroot\kidsunlimi2\uploads\file.jpg”;

You can use Server.MapPath to get the current working directory. eg. Server.MapPath(“~/upload/file.jpg”);

And lastly, CheckBox cb = (CheckBox)row.FindControl(“ApplSelector”);

I usually use: row.Cells[0].FindControl(“ApplSelector”) as CheckBox;

last part is so that cb will be null if not found. It will fail if not found and you trying to cast it.

Oh, and also this: “cb.Checked = checkState”

What is checkState? and if you are comparing someting, you should use ==.

But best is to just do this:
if (cb != null && cb.Checked)

I hope all this makes sense and helps a bit

Hey,

I am trying with this but it does not seem to work:


protected void ForwardCVApplications_Click(object sender, EventArgs e)
    {
        string attachmentPath;

        // creating the email message
        MailMessage email = new MailMessage("ibrar@freemanholland.com", "ibrar@freemanholland.com");

        // information
        email.Subject = "Your Subject";
        email.IsBodyHtml = true;
        email.Body = "Your Email body. You can also use html tags here as it is set to true on above line";

        // generate the contentID string using the datetime
        string contentID = Path.GetFileName(attachmentPath).Replace(".", "") + "@zofm";

        // create the INLINE attachment
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("ApplSelector");
            if (cb != null && cb.Checked = checkState)
            {
                attachmentPath = "http://kidsunlimi2.eweb102.discountasp.net/applications/";
                Attachment inline = new Attachment(attachmentPath);
                inline.ContentDisposition.Inline = true;
                inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
                inline.ContentId = contentID;
                inline.ContentType.MediaType = "image/png";
                inline.ContentType.Name = Path.GetFileName(attachmentPath);
                email.Attachments.Add(inline);
            }
        }

        //send the message
        SmtpClient smtp = new SmtpClient("localhost");
        smtp.Send(email);

        email.Dispose();
    }

Can you please advise? I am getting a red underline under “checkState”…

Any ideas what i am doing wrong?

Thanks

Can you not just loop through the rows of the gridview and check wether they have been check or not and if they are, do whatever you need to do with it?

I could do something like this:


    protected void ForwardCVApplications_Click(object sender, EventArgs e)
    {
        //create the mail message
        MailMessage mail = new MailMessage();

        //set the addresses
        mail.From = new MailAddress("me@mycompany.com");
        mail.To.Add("you@yourcompany.com");

        //set the content
        mail.Subject = "This is an email";
        mail.Body = "this content is in the body";

        //add an attachment from the filesystem
        mail.Attachments.Add(new Attachment("c:\\\	emp\\\\example.txt"));

        //send the message
        SmtpClient smtp = new SmtpClient("127.0.0.1");
        smtp.Send(mail);
    }

But first problem is that i need to access the database row [cv] as an attachment, and also i will have more than one attachment based on the number of cheboxes that are selected…

What should i do people :shifty: