无法连接到远程服务器 [英] Unable to connect to remote server

查看:123
本文介绍了无法连接到远程服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决这个问题,现在我不知道该怎么办,因为每个人都在说我用来发送电子邮件的代码应该工作我甚至尝试过不同的端口从587到25到465我就是不知道了我甚至添加了一个SmtpFailedRecipientsException并且仍然有同样的问题可能有人请帮助我,我需要在计划后推出这个项目。

  string  UserName =   xhasiwe@gmail.com; 
string 密码= mypassword;

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient( smtp .gmail.com);
字符串 report = null ;
mail.From = new MailAddress( xhasiwe@gmail.com);
string connString = @ Provider = Microsoft .ACE.OLEDB.12.0;
+ @ 数据源= C:\ Temp\F1 \Docs \ Expeditors Project \ TestDatabase.accdb;

// 从提供的连接字符串创建OleDbConnection。
使用(OleDbConnection connection = new OleDbConnection(connString))
{
< span class =code-comment> //
商店信息列表
List< String> EmployeeEMailAddress = new List< String>();

// 打开与数据库的连接。
连接。打开();
OleDbDataReader reader = null ;

// 使用给定查询设置命令并将其与当前连接相关联。
OleDbCommand command = new OleDbCommand( SELECT [EmployeeEMailAddress],[TimeOut] FROM Table1 WHERE [TimeOut]为null,connection);
// 读取查询返回的数据
reader = command.ExecuteReader( );
while (reader.Read())
{
EmployeeEMailAddress.Add(reader [ EmployeeEMailAddress]。ToString());
}

foreach string EmployeeEMailAddress)
{
// 如果您想知道元素重复的次数
// 这将返回一个匿名类型的List,每个元素将具有Element和Counter属性,以检索您需要的信息。
if value != \\
{
var result = EmployeeEMailAddress.GroupBy(item = > 项目)
。选择(它em = > new
{
Name = item.Key,
Count = item.Count()
})
.OrderByDescending(item = > item.Count)
.ThenBy (item = > item.Name);
report = String .Join(Environment.NewLine,result
.Select(item = > 字符串 .Format( {0} ,{1};,item.Name,item.Count)));

}
}
connection.Close();
字符串 [] rfqCount = report.Split(' ;');
int ct = 0 ;
for int i = 0 ; i < rfqCount.Length; i ++)
{
if (rfqCount [i] .Trim()!=
{
ct ++;
}
}

字符串 [] emd = rfqCount [ct-1] .Trim()。Split( ' ,');

String emailadd = emd [ 0 ];
mail.To.Add(emailadd);
}

mail.Subject = ReadEmailevent.TypeOfRequest;
mail.Body = ReadEmailevent.Body;

SmtpServer.Port = 25 ;
SmtpServer.Credentials = new System.Net.NetworkCredential(UserName,Password);
SmtpServer.EnableSsl = true ;

尝试
{
SmtpServer.Send(mail);
}
catch (SmtpFailedRecipientsException ex)
{
for int i = 0 ; i < ex.InnerExceptions.Length; i ++)
{
SmtpStatusCode status = ex.InnerExceptions [i] .StatusCode;
if (status == SmtpStatusCode.MailboxBusy ||
status == SmtpStatusCode.MailboxUnavailable)
{
Console。 WriteLine( 传递失败 - 在5秒后重试。);
System.Threading.Thread.Sleep( 5000 );
SmtpServer.Send(mail);
}
else
{
Console.WriteLine( 无法将消息传递给{0}
ex.InnerExceptions [i] .FailedRecipient);
}
}
}

解决方案

执行telnet测试以查看如果您可以连接到Gmail SMTP服务器。您可以在此处找到以下步骤:https: //   support.google.com/mail/answer/78775?hl=en < /跨度> 


I have been struggling with this problem for days now i dont know what to do because everyone is saying the code I am using to send an email should work i have even tried different ports from 587 to 25 to 465 I just do not know anymore i have even added an SmtpFailedRecipientsException and still the same problem could someone please help me i need to launch this project im behind schedule.

string UserName="xhasiwe@gmail.com";
string Password="mypassword";

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
String report = null;
mail.From = new MailAddress("xhasiwe@gmail.com");
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;"
                    + @"Data Source= C:\Temp\F1\Docs\Expeditors Project\TestDatabase.accdb";

// Create an OleDbConnection from the provided connection string.
using (OleDbConnection connection = new OleDbConnection(connString))
{
    //List to store information
    List<String> EmployeeEMailAddress = new List<String>();

    // Open a connection to database.
    connection.Open();
    OleDbDataReader reader = null;

    // Set up a command with the given query and associate this with the current connection.
    OleDbCommand command = new OleDbCommand("SELECT [EmployeeEMailAddress], [TimeOut] FROM Table1 WHERE [TimeOut] is null", connection);
    // Read data returned for the query
    reader = command.ExecuteReader();
    while (reader.Read())
    {
        EmployeeEMailAddress.Add(reader["EmployeeEMailAddress"].ToString());
    }

    foreach (string value in EmployeeEMailAddress ) 
    {
        //If you want to know how many times the elements are repeated
        //This will return a List of an anonymous type, and each element will have the properties Element and Counter, to retrieve the informations you need.
        if (value != "\"\"")
        {
            var result = EmployeeEMailAddress.GroupBy(item => item)
                                             .Select(item => new
                                             {
                                                 Name = item.Key,
                                                 Count = item.Count()
                                             })
                                             .OrderByDescending(item => item.Count)
                                             .ThenBy(item => item.Name);
            report = String.Join(Environment.NewLine, result
            .Select(item => String.Format("{0} , {1} ;", item.Name, item.Count)));

        }        
    }
    connection.Close();   
    String [] rfqCount = report.Split(';');
    int ct = 0;
    for (int i = 0; i < rfqCount.Length; i++)
    {
        if (rfqCount[i].Trim() != "")
        {
            ct++;
        }
    }
    
    String[] emd = rfqCount[ct-1].Trim().Split(',');
    
    String emailadd = emd[0];
    mail.To.Add(emailadd);
}

mail.Subject = ReadEmailevent.TypeOfRequest;
mail.Body = ReadEmailevent.Body;

SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential(UserName, Password);
SmtpServer.EnableSsl = true;

try
{
    SmtpServer.Send(mail);
}
catch (SmtpFailedRecipientsException ex)
{
    for (int i = 0; i < ex.InnerExceptions.Length; i++)
    {
        SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
        if (status == SmtpStatusCode.MailboxBusy ||
            status == SmtpStatusCode.MailboxUnavailable)
        {
            Console.WriteLine("Delivery failed - retrying in 5 seconds.");
            System.Threading.Thread.Sleep(5000);
            SmtpServer.Send(mail);
        }
        else
        {
            Console.WriteLine("Failed to deliver message to {0}",
                ex.InnerExceptions[i].FailedRecipient);
        }
    }
}

解决方案

perform a telnet test to see if you can connect to Gmail SMTP server. You can find steps here: https://support.google.com/mail/answer/78775?hl=en


这篇关于无法连接到远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆