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

End Sub

One thought on “VB Script to Send Mail From Outlook

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s