Email Forwarding

T

TerryM

I have an question, I'm relatively new to VBA coding. Especially in Outlook,
I am running an Exchange 2003 server using Outlook 2003 clients.

What I am needing is code that will run when a specific sender sends an
email to me with a specific (text) in the body of the email. I then want it
forward/email to an outside email recipient.

I am aware that you can activate automatic email forwarding in the Exchange
server, however I view this as a major security risk.

Any assistance would be greatly welcomed
 
K

Ken Slovak - [MVP - Outlook]

You can certainly write code for that but it probably would be much easier
for you to just create a rule to do that. Start with a blank rule and select
the conditions for certain senders and specified text in the subject, then
select the forward action for the rule.
 
T

TerryM

What you wrote is totally true except for the fact that Exchange by default
disables automatic forwarding to external sources. That is the reason why I
was wanting code to basically take the email with the specific sender and
text in the body and automatically create a new email and send it out. This
way Exchange doesn't view it as a forward to an outside location.

You can manually send to outside locations, however you can't automatically
forward emails to outside locations without enabling this function. And that
security wise is something I do not want to do.
 
K

Ken Slovak - [MVP - Outlook]

In that case handle either the NewMailEx event or the Item.Add event on the
Items collection of the Inbox. If you handle NewMailEx then you'd get a
string of all the EntryID's of the incoming items, separated by commas. You
can use the Split function to split those into an array of EntryID's. Use
NameSpace.GetItemFromID with each EntryID to get the item, then use string
functions to parse the Body looking for your catch phrase. Also check for
SenderName and/or SenderEmailAddress to see who sent the item to you. Then
use the Forward method to send the item wherever you want.
 
T

TerryM

That's more of what I was looking for, but like I stated before. I'm
relatively new to VBA programming especially in Outlook. Were more of a
Delphi place, what would the code that I'm requiring look like?
 
K

Ken Slovak - [MVP - Outlook]

Something like this, although this has no real error handling and needs to
be tweaked so there's no possibility of an endless loop. This code would go
into the ThisOutlookSession class module in the Outlook VBA project.

You would of course have to sign the project or lower macro security to have
it run.

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
Dim aID() As String
Dim oMail As Outlook.MailItem
Dim oFwd As Outlook.MailItem
Dim oNS As Outlook.NameSpace
Dim oRecip As Outlook.Recipient
Dim obj As Object
Dim i As Long
Dim sBody As String

Const SEARCH_FOR As String = "Foobar"
Const SEARCH_ADDY As String = "(e-mail address removed)"
Const FORWARD_TO As String = "(e-mail address removed)"

Set oNS = Application.GetNamespace("MAPI")

On Error Resume Next

aID = Split(EntryIDCollection, ",")
For i = LBound(aID) To UBound(aID)
Set obj = oNS.GetItemFromID(aID(i))
If obj.Class = olMail Then
Set oMail = obj
sBody = oMail.Body
If ((InStr(1, sBody, SEARCH_FOR, vbTextCompare) > 0) And _
(oMail.SenderEmailAddress = SEARCH_ADDY)) Then

Set oFwd = oMail.Forward
Set oRecip = oFwd.Recipients.Add(FORWARD_TO)
oRecip.Resolve
If oRecip.Resolved Then
oFwd.Send
End If

End If
End If

Set oMail = Nothing
Set oFwd = Nothing
Set oRecip = Nothing
Set obj = Nothing
Next

Set oNS = Nothing
End Sub
 
T

Tomas

Hello Ken,
may be you could help me with similar Issue i think i can not handle easily.
I am not sure which way to go on incoming email and specific text in subject.

i see i have two options. On incoming email with text in Subject either run
script or make auto reply directly in outlook. What i am looking for is to
take a piece subject of email i got and repost it.

The incoming email subject looks like "Service call 10745707 has been
assigned to you."

What i would like to do is to reply to this emial like:
_send to: (e-mail address removed)
_subject: update 10745707
_Body: status=In Progress



I also though i could run a script on this like start application excel,
data to columns and then the code for send email. Anyway this looks to me
kinda to complicated and also when running this script my pc must be always
on.

Could you please help me with this issue?
Thank you in advance
Best regards
Tomas
 
K

Ken Slovak - [MVP - Outlook]

Please do not hijack other people's threads. What you are asking has nothing
to do with this thread.

If you want code to run obviously your computer must be running.

You can use an ItemAdd() handler or a NewMailEx() handler in the Outlook VBA
project and get the properties you want from the incoming item and use
Application.CreateItem(olMail) to create a new email. You can then populate
that item, say it's oMail as desired: oMail.Subject = "whatever",
oMail.Recipients.Add("(e-mail address removed)"), oMail.Body = "status=In Progress",
etc. You can then call Send() on the item to send it out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top