Search code examples
c#listsocks

Get value from a list and work with it


I am trying to build an application sends emails by socks, messages will be sent per message if the first message is sent through a socks, the second should use a different socks, what I do in my application if I as I Recuper the information from a txt file and I add to list :

try
{
    SmtpServer oServer = new SmtpServer("");

    var list = new List<string>();
    var input = File.ReadAllText(@"C:\New folder\SendMail6\socks-list.txt");
    var r = new Regex(@"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})");
    foreach (Match match in r.Matches(input))
    {
         string ip = match.Groups[1].Value;
         string port = match.Groups[2].Value;
         list.Add(ip);
         list.Add(port);
    }
    foreach (string ip in list)
    {

    }
}
catch(Exception)
{
}

what I want that

oServer.SocksProxyServer = "37.187.118.174";
oServer.SocksProxyPort = 14115;

takes the values from the list I completed by ip values and port, and

if the first mail is sent by an ip the second mail is use another ip in list dont send tow email which follow by same ip

Thanks


Solution

  • You need to create a class for IP and Port

    public class IpAndPort
    {
        public string IpAddress { get; set; }
        public string Port { get; set; }
    }
    

    Now use ConcurrentBag

    using System.Collections.Concurrent;
    
    //------
    var ips =  new ConcurrentBag<IpAndPort>();
    var input = File.ReadAllText(@"C:\New folder\SendMail6\socks-list.txt");
    var r = new Regex(@"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})");
    foreach (Match match in r.Matches(input))
    {
         string ip = match.Groups[1].Value;
         string port = match.Groups[2].Value;
         if(ips.Any(x => x.IpAddress.Trim() == ip.Trim()))
             continue; 
         ips.Add(new IpAndPort { IpAddress = ip, Port = port});
    }
    

    Now send message by taking values from ConcurrentBag

    while (!ips.IsEmpty)
    {
         IpAndPort ipAndPort;
         if (!ips.TryTake(out ipAndPort)) continue;
         try
         {
               //code here to send message using below IP and Port
               var ip = ipAndPort.IpAddress;
               var port = ipAndPort.Port;
               /----
               oServer = new SmtpServer("");
               oServer.SocksProxyServer = ip;
               oServer.SocksProxyPort = port;
         }
         catch (Exception ex)
         {
               Console.WriteLine(ex.Message);
         }
    }