More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

Login

login
password
Remember me

You will need to register and log in if you want to download the source code for the Microsoft Outlook Programming book. The forums and code sharing areas are open to both registered and non-registered visitors.

share code 02-Sep-2010 22:32

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

 

Code level: beginner    Code area: Basic Outlook Printer Friendly Version
Title: Report Spam!
Description: Sends copy of e-mail body and full Internet header to sender's mail host, then deletes original message. This is just a couple hours' of very quick and dirty coding, could certainly be easily improved. (Hint: After I compiled it and tested it, I threw a button on the Message toolbar--not the maon Outlook toolbar--so I can just open a message, click an icon, and it runs!) Might be security issues with some people's installations, ie, allowing macros to run, allowing the code to send an e-mail, etc.
Date: 19-May-2003  05:44
Code level: beginner
Code area: Basic Outlook
Posted by: Brian Battles
Body:
All 27comments
Page [ 1 2 3 Next >>  
  07-Nov-2003  12:10   
Sorry, new at this, but I get on a compile error on this: Dim objSession As MAPI.Session

Also, this is a bit more complicated that I think I need. All I need to do is forward an email to one specific address, then delete the email. Is something like that available?
  07-Nov-2003  18:36   
Did you add a reference to the Collaboration Data Objects (CDO) library to your project? That's what you need to be able to use MAPI.* declarations.

Forwarding and deleting is much, much simpler than what you have here. Once you have the message you want to forward and delete as an object variable (e.g. objMail), it's just:

    Set objForward = objMail.Forward
    objForward.To = "someome@somewhere.com"
    objForward.Send ' [1]
    objMail.Delete
    set objMail = Nothing
    Set objForward = Nothing

[1] Will trigger security prompt -- see http://www.slipstick.com/outlook/esecup.htm#autosec
  14-May-2004  13:44   
(Help) Forwarding spam as attachment.

My office is using MDaemon as our email server. It's spam reporter requires that the spam email be forwarded as an attachment. I created an Outlook 2000 macro to create a new mail item, attach the selected mail item to the new mail item, send the new mail item to "spam@somewhere.com" and delete the selected mail item.

The only problem I have is that MDaemon sends me back a reply saying:
 "Incorrect Message Format"... "The message(s) you submitted to the spam or ham learning system were not accepted. You must submit these messages to MDaemon as attachments of type message/rfc822. When using Outlook Express use the 'Forward as attachment' feature."

Here is the code I have:

'############# Outlook 2000 Spam Reporting Macro ##################
' Select the Mail Item you want to report to spam and run this code

Public Sub SendToSpam()
    Dim objOL As Outlook.Application
    Dim objSelection As Outlook.Selection
    Dim objMsg As Object
    Dim objNewMsg As Object
    
    On Error Resume Next

    ' Instantiate an Outlook Application object
    Set objOL = CreateObject("Outlook.Application")
    ' Get the collection of selected objects
    Set objSelection = objOL.ActiveExplorer.Selection

    ' This code sends the selected mail items
    For Each objMsg In objSelection
        ' This code only sends mail items
        If objMsg.Class = olMail Then
            ' Create a new mail Item
            Set objNewMsg = Application.CreateItem(olMailItem)
            ' send the new mail item to the spam reporting email address
            objNewMsg.To = "spam@somewhere.com"
            ' add selected mail item as attachment to new mail item
            objNewMsg.Attachments.Add objMsg
            ' send the new mail item
            objNewMsg.Send
            
            ' Clear the New Mail Item object
            Set objNewMsg = Nothing
            ' Delete the spam mail item
            objMsg.Delete
        End If
    Next

ExitSub:
    Set objMsg = Nothing
    Set objSelection = Nothing
    Set objOL = Nothing
End Sub

'############# End of Outlook 2000 Spam Reporting Macro ###########


I have searched for two days to find a way to make this work... any help would be appreciated!
  14-May-2004  14:35   
Addition:

I wanted to point out that the new mail item that is created looks just like a new mail item that I create without Automation by clicking the new mail button and then dragging the spam mail onto the new mail item and clicking send.

I also tried [automation] saving the spam email item as a file and attaching the file to the new mail item with the following modified code:

'############# Outlook 2000 Spam Reporting Macro ##################
' Select the Mail Item you want to report to spam and run this code

Public Sub SendToSpam()
    Dim objOL As Outlook.Application
    Dim objSelection As Outlook.Selection
    Dim objMsg As Outlook.MailItem
    Dim objNewMsg As Outlook.MailItem
    Dim FSO As FileSystemObject
    
    On Error Resume Next

    ' Instantiate an Outlook Application object
    Set objOL = CreateObject("Outlook.Application")
    ' Get the collection of selected objects
    Set objSelection = objOL.ActiveExplorer.Selection

    ' This code sends the selected mail items
    For Each objMsg In objSelection
        ' This code only sends mail items
        If objMsg.Class = olMail Then
        
            'save as temp file
            objMsg.SaveAs "c:\temp.msg"
        
            ' Create a new mail Item
            Set objNewMsg = Application.CreateItem(olMailItem)
            
            ' send the new mail item to the spam reporting email address
            objNewMsg.To = "spam@somewhere.com"
            ' add selected mail item as attachment to new mail item
            objNewMsg.Attachments.Add "c:\temp.msg"
            ' send the new mail item
            objNewMsg.Send
            
            ' delete the temp file
            Set FSO = New FileSystemObject
            FSO.DeleteFile "c:\temp.msg"
            Set FSO = Nothing
            
            ' Clear the New Mail Item object
            Set objNewMsg = Nothing
            ' Delete the spam mail item
            objMsg.Delete
        End If
    Next

ExitSub:
    Set objMsg = Nothing
    Set objSelection = Nothing
    Set objOL = Nothing
End Sub

'############# End of Outlook 2000 Spam Reporting Macro ###########
  15-Jun-2004  13:27   
Is there a way to get the internet headers from the mail message without using CDO or other external libraries/tools like Redemption. I would prefer to do this in .NET/C#, but I am perfectly capable of programming much closer to the metal (C/C++) if necessary.
  16-Jun-2004  06:09   
Yes, John, you could use Extended MAPI. See http://www.outlookcode.com/d/mapi.htm for references .
  16-Jun-2004  07:27   
Thanks!
  31-Oct-2004  13:36   
Hi, I'm using VB 5 and Outlook 9 (yeah, I know:). It seems I don't have the Fields property (object?) in either of them. How can I reach the pr_transport_message_headers then?

Was thinking to keep a spam folder under the inbox to move the messages to, so I don't have to open them one by one. The macro could then purge the spam folder and the button could be placed on the main toolbar.

Another idea is to download the www index page from the server obtained through the message-id, look for any email addresses on that page and also send a spam report to those addresses (or at least one), provided they're on the same server.

Even better would be to follow any links on the index page and look for email addresses, such as "contact us"-pages. This could be more effective than to use only abuse, postmaster or webmaster addresses.

But first I've got to get hold of the message-id!!!-)

Any ideas?
/Per
  01-Nov-2004  08:19   
Fields is a property of the CDO Message object (MAPI.Message). Do you have CDO 1.21 installed? It's an optional component for Outlook.
  01-Nov-2004  16:39   
Hi Sue,
Thanks. Yes, I have cdo 1.21 installed. I also followed microsofts advice and registered it with regsvr32, and it seemed ok. But there is sth fishy, I have problems with the mapi.session and mapi.message datatypes in Brian's code as well.
Don't know how to get around it...
/Per
Page [ 1 2 3 Next >>  
Post your comment



name        email