Solved

Filter email based on a serie of numbers contained in the email address

Posted on 2016-07-19
7
36 Views
Last Modified: 2016-07-19
Hello,
I am trying to find a way to automatically junk email which contain a series of numbers (I am receiving quite a lot these last weeks - all spams or worse) for instance mike.457@adomain.subdomain.com.
I would like to create a rule allowing the detect the numbers (ex. 457) and then move the email the a "junk" folder.
I have tried the MS Outlook "rules" but I did not succeed to establish a rule allowing this type of detection.
It is probably possible in VBA but do not know exactly how to manipulate the outlook object
thanks in advance
0
Comment
Question by:micmoo56
  • 4
  • 3
7 Comments
 
LVL 69

Expert Comment

by:Qlemo
ID: 41719016
You can't do that with rules, you need VBA code triggered by receiving mails. Triggering can be done by rule or VBA event handlers, but both have to act on all emails, there is no pre-filtering option.
0
 

Author Comment

by:micmoo56
ID: 41719024
Thanks but have you an VBA example I could use.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 41719052
Have been about to prepare one for you:
Option Explicit
Option Compare Text		' case insensitive - in case of using alphanumeric characters

Public WithEvents AdminInbox As Outlook.Items

Public Sub Application_Startup()
  ' The default inbox
  Set AdminInbox = Outlook.Session.GetDefaultFolder(olFolderInbox).Items
  ' if this is a secondary account, not your main one, with a different Inbox
  ' Set AdminInbox = Session.Folders("Administrator@domain.com").Folders("Inbox").Items
End Sub

Private Sub AdminInbox_ItemAdd(ByVal item as Object)
Dim str As String
Dim i As Integer
Dim noFrom As Variant, noTo As Variant, noSubject As Variant, noBody As Variant

  ' case insensitive. Available Patterns: ?*#[a-z][!a-z]

  noFrom = Array( _
            "*.co.ua", "*.biz.ua", "*girl_name*", "*@*.xyz", "*.#*@*" _
          )
  
  
  noTo = Array( _
            "*.#*@*" _
         )
  

  noSubject = Array( _
            "*help you save*", "*fw* order *", "*lottery*", "*breakthrough*", "*healthy*" _
          ) 
            
  noBody = Array( _
            "*proof of delivery * enclosed *", "* lux *", "*luxury*", "* gift *", "* gifts *", "* loan *", _
            "* mortgage *", "* deposit *" _
        )
        
  On Error Resume Next
  
  str = LCase(item.SenderEmailAddress)
  For i = 0 To UBound(noFrom)
    If str Like noFrom(i) Then item.Delete: Debug.Print: Debug.Print "  ### From: " & noFrom(i);: Exit Sub
  Next
  
  str = LCase(item.To)
  For i = 0 To UBound(noTo)
    If str Like noTo(i) Then item.Delete: Debug.Print: Debug.Print "  ### To: " & noTo(i);: Exit Sub
  Next
  
  str = Replace(str, "emailing: ", "")
  For i = 0 To UBound(noSubject)
    If str Like noSubject(i) Then item.Delete: Debug.Print: Debug.Print "  ### Subject: " & noSubject(i);: Exit Sub
  Next
  
  str = LCase(item.Body)
  For i = 0 To UBound(noBody)
    If str Like noBody(i) Then item.Delete: Debug.Print: Debug.Print "  ### Body: " & noBody(i);: Exit Sub
  Next
  
End Sub

Open in new window

This is derived from what I run, and contains some examples. It is able to scan FROM, TO, SUBJECT and BODY with the respective patterns.
The pattern character for a single digit is #, a ? is a single arbitrary character, * any amount (including none) of arbitrary characters. [a-z] are all (lowercase) letters, [!a-z]  everything but (lowercase) letters. Because I've set Option Compare Text, lower and upper case are treated the same.

The example code works as soon as an email is dropped into the default inbox. The commented example shows how you can use a different folder, e.g. for test.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:micmoo56
ID: 41719127
not sure I understand how to set this up. I have made an Alt+F11 and copy and paste you code in a new module but it seems it should be a class. could you explain me how you make it work  ? thanks in advance
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 41719225
As is, the above needs to be in the application main module, ThisOutlookSession.
Application_Startup is called only if you start Outlook. If you need to run it after having Outlook opened, position cursor inside of the sub and press F5 to execute.
0
 

Author Comment

by:micmoo56
ID: 41719243
Great it works - I have slightly modified the program and added a function to detect series of number (with the condition that the Clng conversion is above 10 - this is just arbitrary and allows some email with one digit to "pass the filter"
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 41719253
The pro with that VBA solution is that you are flexible, the con that you are flexible and have to code a lot :D.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question