0 votes
by (200 points)

Hello,

How would I remove a few receive headers in an email? I can list them out fine but I can't figure out how to strip a few out. I'm a little confused as to how to leverage the removeat method of the HeaderValueCollection. Any help would be welcome.

Dim mail As MailMessage

    mail = imap.GetMailMessage(uniqID)

    For Each header In mail.Headers

            If header.Name Like "Received" Then
                If InStr(header.value.ToString, "prod.outlook.com") > 0 Then
                    MsgBox("Going to remove the header")
                    header.removeat(header)' This doesn't work
                End If

                MsgBox(header.Value.ToString)

            End If
Applies to: Rebex Secure Mail

1 Answer

0 votes
by (72.7k points)

I am not able to compile your code, because header doesn't contain RemoveAt method. You have to call this method on mail.Headers collection, not on a header itself.

You can do what you need like this:

' iterate through all headers
Dim i As Integer = 0
Do While i < (mail.Headers.Count - 1)
    Dim header As Rebex.Mime.MimeHeader = mail.Headers(i)
    If header.Name Like "Received" AndAlso InStr(header.Value.ToString, "prod.outlook.com") > 0 Then
        ' remove header (and keep index, because collection was shortened)
        mail.Headers.RemoveAt(i)
    Else
        ' update index to next header
        i += 1
    End If
Loop

Or, if you cannot do this in one loop, you can do the same in two step process like this:

' find headers to be removed
Dim headersToRemove = New List(Of Rebex.Mime.MimeHeader)
For Each header As Rebex.Mime.MimeHeader In mail.Headers
    If header.Name Like "Received" AndAlso InStr(header.Value.ToString, "prod.outlook.com") > 0 Then
        headersToRemove.Add(header)
    End If
Next

' remove unwanted headers
For Each header In headersToRemove
    mail.Headers.Remove(header)
Next
...