הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Thursday, April 5, 2012 1:46 AM
hi all
How to send DataGridView contents by email in visual basic 2010
with regards
All replies (13)
Thursday, April 5, 2012 2:23 AM ✅Answered | 1 vote
The idea is to go through each DataGridViewRow and form HTML <table><tr> content. And use SMTP HTML enabled email. Below code prototypes this, add/edit as per your requirement
Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient("youremailserver")
mail.From = New MailAddress("your_email_address")
mail.[To].Add("to_address@emailaddress")
mail.Subject = "Grid View Content"
mail.BodyEncoding = Encoding.UTF8
mail.BodyFormat = MailFormat.Html
Dim strB As StringBuilder New StringBuilder()
'create html & table
strB.AppendLine("<html><body><center><" &
"table border='1' cellpadding='0' cellspacing='0'>")
strB.AppendLine("<tr>")
'cteate table header
For value As Integer = 0 To DataGridView.Columns.Count
strB.AppendLine("<td align='center' valign='middle'>" &
dg.Columns(i).HeaderText + "</td>");
Next
'create table body
strB.AppendLine("<tr>");
For value As Integer = 0 To DataGridView.Rows.Count
strB.AppendLine("<tr>");
For Each dgvc As DataGridViewCell in dg.Rows(i).Cells)
strB.AppendLine("<td align='center' valign='middle'>" & dgvc.Value.ToString() & "</td>")
Next
strB.AppendLine("</tr>")
Next
'table footer & end of html file
strB.AppendLine("</table></center></body></html>")
myMessage.Body = strB.ToString()
SmtpMail.Send(mail)
NB: Above code is just a prototype. Haven't tested/complied it, I just wrote directly over the Code Insert modal dialog.
A.m.a.L Hashim Dot Net Goodies |
Friday, April 6, 2012 6:14 AM ✅Answered | 1 vote
Hi Mind-Reader,
It's nice to see you here.
Hashim's reply is quite helpful, and you can use it with a few changes. Here is a full example below, you need to add a DataGridView and a button. Then change the email address to yours.
Imports Outlook = Microsoft.Office.Interop.Outlook
Imports System.Text
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' Create an Outlook application.
Dim oApp As Outlook._Application
oApp = New Outlook.Application()
' Create a new MailItem.
Dim oMsg As Outlook._MailItem
oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem)
oMsg.Subject = "Send DGV Using OOM in Visual Basic .NET"
oMsg.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
Dim strB As StringBuilder = New StringBuilder()
'create html & table
strB.AppendLine("<html><body><center><" &
"table border='1' cellpadding='0' cellspacing='0'>")
strB.AppendLine("<tr>")
'cteate table header
For value As Integer = 0 To DataGridView1.Columns.Count - 1
strB.AppendLine("<td align='center' valign='middle'>" & DataGridView1.Columns(value).HeaderText + "</td>")
Next
'create table body
strB.AppendLine("<tr>")
For value As Integer = 0 To DataGridView1.Rows.Count - 2
strB.AppendLine("<tr>")
For Each dgvc As DataGridViewCell In DataGridView1.Rows(value).Cells
strB.AppendLine("<td align='center' valign='middle'>" & dgvc.Value.ToString() & "</td>")
Next
strB.AppendLine("</tr>")
Next
'table footer & end of html file
strB.AppendLine("</table></center></body></html>")
oMsg.HTMLBody = strB.ToString()
' TODO: Replace with a valid e-mail address.
oMsg.To = "name@address"
'' Add an attachment
'' TODO: Replace with a valid attachment path.
'Dim sSource As String = "D:\Hello.txt"
'' TODO: Replace with attachment name
'Dim sDisplayName As String = "Hello.txt"
'Dim sBodyLen As String = oMsg.Body.Length
'Dim oAttachs As Outlook.Attachments = oMsg.Attachments
'Dim oAttach As Outlook.Attachment
'oAttach = oAttachs.Add(sSource, , sBodyLen + 1, sDisplayName)
' Send
oMsg.Send()
' Clean up
oApp = Nothing
oMsg = Nothing
'oAttach = Nothing
'oAttachs = Nothing
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("c1")
dt.Columns.Add("c2")
dt.Rows.Add("cell1", "cell2")
dt.Rows.Add("cell3", "cell4")
dt.Rows.Add("cell3", "cell4")
dt.Rows.Add("cell3", "cell4")
dt.Rows.Add("cell3", "cell4")
dt.Rows.Add("cell3", "cell4")
DataGridView1.DataSource = dt
End Sub
End Class
If anything is unclear, please feel free and feedback.
Best regards,
Shanks Zen
MSDN Community Support | Feedback to us
Thursday, April 5, 2012 2:56 AM
thanks hashim
i am tried the code but got error
i am using visual basic 2010
can explain? with full sample code
or any other idea?
with regards
Thursday, August 16, 2012 1:47 PM
Very cool
Monday, March 21, 2016 12:44 PM
Good morning all,
I have used your code above in my form, and it is retrieving the correct data which is perfect! how ever it wont display it correctly in the email body?
any ideas? Code is as below.
Private Sub ExportSendMailButton_Click(sender As Object, e As EventArgs) Handles ExportSendMailButton.Click
If Me.ExportReferenceTextbox.Text = "" Then
MessageBox.Show("Please enter a Reference ", "No Reference Entered",
MessageBoxButtons.OK, MessageBoxIcon.Error)
With Me.ExportReferenceTextbox
.Focus()
.SelectAll()
End With
Exit Sub ' tell it to skip the rest
End If
If Me.ExportClearedDateTextbox.Text = "" Then
MessageBox.Show("Please enter a Clearance Time ", "No Time Entered",
MessageBoxButtons.OK, MessageBoxIcon.Error)
With Me.ExportClearedDateTextbox
.Focus()
.SelectAll()
End With
Exit Sub ' tell it to skip the rest
End If
If Me.ExportClearedTextbox.Text = "" Then
MessageBox.Show("Please enter a Clearance Date ", "No Date Entered",
MessageBoxButtons.OK, MessageBoxIcon.Error)
With Me.ExportClearedTextbox
.Focus()
.SelectAll()
End With
Exit Sub ' tell it to skip the rest
End If
Dim Strpath As String
Strpath = "S:\1CLEARANCE ADVICE LISTS\Export PDFS"
Dim di As New DirectoryInfo(Strpath)
Dim arrDi As System.IO.FileInfo() = di.GetFiles("*.pdf")
Dim J As Integer
Dim strB As StringBuilder = New StringBuilder()
Dim dt As New DataTable
Dim OutlookMessage As Microsoft.Office.Interop.Outlook.MailItem
Dim AppOutlook As New Microsoft.Office.Interop.Outlook.Application
OutlookMessage = AppOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
Dim Recipents As Microsoft.Office.Interop.Outlook.Recipients = OutlookMessage.Recipients
OutlookMessage.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML
'create html & table
strB.AppendLine("<html><body><left><" &
"table border='1' cellpadding='0' cellspacing='0'>")
strB.AppendLine("<tr>")
'cteate table header
For value As Integer = 0 To ExportDataGridView.Columns.Count - 1
strB.AppendLine("<td align='left' valign='middle'>" & ExportDataGridView.Columns(value).HeaderText + "</td>")
Next
'create table body
strB.AppendLine("<tr>")
For value As Integer = 0 To ExportDataGridView.Rows.Count - 2
strB.AppendLine("<tr>")
For Each dgvc As DataGridViewCell In ExportDataGridView.Rows(value).Cells
strB.AppendLine("<td align='left' valign='middle'>" & dgvc.Value.ToString() & "</td>")
Next
strB.AppendLine("</tr>")
Next
'table footer & end of html file
strB.AppendLine("</table></left></body></html>")
OutlookMessage.SentOnBehalfOfName = "Clearance@martintrux.co.uk"
OutlookMessage.To = Me.CustomerExportsComboBox.SelectedValue
OutlookMessage.CC = "Clearance@martintrux.co.uk; RossEdwards@mt.local"
OutlookMessage.Subject = " Export Clearance Advice For Reference: " & Me.ExportReferenceTextbox.Text & " // Vehicle: " '& ExportDataGridView
OutlookMessage.Body = "Please See attached documents for your vehicle that cleared at: " & Me.ExportClearedTextbox.Text & " On " & Me.ExportClearedDateTextbox.Text & vbCrLf & Me.ParkingExamChargesTextbox.Text & vbCrLf & vbCrLf & strB.ToString() & vbCrLf & vbCrLf & "Regards" & vbCrLf & "Martintrux"
For J = 0 To UBound(arrDi)
If arrDi(J).Name.Contains(Me.ExportReferenceTextbox.Text) Then
OutlookMessage.Attachments.Add(IO.Path.Combine(Strpath, arrDi(J).Name))
End If
Next
OutlookMessage.Display()
OutlookMessage = Nothing
AppOutlook = Nothing
End Sub
it displays all the information required to make the table but looks like its not recognizing the html format?
Monday, March 21, 2016 12:53 PM
Hello,
First off since the issue is with formatting content this is either a problem with your HTML or how Outlook handles your HTML.
Without me actually trying the following it may or may not solve the problem (and don't know what part of the formatting is in question).
I noticed a definition tag but not the fully cooked e.g. you have dl tag but should place them into the following structure.
<dl>
<dt>Coffee</dt>
<dd>Black hot drink</dd>
<dt>Milk</dt>
<dd>White cold drink</dd>
</dl>
That might be the problem and fix or not along with other formatting tags that are done incorrectly or not supported.
Years ago HTML based content in programs were forgiving i.e. <li> or <p> without closers e.g. <li>some item</li> and now things are stricter so you might consider making sure all tags are closed and done correctly.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Monday, March 21, 2016 2:27 PM
I am way out of my depth here,
if i save the code produced in my email into a notepad and open it in explorer, it displays it exactly how i want it to be displayed,
i did notice a <tr> not having a </tr> after it and have put this in,
so the code i have now is,
Private Sub ExportSendMailButton_Click(sender As Object, e As EventArgs) Handles ExportSendMailButton.Click
If Me.ExportReferenceTextbox.Text = "" Then
MessageBox.Show("Please enter a Reference ", "No Reference Entered",
MessageBoxButtons.OK, MessageBoxIcon.Error)
With Me.ExportReferenceTextbox
.Focus()
.SelectAll()
End With
Exit Sub ' tell it to skip the rest
End If
If Me.ExportClearedDateTextbox.Text = "" Then
MessageBox.Show("Please enter a Clearance Time ", "No Time Entered",
MessageBoxButtons.OK, MessageBoxIcon.Error)
With Me.ExportClearedDateTextbox
.Focus()
.SelectAll()
End With
Exit Sub ' tell it to skip the rest
End If
If Me.ExportClearedTextbox.Text = "" Then
MessageBox.Show("Please enter a Clearance Date ", "No Date Entered",
MessageBoxButtons.OK, MessageBoxIcon.Error)
With Me.ExportClearedTextbox
.Focus()
.SelectAll()
End With
Exit Sub ' tell it to skip the rest
End If
Dim Strpath As String
Strpath = "S:\1CLEARANCE ADVICE LISTS\Export PDFS"
Dim di As New DirectoryInfo(Strpath)
Dim arrDi As System.IO.FileInfo() = di.GetFiles("*.pdf")
Dim J As Integer
Dim strB As StringBuilder = New StringBuilder()
Dim dt As New DataTable
Dim OutlookMessage As Microsoft.Office.Interop.Outlook.MailItem
Dim AppOutlook As New Microsoft.Office.Interop.Outlook.Application
OutlookMessage = AppOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
Dim Recipents As Microsoft.Office.Interop.Outlook.Recipients = OutlookMessage.Recipients
OutlookMessage.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML
'create html & table
strB.AppendLine("<html><body><left><" & "table border='2' cellpadding='2' cellspacing='2'>")
strB.AppendLine("<tr>")
'cteate table header
For value As Integer = 0 To ExportDataGridView.Columns.Count - 1
strB.AppendLine("<td align='left' valign='middle'>" & ExportDataGridView.Columns(value).HeaderText + "</td>")
Next
strB.AppendLine("</tr>")
'create table body
For value As Integer = 0 To ExportDataGridView.Rows.Count - 2
strB.AppendLine("<tr>")
For Each dgvc As DataGridViewCell In ExportDataGridView.Rows(value).Cells
strB.AppendLine("<td align='left' valign='middle'>" & dgvc.Value.ToString() & "</td>")
Next
strB.AppendLine("</tr>")
Next
'table footer & end of html file
strB.AppendLine("</table></left></body></html>")
OutlookMessage.SentOnBehalfOfName = "Clearance@martintrux.co.uk"
OutlookMessage.To = Me.CustomerExportsComboBox.SelectedValue
OutlookMessage.CC = "Clearance@martintrux.co.uk; RossEdwards@mt.local"
OutlookMessage.Subject = " Export Clearance Advice For Reference: " & Me.ExportReferenceTextbox.Text & " // Vehicle: " '& ExportDataGridView
OutlookMessage.Body = "Please See attached documents for your vehicle that cleared at: " & Me.ExportClearedTextbox.Text & " On " & Me.ExportClearedDateTextbox.Text & vbCrLf & vbCrLf & Me.ParkingExamChargesTextbox.Text & vbCrLf & vbCrLf & strB.ToString() & vbCrLf & vbCrLf & "Regards" & vbCrLf & "Martintrux"
For J = 0 To UBound(arrDi)
If arrDi(J).Name.Contains(Me.ExportReferenceTextbox.Text) Then
OutlookMessage.Attachments.Add(IO.Path.Combine(Strpath, arrDi(J).Name))
End If
Next
OutlookMessage.Display()
OutlookMessage = Nothing
AppOutlook = Nothing
End Sub
which produces the email code of,
Please See attached documents for your vehicle that cleared at: 2000 On 20/03/2016
<html><body><left><table border='1' cellpadding='0' cellspacing='0'>
<tr>
<td align='left' valign='middle'>Reference</td>
<td align='left' valign='middle'>Exporter</td>
<td align='left' valign='middle'>EPU</td>
<td align='left' valign='middle'>Entry</td>
<td align='left' valign='middle'>Date</td>
<td align='left' valign='middle'>MRN</td>
<td align='left' valign='middle'>Vehicle</td>
<td align='left' valign='middle'>Pkgs</td>
<td align='left' valign='middle'>Gross</td>
<td align='left' valign='middle'>Route</td>
<tr>
<tr>
<td align='left' valign='middle'>DE124315A</td>
<td align='left' valign='middle'>EUROPRIDE LTD</td>
<td align='left' valign='middle'>011</td>
<td align='left' valign='middle'>A02582W</td>
<td align='left' valign='middle'>15/03/16 00:00:00</td>
<td align='left' valign='middle'>16GB03X57775707030</td>
<td align='left' valign='middle'>AB84GYR / AB62GIR</td>
<td align='left' valign='middle'></td>
<td align='left' valign='middle'>3378.500</td>
<td align='left' valign='middle'>6</td>
</tr>
<tr>
<td align='left' valign='middle'>DE124315B</td>
<td align='left' valign='middle'>EUROPRIDE LTD</td>
<td align='left' valign='middle'>011</td>
<td align='left' valign='middle'>A02585F</td>
<td align='left' valign='middle'>15/03/16 00:00:00</td>
<td align='left' valign='middle'>16GB03X57775961017</td>
<td align='left' valign='middle'>AB84GYR / AB62GIR</td>
<td align='left' valign='middle'></td>
<td align='left' valign='middle'>2970.000</td>
<td align='left' valign='middle'>6</td>
</tr>
<tr>
<td align='left' valign='middle'>DE124315C</td>
<td align='left' valign='middle'>EUROPRIDE LTD</td>
<td align='left' valign='middle'>011</td>
<td align='left' valign='middle'>A02587V</td>
<td align='left' valign='middle'>15/03/16 00:00:00</td>
<td align='left' valign='middle'>16GB03X57776136010</td>
<td align='left' valign='middle'>AB84GYR / AB62GIR</td>
<td align='left' valign='middle'></td>
<td align='left' valign='middle'>5080.250</td>
<td align='left' valign='middle'>6</td>
</tr>
<tr>
<td align='left' valign='middle'>DE124315D</td>
<td align='left' valign='middle'>EUROPRIDE LTD</td>
<td align='left' valign='middle'>011</td>
<td align='left' valign='middle'>A02590B</td>
<td align='left' valign='middle'>15/03/16 00:00:00</td>
<td align='left' valign='middle'>16GB03X57776358011</td>
<td align='left' valign='middle'>AB84GYR / AB62GIR</td>
<td align='left' valign='middle'></td>
<td align='left' valign='middle'>3415.000</td>
<td align='left' valign='middle'>6</td>
</tr>
<tr>
<td align='left' valign='middle'>DE124315E</td>
<td align='left' valign='middle'>OLD DOVER TEXTILES LTD</td>
<td align='left' valign='middle'>011</td>
<td align='left' valign='middle'>A02593J</td>
<td align='left' valign='middle'>15/03/16 00:00:00</td>
<td align='left' valign='middle'>16GB03X57776630011</td>
<td align='left' valign='middle'>AB84GYR / AB62GIR</td>
<td align='left' valign='middle'></td>
<td align='left' valign='middle'>1241.500</td>
<td align='left' valign='middle'>6</td>
</tr>
</table></left></body></html>
Regards
Martintrux
Monday, March 21, 2016 2:31 PM | 1 vote
Hello,
By rendering the html contents in a web browser does not guarantee you will have similar results in Microsoft Outlook.
Matter of fact when designing for the web each browser may have different rendering of HTML (not pointing to your content but overall).
Outlook has it's own internal methods for displaying html.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Monday, March 21, 2016 2:38 PM
OK understood,
So where does that leave me?
i have gone through my code over and over i cant understand why it wont work? its practically the same as above code and i believe it worked for Mind-Reader.
Monday, March 21, 2016 2:58 PM
I have worked out where i am using .body = and not htmlbody = it is displaying all the data,
but the problem is, i want to display text, tables and textbox values, so how would i go about displaying all 3?
Monday, March 21, 2016 3:12 PM
This reply is for a added on question for March 2016 for Ross Edwards.
Okay, you can try another option to use SMTP. When I setup this way the results are shown as follows in the image below which I sent myself an email from one of my personal addresses to another one.
I copied your content and used it as my message body.
Here is a page to describe how to send via SMTP.
Now on a side note, when using SMTP you can setup for redirecting messages to a file for easy testing which I explain here (but is in C#) and if interested I can take my C# code and create a VB.NET version but best to first look at the link above for a simple example.
In closing, yes this is not Outlook automation but it worked for me in Outlook 2013 when receiving the message as indicated in the image above.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Monday, March 21, 2016 3:48 PM
Thanks for your help,
this is getting all too confusing for me,
is SMTP the easiest way of sending, text, Textboxvalues, and html tables in one email?
Monday, March 21, 2016 3:57 PM
SMTP is much easier in my opinion than using MS-Outlook automation. With SMTP Outlook need not be installed as SMTP knows nothing about the email program.
Also, with the option of Pickup folder we can examine the file produced for sending and Outlook can't do this.
Partial example of a file generated via SMTP of your message
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator