hi every one i need to send data gridview values as email body but i dont know how to do it. Could any one help. The values which i want to send are location,date,and price. here is my code

    Dim mail As New MailMessage

    mail.Subject = "hi"
    mail.To.Add("to@hotmail.com")
    mail.From = New MailAddress("from@hotmail.com")
    mail.Body = ??
    Dim smtp As New SmtpClient("smtp.live.com")
    smtp.EnableSsl = True
    smtp.Credentials = New System.Net.NetworkCredential("Myemail@hotmail.com", "My password")
    smtp.Port = "587"
    smtp.Send(mail)

Edited 4 Years Ago by Ezzaral: code formatting

Put the values from the row into Body tag of MailMessage class.
What you can do it to loop through the rows of dgv, and concat data from each row and send mail.

To do a loop you can do:

For Each row As DataGridViewRow In datagridview1.Rows
    'mail to send code and:         
    mail.Body = String.Format("Location: {0}, Date: {1}, Price: {2}", row.Cells(0).ToString(), row.Cells(1).ToString(), row.Cells(1).ToString())
    'i pass data from 1st, 2nd and 3rd column
    'and rest of the code..
Next

Edited 4 Years Ago by Mitja Bonca: changed code a bit

well , it is much better if you tell us about the column of your grid , now lets assume you have two columns in your gird column 1 is roll no and column two is name , now we gernate a email like this , hello!
this is auto gerenated email ,mr------ , your new roll no is ------- .
Regards,
for above format we can do like this .

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines
dim i as integer 
for i = 0 to datagrid.rows.count-1
dim name,rollno as string
name =  datagrid.item(1,i).value.tostring
rollno= datagrid.item(0,i).value.tostring
 'your email code here and  use this line in place of mail.body
 mail.Body ="hello!" & vbcrlf & "this is auto gerenated email ,mr "& name & ", your new roll no is "& rollno& "." & vbcrlf & "Regards."

next

hope this will give you idea , how to send emails after getting some values from the grid ,

Best Regards

well , it is much better if you tell us about the column of your grid , now lets assume you have two columns in your gird column 1 is roll no and column two is name , now we gernate a email like this , hello!
this is auto gerenated email ,mr------ , your new roll no is ------- .
Regards,
for above format we can do like this .

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines
dim i as integer 
for i = 0 to datagrid.rows.count-1
dim name,rollno as string
name =  datagrid.item(1,i).value.tostring
rollno= datagrid.item(0,i).value.tostring
 'your email code here and  use this line in place of mail.body
 mail.Body ="hello!" & vbcrlf & "this is auto gerenated email ,mr "& name & ", your new roll no is "& rollno& "." & vbcrlf & "Regards."

next

hope this will give you idea , how to send emails after getting some values from the grid ,

Best Regards

it is very hard to type code in this new format , please i am very sorry to post twice , and my code is like a comment please read it carefuly .

my data grid view columns are
1 bk_id
2 cust_id
3 location
4 date
5 payment

is it possible if you can help now by getting the idea of my column listing.

i want to copy location and date along with msgs like
hello Mr......
You being booked at "location" on "date"
thanks

You have a gridview, but you didnt tell us, if you want to send mail of each row, like i showed you in my post above?
This means to create a loop through rows, and using MailMessage class on each row.
If so, you can do:

For Each row As DataGridViewRow In dataGridView1.Rows
    Dim mail As New MailMessage()
    Dim SmtpServer As New SmtpClient("smtp.gmail.com")

    mail.From = New MailAddress("your_email_address@gmail.com")
    mail.[To].Add("to_address@mfc.ae")
    mail.Subject = "Test Mail"
    mail.Body = String.Format("hello Mr..." & vbCr & vbLf & "You being booked at {0} on {1}.", row("location").ToString(), row("date").ToString())

    SmtpServer.Port = 587
    SmtpServer.Credentials = New System.Net.NetworkCredential("username", "password")
    SmtpServer.EnableSsl = True

    SmtpServer.Send(mail)
Next

Hope this helps. you only dont forget to change important data with your own (your_email_address, subject, username and password (of your accout).

thanks a lot bro for asking this question if i want to send all rows as email. What i want to do is the most recent added row in datagridview as a booking ,i want to send that in my email. Because in the textboxes i have customer information, and in datagridview i have booking information of that customer,so gridview grows as i enter more rows so i want to send most recent booking,please can you tell me about that

also 1 more thing which i would like to ask here is . when i copy and pasted this line into my code

mail.Body = String.Format("hello Mr..." & vbCr & vbLf & "You being booked at {0} on {1}.", row("location").ToString(), row("date").ToString())
i got an error on "row"

error msg is "Error 1 'System.Data.SqlClient.Row' is not accessible in this context because it is 'Friend'.

disle4sk said:
What i want to do is the most recent added row in datagridview

this means only last row in dgv?
Is so, you can only do:

Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient("smtp.gmail.com")

mail.From = New MailAddress("your_email_address@gmail.com")
mail.[To].Add("to_address@mfc.ae")
mail.Subject = "Test Mail"
mail.Body = String.Format("hello Mr..." & vbCr & vbLf & "You being booked at {0} on {1}.", dataGridView1("location")(dataGridView1.Rows.Count - 1).Value.ToString(), dataGridView1("date")(dataGridView1.Rows.Count - 1).Value.ToString())

SmtpServer.Port = 587
SmtpServer.Credentials = New System.Net.NetworkCredential("username", "password")
SmtpServer.EnableSsl = True

SmtpServer.Send(mail)

Now i am getting error here

dataGridView1("location")

i replaced datagridview1 with my datagridview name "newbookingdatagridview" the error is

Error 1 Overload resolution failed because no accessible 'Item' accepts this number of arguments.

On which line of code does the error occur exactly?
Put a breakpoint and go line by line through thr code to find it.

the datagridview1 in this line causing error
"You being booked at {0} on {1}.", dataGridView1("location")

try it in this way:

String.Format("hello Mr..." & vbCr & vbLf & "You being booked at {0} on {1}.", dataGridView1.Cells("location").Rows(dataGridView1.Rows.Count - 1).Value.ToString(), dataGridView1.Cells("date").Rows(dataGridView1.Rows.Count - 1).Value.ToString())

Again i am getting an error on this line. This time the error is different
dataGridView1.Cells("location").Rows(dataGridView1.Rows.Count - 1).Value.ToString(),

Error is "cell is not a member of system.windows.form.gridview"

This article has been dead for over six months. Start a new discussion instead.