locked
this code doesnt show any error but also doesnt send the email RRS feed

  • Question

  • User1806930132 posted

    Morning everyone,

    I have the below code and just couldn't understand why is it not sending email, it doesn't throw any error though, but can someone please help me out with this.

     <asp:GridView ID="GridView1" runat="server" CssClass="auto-style2" autogeneratecolumns="false">
                <Columns>
                     <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="Checkbox1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="CRESID">
                <ItemTemplate>
                    <asp:TextBox ID="CRESID" runat="server" Text='<%# Eval("CRESID") %>'  />
                </ItemTemplate>
                        </asp:TemplateField>
                     <asp:TemplateField HeaderText="SiteName">
                <ItemTemplate>
                    <asp:Textbox ID="SiteName" runat="server" Text='<%# Eval("SiteName") %>'  />
                </ItemTemplate>
                        </asp:TemplateField>
    <asp:TemplateField HeaderText="MEmail">
                <ItemTemplate>
                    <asp:HyperLink ID="MEmail" runat="server" Text='<%# Eval("MEmail") %>'  />
                </ItemTemplate>
                        </asp:TemplateField> </Columns> </asp:GridView> <p> &nbsp;</p> <p> &nbsp;</p> <asp:Button ID="Button2" runat="server" CssClass="auto-style4" style="z-index: 1" Text="Send Email" onclick="Button2_Click"/> </div>
      Protected Sub Button2_Click(sender As Object, e As EventArgs)
    
    
            Dim dtUsers As New DataTable()
    
            dtUsers.Columns.AddRange(New DataColumn(2) {New DataColumn("CRESID"), New DataColumn("SiteName"), New DataColumn("MEmail")})
            For Each row As GridViewRow In GridView1.Rows
                If row.RowType = DataControlRowType.DataRow Then
    
                    Dim CheckBox1 As CheckBox = TryCast(row.Cells(0).FindControl("CheckBox1"), CheckBox)
    
                    If CheckBox1.Checked Then
    
                        Dim CRESID As String = TryCast(row.Cells(1).FindControl("CRESID"), TextBox).Text
    
                        Dim SiteName As String = TryCast(row.Cells(2).FindControl("SiteName"), TextBox).Text
                        Dim MEmail As String = TryCast(row.Cells(3).FindControl("MEmail"), HyperLink).Text
    
                        dtUsers.Rows.Add(CRESID, SiteName, MEmail)
    
                    End If
    
                End If
            Next
     Dim subject As String = "Survey Email"
            Dim body As String = "Hello {0}, <br/> < br /> "
            body += "<br /><br />Please click the following link to activate your account"
      body += "<br /><br />Thanks"
    
    
            Parallel.ForEach(dtUsers.AsEnumerable(),
                             Function(row)
                                 Return SendEmail(row("MEmail").ToString(), subject, String.Format(body, row("SiteName")))
                             End Function)
        End Sub
    
        Private Function SendEmail(recipient As String, subject As String, body As String) As Boolean
            Dim mm As New MailMessage("abc.com", recipient)
            mm.Subject = subject
            mm.Body = body
            mm.IsBodyHtml = True
            Dim smtp As New SmtpClient()
            smtp.Host = "smtp.gmail.com"
            smtp.EnableSsl = True
            Dim NetworkCred As New NetworkCredential()
            NetworkCred.UserName = "abc.com"
            NetworkCred.Password = ""
            smtp.UseDefaultCredentials = True
            smtp.Credentials = NetworkCred
            smtp.Port = 587
            smtp.Send(mm)
            Return True
        End Function
    


    Tuesday, May 24, 2016 7:12 AM

All replies

  • User753101303 posted

    Hi,

    Have you tried first without Parallel.For (which likely won't help anyway). Are you 100% dtUsers has been populated? You could also easily do a test call to your SendEmail function to see if it works fine. Another option could be that the mail is sent but blocked as spam (double check the Junk mail folder).

    Tuesday, May 24, 2016 8:18 AM
  • User1806930132 posted

    this is pageload event

     Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
      If Not Me.IsPostBack Then
    
                Dim dt As New DataTable()
                dt.Columns.AddRange(New DataColumn(2) {New DataColumn("CRESID"), New DataColumn("SiteName"), New DataColumn("CTMEmail")})
    
    dt.Rows.Add(......what/how do I add rows in here...???????the values are populated from database as shown in .aspx page?
                GridView1.DataSource = dt
    
                GridView1.DataBind()
            End If

    Tuesday, May 24, 2016 8:41 AM
  • User-271186128 posted

    Hi msaahil,

     Dim dt As New DataTable()
     dt.Columns.AddRange(New DataColumn(2) {New DataColumn("CRESID"), New DataColumn("SiteName"), New DataColumn("CTMEmail")})
    
     dt.Rows.Add(......what/how do I add rows in here...???????the values are populated from database as shown in .aspx page?
     GridView1.DataSource = dt
    GridView1.DataBind()

    I suggest you could refer to the following code to query the database and bind GridView:

            'Get the connection string
    Dim constr As String = ConfigurationManager.ConnectionStrings("MyTestDBConnectionString").ToString() 'create a DataTable to store the dataSource Dim dt As DataTable = New DataTable Using con As New SqlConnection(constr) Using cmd As New SqlCommand cmd.Connection = con Dim cmdtext As String = "SELECT [OrderID], [EmployeeID], [OrderDate], [RequiredDate], [ShippedDate] FROM [Orders]" cmd.CommandText = cmdtext con.Open() Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd) sda.Fill(dt) End Using End Using GridView1.DataSource = dt GridView1.DataBind()

    I have the below code and just couldn't understand why is it not sending email, it doesn't throw any error though, but can someone please help me out with this.

    As PatriceSc said, you can call the send email function without using the Parallel.For, and you can set a break point in the send email method and debug your code to see whether there have an error.

    Best regards,
    Dillion

    Tuesday, May 24, 2016 9:19 AM
  • User1806930132 posted

    There are no errors shown when debugging...what should I do now....is there any other way of sending email to list selected from gridview?

    Tuesday, May 24, 2016 9:56 AM
  • User1806930132 posted

    Hello All, I have tried changing the codes and stuff and noticed something wonder if it is going to help someone sort this out and explain me too please...

    I had the code above below which doesn't show any error but also doesn't send any email. In the below code pageload event if I say

     If Not Page.IsPostBack Then
                BindGrid()
            End If

    and also remove where clause from sql command...it shows all the list on pageload and sends email on button click...but I don't want that I want user to choose the country and send email to the list...I cant do that with   If Not Page.IsPostBack as it need to show grid on postback...how do I achieve this?why is it working without where clause and not is post backand not sending email with where clause and ispost back??????????????

     <div>
        
        
            <asp:DropDownList ID="DropDownList1" runat="server"  CssClass="auto-style2" autopostback="true" DataSourceID="SqlDataSource1" DataTextField="Country" DataValueField="Country">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:constr %>" SelectCommand="SELECT [Country] FROM [TMain]"></asp:SqlDataSource>
                              
               
                       <asp:Button ID="Button2" runat="server" CssClass="auto-style4" Text="send email"  onclick="Button2_Click"/>
                   <asp:GridView ID="GridView1" runat="server" CssClass="auto-style3" AutoGenerateColumns="False"  DataKeyNames="Id">
                <Columns>
                    <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkSelect" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
           
     <asp:TemplateField HeaderText="CRESID">
                <ItemTemplate>
                    <asp:TextBox ID="CRESID" runat="server" Text='<%# Eval("CRESID") %>'  />
                </ItemTemplate>
                        </asp:TemplateField>
           <asp:TemplateField HeaderText="SiteName">
                <ItemTemplate>
                    <asp:Textbox ID="SiteName" runat="server" Text='<%# Eval("SiteName") %>'  />
                </ItemTemplate>
                        </asp:TemplateField>
            <asp:TemplateField HeaderText="Email">
                <ItemTemplate>
                    <asp:HyperLink ID="lnkEmail" runat="server" Text='<%# Eval("MEmail") %>'  />
                </ItemTemplate>
            </asp:TemplateField>
                </Columns>
            </asp:GridView>
            </div>
     Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    
            If Page.IsPostBack Then
                BindGrid()
            End If
        End Sub
        Private Sub BindGrid()
            Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand("SELECT Id,CRESID,SiteName,CTMEmail FROM TMain  where Country='" & DropDownList1.SelectedValue & "'")
                    Using sda As New SqlDataAdapter()
                        cmd.Connection = con
                        sda.SelectCommand = cmd
                        Using dt As New DataTable()
                            sda.Fill(dt)
                            GridView1.DataSource = dt
                            GridView1.DataBind()
                        End Using
                    End Using
                End Using
            End Using
        End Sub
    
    Here if I remove where clause and say page is not post back...it sends email
     Protected Sub Button2_Click(sender As Object, e As EventArgs)
            Dim dtUsers As New DataTable()
            dtUsers.Columns.AddRange(New DataColumn(2) {New DataColumn("CRESID"), New DataColumn("SiteName"), New DataColumn("MEmail")})
    
            For Each row As GridViewRow In GridView1.Rows
                If row.RowType = DataControlRowType.DataRow Then
    
                    Dim chkSelect As CheckBox = TryCast(row.Cells(0).FindControl("chkSelect"), CheckBox)
    
                    If chkSelect.Checked Then
    
                        Dim CRESID As String = TryCast(row.Cells(1).FindControl("CRESID"), TextBox).Text
    
                        Dim SiteName As String = TryCast(row.Cells(2).FindControl("SiteName"), TextBox).Text
                        Dim MEmail As String = TryCast(row.Cells(3).FindControl("lnkEmail"), HyperLink).Text
    
                        dtUsers.Rows.Add(CRESID, SiteName, MEmail)
                    End If
    
                End If
            Next
    
            Dim subject As String = "Survey Email"
                        Dim body As String = "Hello {0}, <br/> < br /> "
                        body += "<br /><br />Please click the following link to activate your account"
    
                    body += "<br /><br />Thanks"
    
    
                        Parallel.ForEach(dtUsers.AsEnumerable(),
                             Function(row)
                                 Return SendEmail(row("MEmail").ToString(), subject, String.Format(body, row("SiteName")))
                             End Function)
        End Sub




    Wednesday, May 25, 2016 8:12 AM
  • User1806930132 posted

    can someone please tell me what could it be?

    Wednesday, May 25, 2016 10:43 AM
  • User475983607 posted

    Given the explanation, I suspect the grid is not bound before the Button2_Click event fires or the WHERE clause is incorrect.   One way to figure this out is to step through the code and make sure each logical block does what you expect.  Another option is to post all the code otherwise we have to guess.

    Wednesday, May 25, 2016 10:58 AM
  • User1806930132 posted

    the where clause is correct as it displays the result in the gridview...but I am unable to understand the binding of the gridview....is it the issue?the below code is all the code I have on my webform....please help

    <div>
        
        
            <asp:DropDownList ID="DropDownList1" runat="server"  CssClass="auto-style2" autopostback="true" DataSourceID="SqlDataSource1" DataTextField="Country" DataValueField="Country">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:constr %>" SelectCommand="SELECT [Country] FROM [TMain]"></asp:SqlDataSource>
                              
               
                       <asp:Button ID="Button2" runat="server" CssClass="auto-style4" Text="send email"  onclick="Button2_Click"/>
                   <asp:GridView ID="GridView1" runat="server" CssClass="auto-style3" AutoGenerateColumns="False"  DataKeyNames="Id">
                <Columns>
                    <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkSelect" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
           
     <asp:TemplateField HeaderText="CRESID">
                <ItemTemplate>
                    <asp:TextBox ID="CRESID" runat="server" Text='<%# Eval("CRESID") %>'  />
                </ItemTemplate>
                        </asp:TemplateField>
           <asp:TemplateField HeaderText="SiteName">
                <ItemTemplate>
                    <asp:Textbox ID="SiteName" runat="server" Text='<%# Eval("SiteName") %>'  />
                </ItemTemplate>
                        </asp:TemplateField>
            <asp:TemplateField HeaderText="Email">
                <ItemTemplate>
                    <asp:HyperLink ID="lnkEmail" runat="server" Text='<%# Eval("MEmail") %>'  />
                </ItemTemplate>
            </asp:TemplateField>
                </Columns>
            </asp:GridView>
            </div>
     Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    
            If Page.IsPostBack Then
                BindGrid()
            End If
        End Sub
        Private Sub BindGrid()
            Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand("SELECT CRESID,SiteName,MEmail FROM TMain  where Country='" & DropDownList1.SelectedValue & "'")
                    Using sda As New SqlDataAdapter()
                        cmd.Connection = con
                        sda.SelectCommand = cmd
                        Using dt As New DataTable()
                            sda.Fill(dt)
                            GridView1.DataSource = dt
                            GridView1.DataBind()
                        End Using
                    End Using
                End Using
            End Using
        End Sub
    
    Here if I remove where clause and say page is not post back...it sends email
     Protected Sub Button2_Click(sender As Object, e As EventArgs)
            Dim dtUsers As New DataTable()
            dtUsers.Columns.AddRange(New DataColumn(2) {New DataColumn("CRESID"), New DataColumn("SiteName"), New DataColumn("MEmail")})
    
            For Each row As GridViewRow In GridView1.Rows
                If row.RowType = DataControlRowType.DataRow Then
    
                    Dim chkSelect As CheckBox = TryCast(row.Cells(0).FindControl("chkSelect"), CheckBox)
    
                    If chkSelect.Checked Then
    
                        Dim CRESID As String = TryCast(row.Cells(1).FindControl("CRESID"), TextBox).Text
    
                        Dim SiteName As String = TryCast(row.Cells(2).FindControl("SiteName"), TextBox).Text
                        Dim MEmail As String = TryCast(row.Cells(3).FindControl("lnkEmail"), HyperLink).Text
    
                        dtUsers.Rows.Add(CRESID, SiteName, MEmail)
                    End If
    
                End If
            Next
    
            Dim subject As String = "Survey Email"
                        Dim body As String = "Hello {0}, <br/> < br /> "
                        body += "<br /><br />Please click the following link to activate your account"
    
                    body += "<br /><br />Thanks"
    
    
                        Parallel.ForEach(dtUsers.AsEnumerable(),
                             Function(row)
                                 Return SendEmail(row("MEmail").ToString(), subject, String.Format(body, row("SiteName")))
                             End Function)
        End Sub
    

    Wednesday, May 25, 2016 12:12 PM
  • User1806930132 posted

    can someone please help me out solve this

    Thursday, May 26, 2016 7:11 AM
  • User1806930132 posted

    Is there any example atleast where I can have to a loo and change my code to some other method, I need to send email to all the people displayed in the gridview with specific ids....atleast someone help me with a different approach you all have tried

    Friday, May 27, 2016 7:08 AM
  • User475983607 posted

    msaahil2015@gmail.com

    Is there any example atleast where I can have to a loo and change my code to some other method, I need to send email to all the people displayed in the gridview with specific ids....atleast someone help me with a different approach you all have tried

    The problem you're having has to do with the page life cycle.  The Page_Load handler fires before the Button2_Click handler.  The Page_Load handler binds the GridView which reset all the checkboxes.

    One way to get around this issue is to have one button that filters and binds the GridView and one button to send email.

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="ForumGridEmail.aspx.vb" Inherits="TestWebvb.ForumGridEmail" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:DropDownList ID="DropDownList1" runat="server" CssClass="auto-style2" AutoPostBack="true" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Name">
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorks %>" SelectCommand="SELECT [Name] FROM [AdventureWorks2012].[Person].[CountryRegion]"></asp:SqlDataSource>
    
    
                <asp:Button ID="Button1" runat="server" Text="Filter" />
                <asp:GridView ID="GridView1" runat="server" CssClass="auto-style3" AutoGenerateColumns="False" DataKeyNames="CRESID">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:CheckBox ID="chkSelect" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="CRESID">
                            <ItemTemplate>
                                <asp:TextBox ID="CRESID" runat="server" Text='<%# Eval("CRESID") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="SiteName">
                            <ItemTemplate>
                                <asp:TextBox ID="SiteName" runat="server" Text='<%# Eval("SiteName") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Email">
                            <ItemTemplate>
                                <asp:HyperLink ID="lnkEmail" runat="server" Text='<%# Eval("MEmail") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:Button ID="Button2" runat="server" CssClass="auto-style4" Text="send email" OnClick="Button2_Click" Visible="false" />
    
            </div>
        </form>
    </body>
    </html>
    
    Imports System.Data.SqlClient
    Imports System.Threading.Tasks
    Imports System.Web.Mail
    Imports System.Net.Mail
    Imports System.Net
    
    Public Class ForumGridEmail
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
        Private Sub BindGrid()
            Dim constr As String = ConfigurationManager.ConnectionStrings("AdventureWorks").ConnectionString
            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand("SELECT CountryRegionCode AS CRESID ,Name AS SiteName ,CountryRegionCode + '@email.com' AS MEmail FROM Person.CountryRegion WHERE Name = '" & DropDownList1.SelectedValue & "'")
                    Using sda As New SqlDataAdapter()
                        cmd.Connection = con
                        sda.SelectCommand = cmd
                        Using dt As New DataTable()
                            sda.Fill(dt)
                            GridView1.DataSource = dt
                            GridView1.DataBind()
                        End Using
                    End Using
                End Using
            End Using
        End Sub
    
    
        Protected Sub Button2_Click(sender As Object, e As EventArgs)
            Dim dtUsers As New DataTable()
            dtUsers.Columns.AddRange(New DataColumn(2) {New DataColumn("CRESID"), New DataColumn("SiteName"), New DataColumn("MEmail")})
    
            For Each row As GridViewRow In GridView1.Rows
                If row.RowType = DataControlRowType.DataRow Then
    
                    Dim chkSelect As CheckBox = TryCast(row.Cells(0).FindControl("chkSelect"), CheckBox)
    
                    If chkSelect.Checked Then
    
                        Dim CRESID As String = TryCast(row.Cells(1).FindControl("CRESID"), TextBox).Text
    
                        Dim SiteName As String = TryCast(row.Cells(2).FindControl("SiteName"), TextBox).Text
                        Dim MEmail As String = TryCast(row.Cells(3).FindControl("lnkEmail"), HyperLink).Text
    
                        dtUsers.Rows.Add(CRESID, SiteName, MEmail)
                    End If
    
                End If
            Next
    
            Dim subject As String = "Survey Email"
            Dim body As String = "Hello {0}, <br/> < br /> "
            body += "<br /><br />Please click the following link to activate your account"
    
            body += "<br /><br />Thanks"
    
    
            Parallel.ForEach(dtUsers.AsEnumerable(), Function(row)
                                                         Return SendEmail(row("MEmail").ToString(), subject, String.Format(body, row("SiteName")))
                                                     End Function)
        End Sub
    
    
    
        Private Function SendEmail(recipient As String, subject As String, body As String) As Boolean
            Dim mm As New System.Net.Mail.MailMessage("abc.com", recipient)
            mm.Subject = subject
            mm.Body = body
            mm.IsBodyHtml = True
            Dim smtp As New SmtpClient()
            smtp.Host = "smtp.gmail.com"
            smtp.EnableSsl = True
            Dim NetworkCred As New NetworkCredential()
            NetworkCred.UserName = "abc.com"
            NetworkCred.Password = ""
            smtp.UseDefaultCredentials = True
            smtp.Credentials = NetworkCred
            smtp.Port = 587
            smtp.Send(mm)
            Return True
        End Function
    
        Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            BindGrid()
            Button2.Visible = True
        End Sub
    End Class




    Friday, May 27, 2016 11:33 AM
  • User1806930132 posted

    I tried this too friend but doesn't work, I tried wid above code, on clicking filter 3 rows come up but then on selecting and send mail...doesn't send any email

    Sunday, May 29, 2016 10:44 PM
  • User475983607 posted

    I tried this too friend but doesn't work, I tried wid above code, on clicking filter 3 rows come up but then on selecting and send mail...doesn't send any email

    Responding with I tried this too friend but doesn't work, is not helpful.  Debug your code by single stepping through the code and verify each line of code does what you expect.  If you had done this with the original code you should have noticed the SendEmaill method was never executed because chkSelect was always false.

    Monday, May 30, 2016 2:25 PM
  • User1806930132 posted

     If you had done this with the original code you should have noticed the SendEmaill method was never executed because chkSelect was always false.

    what does this mean?

    Monday, May 30, 2016 7:36 PM