Results 1 to 12 of 12

Thread: How to move an incoming mail to a folder depending on mail header content?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Linkoping, Sweden
    Posts
    50

    How to move an incoming mail to a folder depending on mail header content?

    Does anyone know if it is possible to access the mail header information from VBA in Outlook 2002?

    Our incoming spam are marked with X-Spam-Flag in the header information and it would be really nice to be able to trash these mails automatically.

    This is, unfortunatly, not working with the built-in rules in Outlook. Those rules only work with Subject, body and sending address not with mail header as far as I can understand.

    /Anders

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Can you post the header info and other details so I can give you
    an example?

    You can wite some VBA behind Outlook to inspect the email info
    and delete it if it finds that data in the header. This will be written
    in the Application_NewMail() procedure.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Linkoping, Sweden
    Posts
    50

    mail header

    The mails I want to trash (or move to a folder) looks like this in their header: (and it is the string "X-Spam-Flag: YES" we are looking for!)

    X-Spam-Level: ******
    X-Spam-Status: Yes, hits=6.8 required=5.0 tests=FAKED_UNDISC_RECIPS,
    FROM_ENDS_IN_NUMS,PLING,PLING_PLING,EXCUSE_6,HTML_WITH_BGCOLOR,
    BIG_FONT version=2.20
    X-Spam-Flag: YES
    X-Spam-Checker-Version: SpamAssassin 2.20

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    This mail comes in as HTML or Plain RTF?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Can you save it as a text file and attach it to a post for me?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Linkoping, Sweden
    Posts
    50

    more info

    here is the complete header (I think): (it seems as the mail is HTML)
    ------------------------------------------------------------------
    Return-path: <[email protected]>
    Received: from mail.liu.se (mail.liu.se [130.236.1.35])
    by ims.student.liu.se (iPlanet Messaging Server 5.2 HotFix 1.03 (built Oct 1
    2002)) with ESMTP id <[email protected]> for
    andfa93%bibl.liu.se@ims-ms-daemon; Wed, 14 May 2003 08:03:24 +0200 (MEST)
    Received: by mail.liu.se (Postfix, from userid 506) id 5931D1FEF4; Wed,
    14 May 2003 08:03:24 +0200 (CEST)
    Received: from sarek.skynet.be (sarek.skynet.be [195.238.3.230])
    by mail.liu.se (Postfix) with ESMTP id 4FF621FED1 for <[email protected]>;
    Wed, 14 May 2003 08:03:23 +0200 (CEST)
    Received: from server01.muyshondt.be
    (159.246-201-80.adsl-fix.skynet.be [80.201.246.159]) by sarek.skynet.be
    (8.12.9/8.12.9/Skynet-OUT-2.21) with ESMTP id h4E63GmE010853 for
    <[email protected]>; Wed,
    14 May 2003 08:03:16 +0200 (envelope-from <[email protected]>)
    Received: from smtp0311.mail.yahoo.com ([200.31.145.40])
    by server01.muyshondt.be with Microsoft SMTPSVC(5.0.2195.5329); Wed,
    14 May 2003 07:59:34 +0200
    Date: Wed, 14 May 2003 05:59:25 +0000 (GMT)
    From: lileana <[email protected]>
    Subject: Show Me
    To: [email protected]
    Message-id: <[email protected]>
    MIME-version: 1.0
    Content-type: text/html; charset=us-ascii
    Content-transfer-encoding: base64
    X-Priority: 3
    X-Spam-Status: Yes,
    hits=10.3 required=5.0 tests=BASE64_ENC_TEXT,CLICK_BELOW,EARN_MONEY,FROM_ENDS_IN_NUMS,
    HTML_10_20,HTML_MESSAGE,MAILTO_LINK,MAILTO_TO_REMOVE,
    MAILTO_WITH_SUBJ,MAILTO_WITH_SUBJ_REMOVE,MIME_HTML_ONLY,
    MLM,PRIORITY_NO_NAME,SUBJ_REMOVE version=2.52-liu_1.4
    X-Spam-Level: **********
    X-Spam-Checker-Version: SpamAssassin 2.52-liu_1.4 (1.174.2.8-2003-03-24-exp)
    X-Spam-Report: ---- Start SpamAssassin results 10.30 points,
    5 required; * 0.6 -- From: ends in numbers * 1.0 -- BODY: Message talks
    about earning money * 0.9 -- BODY: Multi Level Marketing mentioned * 1.1 --
    BODY: List removal information * 1.0 -- BODY: Message is 10% to 20% HTML *
    0.2 -- BODY: HTML included in message * 1.1 -- BODY: mailto URI includes
    removal text * 0.1 -- BODY: Includes a URL link to send an email * 2.7 --
    RAW: Message text disguised using base-64 encoding * 0.4 -- URI: Includes a
    link to send a mail with a subject * 0.3 -- URI: Includes a 'remove' email
    address * 0.2 -- Asks you to click below * 0.6 -- Message has priority
    setting, but no X-Mailer * 0.1 -- Message only has text/html MIME parts ----
    End of SpamAssassin results
    X-Spam-Flag: YES
    Original-recipient: rfc822;andfa%[email protected]
    X-OriginalArrivalTime: 14 May 2003 05:59:43.0981 (UTC)
    FILETIME=[042A21D0:01C319DE]

    --------------------------------------------------------------------
    And here is the mail text itself:
    ---------------------------------------------------------------------------------
    Hello [email protected]

    If You TRULY NEED...... $2,000 to $10,000 or More
    Within The Next 7 Days....

    WE HAVE THE SOLUTION! This Thing Is FAST!!!

    No Selling! No MLM or Network Marketing! No Boards!
    No Matrix! No Binary! No Monthly Purchases!


    ** New! ** THIS IS NOTHING YOU HAVE SEEN BEFORE!

    IT IS SOMETHING UNIQUELY DIFFERENT....
    An Extremely Powerful Cash Generating System...
    That is changing the world one family at a time.


    Mark Received $30,000 His first 2 Weeks!
    Ted Received $7,000 His First 24 Hours!
    Mike Received $12,000 His First 3 Days!
    Linda Received $10,000 Her First Month!
    Steve Received $5,000 In His First 24 Hours!


    These are just a FEW of the THOUSANDS of ordinary
    people, JUST LIKE YOU, who decided to take a look
    at this and have Changed Their Lives in DAYS!

    AND YOU CAN TOO!

    If you feel $2,000+ Per Day is worth 2 minutes of your
    time, Please Call Me With Your Contact Information:

    I WILL NOT try to convince you of anything. I'll only
    pass the information on to you.

    Only YOU can decide if this is for you....
    Only YOU can take the necessary steps to IMMEDIATELY
    and DRAMATICALLY change your financial life!!!


    A Week From Now, You Can Have THOUSANDS In Your Hands
    OR, Be Just As You Are Today. THE CHOICE IS YOURS!

    You owe it to yourself AND your family to AT LEAST
    take a peek at this!!!


    Please do not reply to this email.

    Call 1-888-881-5804

    and leave the following information:

    Name:

    Phone:

    Best Time to Call:

    Time Zone or State:

    I'll contact you as soon as I hear from you.

    God Bless & Much Success,
    Chris






    You are receiving this message as someone who is interested
    in online or offline business opportunities. If this email
    has reached you in error, please accept our apologies and
    click on the remove link below:
    [email protected], and your name
    will be removed from our Data Base. Please allow at least
    48-72 hours to be removed.

    Thank you!

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    This will get you into all the properties of the email. It will loop
    through your Inbox until it marks all new emails as read. Modify it
    to do functions on the (.Body or .SentOnBehalfOfName) with Instr.
    This is searching the .HTMLBody properties. If the spam is rtf or
    plain then search the .Body properties.
    This will give you total control on the email, way better than Rules.
    Code:
    Private Sub Application_NewMail()
    
        Dim oNS As Outlook.NameSpace
        Dim oInbox As Outlook.MAPIFolder
        Dim oEmail As Outlook.MailItem
        Dim i As Integer
        
        Set oNS = Application.GetNamespace("MAPI")
        Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
        
        If oInbox.UnReadItemCount > 0 Then MsgBox "New Email!", vbOKOnly + vbInformation
        Do While i < oInbox.Items.Count And oInbox.UnReadItemCount > 0
            i = i + 1
            DoEvents
            If oInbox.UnReadItemCount > 0 Then
                Set oEmail = oInbox.Items.Item(i)
                If Instr(1,oEmail.HTMLBody, "If You TRULY NEED...... $2,000 to $10,000 or More") > 0 Then
                    oEmail.Delete
                Else
                    If oEmail.UnRead = True Then oEmail.UnRead = False
                Endif
                Set oEmail = Nothing
            End If
            Set oInbox = Nothing
            Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
        Loop
    
        Set oInbox = Nothing
        Set oNS = Nothing
    
    End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Linkoping, Sweden
    Posts
    50

    OK

    Thanks for the code. This will really get me started in Outlook VBA programming. I am waiting for a few ordered books on this topic. I have done most VB6 and VB.NET so far.

    But will your sample code really work with the mail header information? This was my initial thought as our university is using Spam-Assassin and marks all incoming mail with "X-Spam-Flag: Yes" if it is a suspected spam.

    I'll start testing now anyway.

    Thanks again and let me know if you have plans on visiting Linköping (my home town).

    Best regards,
    Anders
    Linköping, Sweden

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Linkoping, Sweden
    Posts
    50

    the code works fine!

    But I still don't get how to find the header information where the important information is.

    In other email clients, like Eudora, it works with the built in Rules or filter functions.

    Thanks again, but we haven't really solved my problem.

    /Anders

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Does Spam-Assassin have any configurations to have it put that
    marking in the body text or subject?

    You may need to look at writting something on the Exchange
    Server and stop it there, if possible.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Linkoping, Sweden
    Posts
    50

    It works with Outlook rules!

    It does actually work with the Outlook rules! I didn't understand at first because of a bad translation of mail header to Swedish (I am using Swedish version of Outlook). The translation made you think it concerned Subject rather than Mail header.

    In the rules whizard you can click next in the first dialog and then a bit further down, you can choose to filter on mail header content. And this works with the mails that are marked with the Spam-Assassin flag! :-)

    Thanks anyway for your interest and I have learnt how to handle incoming mails in VBA! That's just great!

    /Anders

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Linkoping, Sweden
    Posts
    50

    [Resolved]

    :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width