2016-06-20 9 views
0

Received:fromヘッダを使用して、電子メールヘッダの最後のIPアドレスを解析します。私はヘッダーから最後に受信されたものを見つけ、IPを特定しようとしています。私のコードは、 "{}など"から受け取った多くの特殊文字が存在するため、動作しないようです。私はまた、ipが同じ行にないかもしれないという点で問題に取り組んでいます。メールのヘッダーに最後に送信されたIPを簡単に識別する方法はありますか?別の行にある可能性があります。最後に受信したメールの解析:Received:fromヘッダ

これは私が最初にして働いているものです:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Text.RegularExpressions; 
using System.Net; 
using System.IO; 

namespace IP 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int counter = 0; 
      string line; 
      System.IO.StreamReader file = 
       new System.IO.StreamReader("C:\\ip.txt"); 

      while ((line = file.ReadLine()) != null) 
      { 
       const string x_orig_ip = "Received: from"; 
       line = line.Trim(); 
       if (line.StartsWith(x_orig_ip, StringComparison.OrdinalIgnoreCase)) 
       { 
        string sIpAddress = line.Substring(x_orig_ip.Length, line.Length - x_orig_ip.Length).Trim(new char[] { ' ', '\t', '[', ']', '(', ')' }); 
        var ipAddress = System.Net.IPAddress.Parse(sIpAddress); 
        Console.WriteLine(ipAddress); 
        counter++; 
       } 
      } 

      Console.ReadLine(); 
     } 
    } 
} 

ので、下記のヘッダから、私が受け取った最後の経由101.123.148.12を取得したいのですが:エントリから:

Received: from test (subdomain.domain.com [192.168.0.1]) 
    Mon, 20 Jun 2016 10:46:57 -0400 (EDT) 
Received: from test123 ([192.168.0.1]) 
    by test.test; Mon, 20 Jun 2016 10:46:57 -0400 
Received: from test.engine.com (localhost [127.0.0.1]) 
    by test.testty.com (Postfix) with ESMTP id ABCDEF 
    for <[email protected]>; Sun, 19 Jun 2016 09:06:35 -0400 (EDT) 
Received: from test.message.com (localhost [127.0.0.1]) 
    by from test.message.com (Authentication) with ESMTP 
    Sun, 19 Jun 2016 09:06:35 -0400 
Authentication-Results: 
    spf=none smtp.mailfrom= smtp.helo 
Received-SPF: none 
    (192.168.0.1: No applicable sender policy available) 
Received: from 192.168.0.1 (unknown [192.168.0.1]) 
    by with SMTP 
Received: from unknown (HELO localhost) 
    by 101.123.148.12 with ESMTPA; Sun, 19 Jun 2016 10:00:20 -0300 
X-Originating-IP: 101.123.148.12 
From: [email protected] 
To: [email protected] 
Subject: Test 
Date: Sun, 19 Jun 2016 09:56:41 -0300 
+0

http://stackoverflow.com/questions/1669797/are-there-net-framework-methods-to-parse-an-email-mime古い質問ですが、上記のライブラリがまだ維持されているかどうかはわかりません。 –

答えて

1
あなたは、この正規表現を試すことができます

ヘッダがある
var re = new RegEx(@"Received: (.|\n )*([^\d](\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))+", RegExOptions.Multiline); 
var matches = re.Matches(headers); 
if(matches.Count>0) 
{ 
    var group = matches[matches.Count-1].Groups[3]; 
    string ip = group.Captures[group.Captures.Count-1].Value; 
    // do something with ip... 
} 

すべてのヘッダー(単一行だけでなく)を保持する文字列変数。

ReceivedヘッダーのすべてのIPアドレスをキャプチャグループ3に抽出します。最後の一致の最後のキャプチャを行い、必要なものを取得します。メッセージが全く異なっている、によってを受信されたメッセージはむしろ、 101.123.148.12からを受けたことを示すないヘッダが存在しないように、あなたは、通常101.123.148.12を考慮しないであろう

注意。

関連する問題