Hello can someone help me,
How i can fetch the items from datagridview to attach them to email
How it will be in function SendMail()
Can someone tell me is it correct now , how to read files from Datagridview in attachment
Button Browse:
Is it correct to attach the files from the datagridview to the email ?
How i can fetch the items from datagridview to attach them to email
How it will be in function SendMail()
Can someone tell me is it correct now , how to read files from Datagridview in attachment
VB.NET:
Public Sub SendMail()
Dim oEmail As MailItem = CType(New Application().CreateItem(OlItemType.olMailItem), MailItem)
oEmail.Recipients.Add("emailTo") 'Send TO
oEmail.CC = "CopyEmail" 'Send CC
oEmail.Recipients.ResolveAll()
oEmail.Subject = "Spam - Meow!"
oEmail.BodyFormat = OlBodyFormat.olFormatHTML
oEmail.Body = "Blah, blah, blah..."
oEmail.Importance = OlImportance.olImportanceNormal 'Request read email confirmation
oEmail.ReadReceiptRequested = True
If DataGridView1.Rows.Count > 0 Then
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells("Name").Tag IsNot Nothing Then
Dim MsgAttach As New Attachment(CStr(row.Cells("Name").Tag))
oEmail.Attachments.Add(MsgAttach)
End If
Next
End If
oEmail.Recipients.ResolveAll()
oEmail.Save()
oEmail.Display(False) 'Show the email message and allow for editing before sending
oEmail.Send() 'You can automatically send the email without displaying it.
End Sub
VB.NET:
OpenFileDialog1.FileName = "Folder1"
OpenFileDialog1.DefaultExt = "*.*"
OpenFileDialog1.FilterIndex = 6
OpenFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
OpenFileDialog1.Multiselect = True
Dim saveDirectory As String = ".\Files\"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
If Not Directory.Exists(saveDirectory) Then
Directory.CreateDirectory(saveDirectory)
End If
For Each item As String In OpenFileDialog1.FileNames
Dim fileName As String = Path.GetFileName(OpenFileDialog1.FileName)
Dim fileSavePath As String = Path.Combine(saveDirectory, fileName)
File.Copy(OpenFileDialog1.FileName, fileSavePath, True)
Next
End If
BindDataGrid()
VB.NET:
Private Sub BindDataGrid()
Dim filePaths As String() = Directory.GetFiles(".\Files\")
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Text")
dt.Columns.Add("Value")
For Each filePath As String In filePaths
dt.Rows.Add(IO.Path.GetFileName(filePath), filePath)
Next
dataGridView1.AllowUserToAddRows = False
dataGridView1.Columns.Clear()
Dim name As DataGridViewColumn = New DataGridViewTextBoxColumn With {
.Name = "Name",
.HeaderText = "File Name",
.DataPropertyName = "Text",
.Width = 100
}
DataGridView1.Columns.Insert(0, name)
Dim path As DataGridViewColumn = New DataGridViewTextBoxColumn With {
.Name = "Path",
.HeaderText = "File Path",
.DataPropertyName = "Value",
.Width = 100
}
DataGridView1.Columns.Insert(1, path)
DataGridView1.DataSource = dt
Dim buttonColumn As DataGridViewButtonColumn = New DataGridViewButtonColumn With {
.HeaderText = "",
.Width = 60,
.Name = "buttonColumn",
.Text = "Delete",
.UseColumnTextForButtonValue = True
}
DataGridView1.Columns.Insert(2, buttonColumn)
End Sub
VB.NET:
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.ColumnIndex = 2 Then
Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
If MessageBox.Show(String.Format("Do you want to delete Name:{0}?", row.Cells("Name").Value), "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
File.Delete(row.Cells("Path").Value.ToString())
BindDataGrid()
End If
End If
End Sub
Is it correct to attach the files from the datagridview to the email ?