Mobile Content

Monday, December 26, 2011

Mail the data to those mailid which is selected in datagridview using vb.net

Step:


1.First select the DataGridView control


2.Right click on DataGridView -> add column-> Select   DataGridViewCheckBoxColumn in Type


3.Set AllowUserToAddRows and AllowUserToDeleteRows  to false in DataGridView property.


4.Code to bind DataGridView



Imports System.Windows.Forms
Imports System.IO
Imports System.Net
Imports System.Data.SqlClient
Imports System.Net.Mail



Public Class frmListing



Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 Dim sqlcon as new sqlconnection("connectionstring")
dim cmd as new sqlcommand("select emailid from test")
dim da as new sqldataadapter(cmd)
dim ds as dataset()
da.fill(ds)
DataGridView1.Datasource=ds.Table(0)
DataGridView1.DataBind()    

    End Sub



End Class


5. Now code of send mail 
write code in button click event



Dim dr As DataGridViewRow
        For Each dr In DataGridView1.Rows
            If dr.Cells(0).Value Then


  Dim mailMsg1 As New Net.Mail.MailMessage
                    Dim fromAdd As Net.Mail.MailAddress
                    fromAdd = New Net.Mail.MailAddress(Fromadd,"display name" )
                    mailMsg1.From = fromAdd
                 mailMsg1.To.Add(


dr.Cells(0).Value



)

                    chk = dSet1.Tables(0).Rows(i).ItemArray(0).ToString()
                    mailMsg1.IsBodyHtml = True
                    mailMsg1.Priority = MailPriority.High
                    mailMsg1.Subject = "BarodaWeb" ' "New Task Assign!"

                    mailMsg1.Body = line
                    Dim smtp As New Net.Mail.SmtpClient()
                    smtp.Host = strMailhost

                    smtp.Port = strMailport
                    smtp.EnableSsl = True
                    smtp.UseDefaultCredentials = False
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network
                    smtp.Credentials = New NetworkCredential(your gmail id, password)

                    smtp.Send(mailMsg1)





 End If
        Next



No comments:

Post a Comment