Read outlook mails and send mails using VB

Use this code to read outlook mails or send mails using VB.

Dim OlApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim oOApp As Outlook.Application
Dim oOMail As Outlook.MailItem
Dim InboxItems As Outlook.Items
Dim Mailobject As Object
Dim Subject As String
Dim Tomails As String

Private Sub ReadMails
    Set OlApp = CreateObject("Outlook.Application")
    Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
    Set InboxItems = Inbox.Items
    For Each Mailobject In InboxItems
        If Mailobject.UnRead Then 
            Subject = Mailobject.Subject
            SenderName = Mailobject.SenderName
            Tolist = Mailobject.To
             MailContent = Maililobject.Body        
            Mailobject.UnRead = False
           //Print all values or process as per your requirement
        End If
   Set OlApp = Nothing
   Set Inbox = Nothing
   Set InboxItems = Nothing
   Set Mailobject = Nothing
   Set TempRst = Nothing
End Sub

Private Sub SendMails
    Set oOApp = CreateObject("Outlook.Application")
    Set oOMail = oOApp.CreateItem(olMailItem)

    With oOMail
        .To = Tomails
        .Subject = "email subject"
        .Body = "email message"
        .Attachments.Add "path for filename", olByValue, 1
    End With
End Sub

By Perry    Popularity  (7463 Views)