2016-09-27 12 views
0

このコードが機能しない理由を理解できません。クライアント電子メール - C#

私はこのエラーを持っている:

  • System.IO.IOException:接続がSystem.Net.Mail.SmtpClient.Readで を閉じて()

  • System.IO.IOException:接続がクローズSystem.Net.Mail.SmtpClient.SendCore

  • System.IO.IOExceptionで :接続がSystem.Net.Mail.SmtpClient.SendInternalで を閉じ

  • System.IO.IOException:接続System.Net.Mail.SmtpClient.Send

    static void Main(string[] args) 
        { 
         var smtp = new SmtpClient 
         { 
          DeliveryMethod = SmtpDeliveryMethod.Network, 
          UseDefaultCredentials = false, 
          EnableSsl = true, 
          Host = "smtp.gmail.com", 
          //465 SSL se uso 25 solo ad utenti google mando 
          Port = 465, 
          Credentials = new NetworkCredential("id", "password"), 
         }; 
    
         Console.WriteLine("Mail From: "); 
         var fromAddress = new MailAddress(Console.ReadLine()); 
    
         Console.WriteLine("Mail To: "); 
         var toAddress = new MailAddress(Console.ReadLine()); 
    
         Console.WriteLine("Subject: "); 
         string subject = Console.ReadLine(); 
    
         Console.WriteLine("Body: "); 
         string body = Console.ReadLine(); 
    
         using (var message = new MailMessage(fromAddress, toAddress) 
         { 
          Subject = subject, 
          Body = body 
         }) 
    
         try 
         { 
          smtp.Send(message); 
         } 
         catch(Exception ex) 
         { 
          Console.WriteLine("Unable to send message due to the following reason: " + ex.ToString()); 
         } 
        } 
    

どのように私はこれらの問題を解決することができるで を閉じて?

+1

[SmtpException:トランスポート接続からデータを読み取れません:net \ _io \ _connectionclosed](http://stackoverflow.com/questions/20228644/smtpexception-unable-to-read-data-from-the -transport-connection-net-io-connect) –

答えて

0

465の代わりにポート587を使用してください。ポート465は、技術的に廃止されました。 OPさんのコメントから

+0

同様の問題があります: http://stackoverflow.com/questions/20228644/smtpexception-unable-to-read-data-from-the-transport-connection -net-io-connect – iDesireJustice

+0

私が587でポートを設定しても、私は同じエラーがあります。さらに、Gmailは誰かが自分のアカウントにアクセスしようとしたことを伝えています。 – th3g3ntl3man

+0

@JeremyShiklov答えをコピー&ペーストするだけの場合は、その質問を重複としてフラグを立てるだけです。 – user1666620

0

Even if I set the port on 587 I have the same errors. In addition, Gmail tells me that someone attempted to access my account

それはあなたがポート587を使用している場合なら、あなたは自分のGmailの設定で安全性の低いアプリケーションを可能にしなければならないが。 2因子認証を使用している場合は、アプリケーションパスワードを追加して代わりに使用する必要があります。