VB Script to Send Mail From Outlook

In our office we cannot create Outlook rules to forward emails outside the corporate network  (say to Gmail) automatically but you can write a VB script/Macro to do that. Although I have never done that as it might be illegal but this just explains that it is feasible!

The script basically is called whenever Outlook received a new email:

private Sub Application_NewMail()

 Dim newMail As MailItem
 Dim s As String
 Dim b As String
 Set newMail = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items.GetLast
 s = CStr(newMail.SenderName + ": " + newMail.subject)
 b = CStr(newMail.body + "")
 Call CreateEmail(s, b)
 'later you can delete these from your sent items folder

End Sub

Sub CreateEmail(subject As String, body As String)

Dim olApp As Object
 Dim OlMail As MailItem
 Dim ToRecipient As Variant
 Dim CcRecipient As Variant

Set olApp = Application
 Set OlMail = olApp.CreateItem(olMailItem)

OlMail.Recipients.Add "<me>@gmail.com"

OlMail.subject = subject
 OlMail.body = body
 OlMail.Send

End Sub
Advertisements