从列表中获取值并使用它

Get value from a list and work with it

我正在尝试构建一个通过袜子发送电子邮件的应用程序,如果第一条消息是通过袜子发送的,则消息将按每条消息发送,第二条消息应该使用不同的袜子,如果我作为我从 txt 文件中恢复信息并添加到列表中:

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)
{
}

我想要的

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

从我通过 ip 值和端口完成的列表中获取值,

如果第一封邮件是通过一个 ip 发送的,第二封邮件是使用列表中的另一个 ip,不要发送后面跟着相同 ip 的两封电子邮件

谢谢

您需要为 IP 和端口创建 class

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

现在使用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});
}

现在通过从 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);
     }
}