Results 1 to 16 of 16

Thread: [2005] Sending e-mail from a web page

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    [2005] Sending e-mail from a web page

    I would like to allow the user to check off "who" to send e-mails to (see attached image) and then open their mail with all those usernames in the To: line. I'm looking to allow them to check more then 1 check box - having all the names appear.

    How can I do that.
    Attached Images Attached Images  

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Sending e-mail from a web page

    When you say "and then open their mail", do you mean their email client?

    If yes, then it's a liiiitle bit complicated.

    So the user checks a bunch of checkboxes, clicks on a button and you want this to happen. In the button's click event, go through each row where the checkbox is checked. Find the corresponding email address. Then, concatenate a string and your eventual aim is to do this

    Code:
    Response.Redirect("mailto:[email protected];[email protected];[email protected]")

    Alternatively, in the buttons click event (client side), you can get some javascript to do the same thing as above, except it'd be

    Code:
    document.location.href='mailto:[email protected];[email protected];[email protected]';

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [2005] Sending e-mail from a web page

    Yes - their e-mail client (I thought you told someone today that you could read minds...)

    Should I be avoiding javascript?

    Isn't javascript what's behind (or should I say in front of ) making this asp.net stuff run anyway...

    If I did it in js I would need to know how to concatenate the names and such - right?

    Which way would you lean?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Sending e-mail from a web page

    Hmm... do you want to avoid javascript? Because I know this is an 'internal' application for your company, you can take certain liberties such as assuming the user has javascript turned on, uses IE/Firefox, compatible browsers, all that business.

    Yes, javascript is what enables postbacks but I believe that if you disable javascript, the browser attempts to 'gracefully' degrade to use form submissions with extra information.

    The codebehind way, in which you loop through your repeater, get all the email addresses, concatenate it and do the response.redirect, would actually be easier.

    If you did it in javascript, you will need to have a hidden field in each row of your repeater that contains the corresponding email address. When the checkbox is checked, you can have each checkbox manipulate a global javascript variable (array), adding or removing its value from that array. The array is obviously important, since you will use that array - loop through it, read each element, concatenate the string.

    So, what do you want to do?

  5. #5

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [2005] Sending e-mail from a web page

    I like the vb.net backend coding - that's easier for me.

    Done!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Sending e-mail from a web page

    Because you're doing it the codebehind way, I'm currently working on a sample to do it the other way - javascript.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Sending e-mail from a web page

    Ready. In my ASPX, I first make the javascript.

    Code:
     <script type="text/javascript">
      
    
            Array.prototype.remove=function(str)
            {
              for(i=0;i<this.length;i++)
              {
                if(this[i]==str) 
                {
                    this.splice(i, 1);
                }
              }
            }
    
            var EmailAddresses = new Array();
    
            function AddRemoveEmailAddress(chk, emailaddress)
            {
                if(chk.checked == true)
                {
                    EmailAddresses.push(emailaddress);
                }
                else
                {
                    EmailAddresses.remove(emailaddress);
                }
                
            }
            
            
            function SendEmails()
            {
            
                var emailString = "";
                
                for(i=0;i<EmailAddresses.length;i++)
                {
                    emailString += EmailAddresses[i] + ";";
                }
                
               document.location.href='mailto:' + emailString;
               
            }
    
         </script>
    Then, setup the gridview and button.

    Code:
     <div>
        <asp:GridView ID="Students" Runat="server" OnRowDataBound="Students_RowDataBound">
        <Columns>
        <asp:TemplateField>
        <ItemTemplate>
        <asp:CheckBox ID="chkSendMail" runat="server" />
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>
        </div>
        <div>
        <input type="button" onclick="javascript:SendEmails();" value="Send" id="EmailButton" />
        </div>

    Then in the codebehind, I first setup a dummy datatable.

    vb Code:
    1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.  
    3.  
    4.         Dim dt As New Data.DataTable()
    5.         dt.Columns.Add("col1")
    6.         dt.Columns.Add("col2")
    7.         dt.Columns.Add("col3")
    8.  
    9.         Dim dr As Data.DataRow = dt.NewRow()
    10.         dr.Item("col1") = "111"
    11.         dr.Item("col2") = "222"
    12.         dr.Item("col3") = "[email protected]"
    13.  
    14.         Dim dr2 As Data.DataRow = dt.NewRow()
    15.         dr2.Item("col1") = "333"
    16.         dr2.Item("col2") = "444"
    17.         dr2("col3") = "[email protected]"
    18.  
    19.  
    20.         Dim dr3 As Data.DataRow = dt.NewRow()
    21.         dr3.Item("col1") = "555"
    22.         dr3.Item("col2") = "666"
    23.         dr3.Item("col3") = "[email protected]"
    24.  
    25.  
    26.         dt.Rows.Add(dr)
    27.         dt.Rows.Add(dr2)
    28.         dt.Rows.Add(dr3)
    29.  
    30.  
    31.  
    32.         Students.DataSource = dt
    33.         Students.DataBind()
    34.  
    35.  
    36.  
    37.     End Sub



    And the main bit, the RowDataBound event


    vb Code:
    1. Protected Sub Students_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Students.RowDataBound
    2.         If e.Row.RowType = DataControlRowType.DataRow Then
    3.             Dim chk As New CheckBox
    4.             chk = e.Row.FindControl("chkSendMail")
    5.             chk.Attributes.Add("onClick", "AddRemoveEmailAddress(this, '" & CType(e.Row.DataItem, Data.DataRowView).Item("col3").ToString() & "');")
    6.  
    7.  
    8.         End If
    9.     End Sub


    Ta-daaaa

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Sending e-mail from a web page

    Should this have gone in the codebank?

  9. #9

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [2005] Sending e-mail from a web page

    Let's do a joint submission - when I finish up my codebehind logic I'll post that here as well.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Sending e-mail from a web page

    Good idea. Post your bit here too: http://www.vbforums.com/showthread.php?t=515108

  11. #11

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [2005] Sending e-mail from a web page

    We are finally getting around to working on this...

    How come this code

    Code:
            Dim mailto As String = "mailto:"
            For Each n As String In l
                mailto &= n & ";"
            Next
            Response.Redirect(mailto)
    Opens a webpage with the "mailto" list.

    Any way to make it stay on the original page?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2005] Sending e-mail from a web page

    A Response.Redirect isn't going to work. The redirect process starts on the server side and thus, you'll end up on a page with just the mailto stuff in it.

    The only way to make the client mail application open is via a client side event on the page (a click of a link, if you will).

    I don't think there is anyway to do this entirely on the server side. Take the frog's code, put your name on it and use that

    Edit: Unless I'm missing something and there is a header you can add to your redirect that could make this work...

    Edit 2: I wonder if it's possible to modify the headers when the response is being sent with something like this:
    Code:
    WWW-Link:  href="mailto:[email protected]"; rev=made
    Found this info here: http://www.w3.org/Protocols/HTTP/Object_Headers.html

    Edit 3: Another option is to register some JavaScript to run onLoad and have it automatically open Url with mailto in the address.

    So in your OnInit method, register a JavaScript that you modify to include the appropriate Url. When the page returns on that postback, Bam! E-mail pops open.
    Last edited by Kasracer; Aug 20th, 2008 at 12:18 PM.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  13. #13
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Sending e-mail from a web page

    A webpage with the mailto list?

    Perhaps mailto hasn't been registered for you, although it's been years since I've done this I thought mailto would open up the email client on the user's desktop.

  14. #14
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Sending e-mail from a web page

    Re: Edit3: He means ClientScript.RegisterStartupScript or ClientScript.RegisterClientScriptBlock.

  15. #15

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [2005] Sending e-mail from a web page

    Just to clarify - it's doing what KAS indicated.

    See the attached image.

    We click on the Email Selected spot and it runs re-direct.

    The re-direct sends the browser to an empty page with the MAILTO: in the address bar.

    The CLIENT E-MAIL app does open - that's working great.

    But the browser has to be back-buttoned to the page we were just on.

    We tried make the EMAIL SELECTED be a MAILTO: link - that grows as each person to e-mail is clicked. But the page posts back and there is a noticeable flash - that stinks.
    Attached Images Attached Images  
    Last edited by szlamany; Aug 22nd, 2008 at 11:56 AM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  16. #16
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Sending e-mail from a web page

    Tools > Options > Programs : Is there a default email client set?

    Perhaps you should consider the Javascript solution then, since you're relying on client-side behavior in either scenario.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width