Solved

"Send Email To All" - How do I pass all the adresses to mailto hyperlink?

Posted on 2009-05-15
14
403 Views
Last Modified: 2013-12-17
I coudn't figure out how to handle this, so I have a gridview of the Students who are registered to a class with their Username,Name,Email...etc. Email column is hyperlink with a mailto and it opens Outlook - working fine.
I added a new checkbox column with a "select all" header where you can select all or multiple rows by clicking the checkboxes on each row. And I added a button to the footer of the newly created column - Send Email. I can also capture all the emails of the selected rows from Email field.
So this is where I got stuck, this button could be an hyperlink also whichever works better. So I need to click and see that "array/string" of addresses in the To: field of the outlook.
How can I do this? I will post some code snippets also.

So basicly how will I put that "button" into the form of my hyperlinks within the gridview so that when I click it opens up outlook with the adresses I have, which currently I write on to the screen:)
---Gridview----
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                DataSourceID="SqlDataSource4" AllowSorting="True" 
                DataKeyNames="Email,Class" ShowFooter="True">
                <Columns>
                    <asp:TemplateField>
                        <FooterTemplate>
                            <asp:Button ID="Button1" runat="server" onclick="Button1_Click1" 
                                Text="Button" />
                            <br />
                            <asp:HyperLink ID="HyperLink6" runat="server">HyperLink</asp:HyperLink>
                        </FooterTemplate>
                        <HeaderTemplate>
                            <asp:CheckBox ID="CheckBox2" runat="server" 
                                oncheckedchanged="CheckBox2_CheckedChanged" Text="Select All" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox3" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Username" HeaderText="Username" 
                        SortExpression="Username" />
                    <asp:BoundField DataField="First Name" HeaderText="First Name" 
                        SortExpression="First Name" />
                    <asp:BoundField DataField="Last Name" HeaderText="Last Name" 
                        SortExpression="Last Name" />
                    <asp:TemplateField HeaderText="Email" SortExpression="Email">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Email") %>'>
                            </asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink7" runat="server" 
                            NavigateUrl='<%# Eval("Email","mailto:{0}?Subject=Course Inquiry") %>' 
                            Text='<%# Bind("Email") %>'></asp:HyperLink>
                        </ItemTemplate>
                    </asp:TemplateField>                        
                    <asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
                    <asp:BoundField DataField="Location" HeaderText="Location" 
                        SortExpression="Location" />
                    <asp:BoundField DataField="Instructor" HeaderText="Instructor" ReadOnly="True" 
                        SortExpression="Instructor" />
                </Columns>
            </asp:GridView>
 
----Code-----
//select all
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk;
 
        foreach (GridViewRow rowItem in GridView1.Rows)
        {
            chk = (CheckBox)(rowItem.Cells[0].FindControl("CheckBox3"));
            chk.Checked = ((CheckBox)sender).Checked;
        }
    }
 
//get the checked ones
    protected void Button1_Click1(object sender, EventArgs e)
    {
        CheckBox chk;
 
        foreach (GridViewRow rowItem in GridView1.Rows)
        {
            chk = (CheckBox)(rowItem.Cells[0].FindControl("CheckBox3"));
            if (chk.Checked)
            {
                Response.Write(GridView1.DataKeys[rowItem.RowIndex]["Email"].ToString() + "<br />");
            }
        }
    }

Open in new window

0
Comment
Question by:askasp
[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
  • 9
  • 5
14 Comments
 
LVL 3

Expert Comment

by:channa_m
ID: 24399051
You need to build the , delimitted string of email address and use Response.Redirect
 protected void Button1_Click1(object sender, EventArgs e)
    {
        CheckBox chk;
        string toAddressList = "";
        foreach (GridViewRow rowItem in GridView1.Rows)
        {
            chk = (CheckBox)(rowItem.Cells[0].FindControl("CheckBox3"));
            if (chk.Checked)
            {
                string emailAddress = GridView1.DataKeys[rowItem.RowIndex]["Email"].ToString();
 
                if (String.IsNullOrEmpty(toAddressList))
                {
                    toAddressList = emailAddress;
                }
                else
                {
                    toAddressList = toAddressList + "," + emailAddress;
                }
            }
        }
        Response.Redirect(String.Format("mailto:{0}?Subject=Course Inquiry", toAddressList));
    }

Open in new window

0
 

Author Comment

by:askasp
ID: 24399257
Thank you for your reply.
Well If I do the way you suggested then
- First I end up on a blank page (which could be solved probably by setting a target - not sure though)
- Second because of the Response.Redirect, I see all the emails in url such as my url looks like >>
mailto:aaaa@aaa.com,bbb@bbb.com,ccc@ccc.com?Subject=Course%20Inquiry

which I definetly don't want either of them.
0
 
LVL 3

Expert Comment

by:channa_m
ID: 24399635
If you dont like using redirect, your other option is
 <script>
         function mailTo(toAddress){
             window.location.href="mailto:" + toAddress + "?Subject=Course Inquiry";
        }    
    </script>
 
 
   StringBuilder sb = new StringBuilder();
        sb.Append("<script type='text/javascript'>");
        sb.Append(String.Format("mailTo(\"{0}\")", t1.Text));
        sb.Append("</script>");
        ClientScript.RegisterClientScriptBlock(this.GetType(), "sendMail", sb.ToString());

Open in new window

0
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

Author Comment

by:askasp
ID: 24399711
sb.Append(String.Format("mailTo(\"{0}\")", t1.Text));

and what is t1.Text in your code?
0
 

Author Comment

by:askasp
ID: 24399763
Oh ok nevermind. I think I got it. So I tried and it worked but I am not familiar with that syntax at all so my code is now

 <script>
         function mailTo(toAddress){
             window.location.href="mailto:" + toAddress + "?Subject=Course Inquiry";
        }    
    </script>

        StringBuilder sb = new StringBuilder();
        sb.Append("<script type='text/javascript'>");
        sb.Append(String.Format("mailTo(\"{0}\")",toAddressList));
        sb.Append("</script>");
        ClientScript.RegisterClientScriptBlock(this.GetType(), "sendMail", sb.ToString());

How can i add the other fields to mailTo , like "bcc","cc" ...etc?
0
 
LVL 3

Expert Comment

by:channa_m
ID: 24399787

<script>
         function mailTo(toAddress,cc,bcc){
             window.location.href="mailto:" + toAddress + "?Subject=Course Inquiry&cc=" + cc + "&bcc=" +bcc;
        }    
    </script>
 
        StringBuilder sb = new StringBuilder();
        sb.Append("<script type='text/javascript'>");
        sb.Append(String.Format("mailTo(\"{0}\",\"{1}\",\"{2}\")",toAddressList,ccAddress,bccAddress));
        sb.Append("</script>");
        ClientScript.RegisterClientScriptBlock(this.GetType(), "sendMail", sb.ToString());

Open in new window

0
 

Author Comment

by:askasp
ID: 24400300
Getting closer:)
I still couldnt figure out, there is a syntax issue here
    >>     window.location.href = "mailto:" + toAddress + "?Subject=Course Inquiry&cc=" + cc + "&bcc=" + bcc;

because it puts all the address and then "&bccaddresses" to CC in outlook and if I do

window.location.href = "mailto:" + toAddress + "?Subject=Course Inquiry&cc=" + cc + "&" + "bcc=" + bcc;

then it puts "&" to CC and the bcc adresses to bcc

Any suggestion?
And thank you for your time :)
0
 

Author Comment

by:askasp
ID: 24400354
To make it more clear:
First case looks like this
CC: &bcc=aaa@aaa.com,bbb@bbb.com

Second case looks like this
CC: &
BCC: aaa@aaa.com,bbb@bbb.com
0
 
LVL 3

Accepted Solution

by:
channa_m earned 250 total points
ID: 24400887
<script>
         function mailTo(toAddress,cc,bcc){

            var url="mailto:" + toAddress + "?Subject=Course Inquiry"
            
            if (cc !=""){
                  url = url + "&cc=" + cc
            }            
            if (bcc !=""){
                  url = url + "&bcc=" + bcc
            }            

            window.location.href=url
        }    
    </script>
0
 

Author Comment

by:askasp
ID: 24409575
I'll give it a try tomorrow the first thing and Thank you.
0
 

Author Closing Comment

by:askasp
ID: 31582062
Thank you very much.
0
 

Author Comment

by:askasp
ID: 24414051
I actually have another question related with javascript again for a delete confirmation in gridview which is not a template column. But probably it would be better if I open a new thread for that, or can I just ask here again?
0
 
LVL 3

Expert Comment

by:channa_m
ID: 24415418
Yes Please post a new request
0
 

Author Comment

by:askasp
ID: 24416295
If you would wanna follow-up here is the link to the new thread
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_24419031.html

and Thank you for your help on this one.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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 video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

734 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