Microsoft Outlook has been widely used to send and receive emails. But there are times the we need to connect our system to send information directly via email. Though it is possible also to send it directly without passing to Outlook, some users prefer it to send via Outlook so that they can monitor their emails easily.
Using Microsoft.Office.Interop, we can make a program that will connect to any MS Office application. This time, we will use it to send email directly from .Net application.
Here's the simple code on how to work with Outlook application;
Dim oEmail As Outlook.MailItem
Dim msoApp as New Outlook.Application
oEmail = DirectCast( msoApp .CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
With oEmail
.To = "emailaddress@email.com"
.CC = "CCemailaddress@email.com "
.Subject = "Your Mail Title"
.BodyFormat = Outlook.OlBodyFormat.olFormatPlain
.Body = "Your email content"
.Importance = Outlook.OlImportance.olImportanceHigh
.ReadReceiptRequested = True
Dim sAttachedmentPath As String = "C:\Attachments\sample.jpg"
.Attachments.Add( sAttachedmentPath , Outlook.OlAttachmentType.olByValue)
.Recipients.ResolveAll()
.Save()
.Display()
End With
You can use it on button event or as a function to call on to send email. However, before running the code, be sure you already setup your Outlook to send and receive emails. Also, you need to add the Microsoft.Office.Interop in the Solution reference folder of your project.
No comments:
Post a Comment