Mailing the Excel file using Outlook
In continuation from here…
Before you mail a file from Outlook, click on the menu Project~~>Add Reference. You will be presented with a “Add Reference” dialog box. Navigate to the “COM” Tab and select the Microsoft Outlook Object Library. Once done simply use this code. Amend as applicable.
Imports Excel = Microsoft.Office.Interop.Excel Imports Outlook = Microsoft.Office.Interop.Outlook Public Class Form1 '~~> Define your Outlook Objects Dim objOutlook As New Outlook.Application Dim objEmail As Outlook.MailItem Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim body As String Dim ExcelFile As String = "C:\Tutorial\Sample.xlsx" objOutlook = CType(CreateObject("Outlook.Application"), Outlook.Application) objEmail = objOutlook.CreateItem(Outlook.OlItemType.olMailItem) body = "Hello World," & vbCrLf & vbCrLf body += "This is my First email from vb.net. Please find attached the Excel file" & vbCrLf & vbCrLf body += "Regards," & vbCrLf & "<Your Name Goes Here>" With objEmail .Subject = "My First Email" .To = "EveryOneInThisWorld@Universe.Com" .Body = body .Attachments.Add(ExcelFile) '~~> Show on screen .Display(True) '~~> Uncomment the below to actually mail the file '.Send() End With End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Me.Close() End Sub End Class
This is how you email will look like if you ran the above code