C#:发送邮件失败

C#:发送邮件失败,c#,smtp,gmail,port,C#,Smtp,Gmail,Port,我尝试过多种方法,如: Exception thrown: 'System.IO.IOException' in System.dll Exception thrown: 'System.Net.Mail.SmtpException' in System.dll System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the trans

我尝试过多种方法,如:

Exception thrown: 'System.IO.IOException' in System.dll
Exception thrown: 'System.Net.Mail.SmtpException' in System.dll
System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
   at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
   at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at TreeView.TreeView.<Email>d__28.MoveNext() in C:\Users\Alexander\Documents\Visual Studio 2015\Projects\TreeView\TreeView\TreeView\TreeView.cs:line 365
public void Email()
{
    try
    {
        SmtpClient client = new SmtpClient()
        {
            Host = @"smtp.gmail.com",
            Port = 465,
            Credentials = new NetworkCredential(@"mailid@gmail.com", @"Password"),
            UseDefaultCredentials = false,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            EnableSsl = true
        };

        MailMessage message = new MailMessage()
        {
            From = new MailAddress(@"mailid@gmail.com"),
            Subject = @"Test",
            Body = @"Test",
            Priority = MailPriority.Normal
        };

        message.To.Add(@"mailid@example.com");
        Attachment file = new Attachment(@"C:\Users\Alexander\Documents\File.txt");
        message.Attachments.Add(file);

        client.Send(message);
    }
    catch { }
}
  • 使用不同的变体、端口、SSL开启或关闭
  • 不同的邮件服务提供商
  • 在我的gmail帐户设置中设置“允许不太安全的应用”
问题:

Exception thrown: 'System.IO.IOException' in System.dll
Exception thrown: 'System.Net.Mail.SmtpException' in System.dll
System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
   at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
   at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at TreeView.TreeView.<Email>d__28.MoveNext() in C:\Users\Alexander\Documents\Visual Studio 2015\Projects\TreeView\TreeView\TreeView\TreeView.cs:line 365
public void Email()
{
    try
    {
        SmtpClient client = new SmtpClient()
        {
            Host = @"smtp.gmail.com",
            Port = 465,
            Credentials = new NetworkCredential(@"mailid@gmail.com", @"Password"),
            UseDefaultCredentials = false,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            EnableSsl = true
        };

        MailMessage message = new MailMessage()
        {
            From = new MailAddress(@"mailid@gmail.com"),
            Subject = @"Test",
            Body = @"Test",
            Priority = MailPriority.Normal
        };

        message.To.Add(@"mailid@example.com");
        Attachment file = new Attachment(@"C:\Users\Alexander\Documents\File.txt");
        message.Attachments.Add(file);

        client.Send(message);
    }
    catch { }
}
  • 如何修复此错误并成功发送附件
  • 是什么导致了这个错误
  • 是否因为端口可能被阻塞
错误:

Exception thrown: 'System.IO.IOException' in System.dll
Exception thrown: 'System.Net.Mail.SmtpException' in System.dll
System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
   at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
   at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at TreeView.TreeView.<Email>d__28.MoveNext() in C:\Users\Alexander\Documents\Visual Studio 2015\Projects\TreeView\TreeView\TreeView\TreeView.cs:line 365
public void Email()
{
    try
    {
        SmtpClient client = new SmtpClient()
        {
            Host = @"smtp.gmail.com",
            Port = 465,
            Credentials = new NetworkCredential(@"mailid@gmail.com", @"Password"),
            UseDefaultCredentials = false,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            EnableSsl = true
        };

        MailMessage message = new MailMessage()
        {
            From = new MailAddress(@"mailid@gmail.com"),
            Subject = @"Test",
            Body = @"Test",
            Priority = MailPriority.Normal
        };

        message.To.Add(@"mailid@example.com");
        Attachment file = new Attachment(@"C:\Users\Alexander\Documents\File.txt");
        message.Attachments.Add(file);

        client.Send(message);
    }
    catch { }
}
对于gmail,我有

SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";

client.Port = 587;

client.EnableSsl = true;
client.Credentials = new NetworkCredential("mygmailusername@gmail.com", "mygmailpassword");
                ////email.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network ;
PS您可能需要启用此功能:

附加:

下载此工具:

看看你是否可以查询这个端口/地址

我得到以下结果:

=============================================

 *Starting portqry.exe -n smtp.gmail.com -e 587 -p TCP ...
Querying target system called:
 smtp.gmail.com
Attempting to resolve name to IP address...
Name resolved to 74.125.29.109
querying...
TCP port 587 (unknown service): LISTENING
portqry.exe -n smtp.gmail.com -e 587 -p TCP exits with return code 0x00000000.*

设置
UseDefaultCredentials
将始终将
凭据设置为
null

因此,在设置
凭据之前,必须先设置
UseDefaultCredentials=true/false


参考资料:

您可以尝试端口587…并尝试注释掉
DeliveryMethod=SmtpDeliveryMethod.Network
行。我已经尝试过了,但仍然不起作用。请先尝试在没有附件的情况下发送…以验证设置是否正确。然后调试附件。他已经尝试启用“不太安全的应用程序”,这在他的问题的第三个要点中有说明:真奇怪。我刚刚重新测试了我的代码,代码正常。我做了一个附加来测试端口。确定,因此出现了一个新错误,称SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:需要5.5.1身份验证。