2017-02-22 56 views
0

MVCサイトに新しい電子メールによる確認方法を追加しようとしています。確認メールが届きます。smtpエラーでGmailを使用しているASP.NET:「接続した相手が正しく応答しなかったため接続に失敗しました。」

"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 66.102.1.108:587".

私が使用したコードは次のとおりです:

using ATMProject.Models; 
using Microsoft.AspNet.Identity; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net.Mail; 
using System.Security.Policy; 
using System.Web; 
using System.Web.Mvc; 

namespace ATMProject.Services 
{ 
    public class SendEmailConfirmation 
    { 
     public string email { get; set; } 
     public string fullname { get; set; } 
     public string username { get; set; } 
     public string random { get; set; } 

     public SendEmailConfirmation(string Email, string Fullname, 
      string Username, string Random) 
     { 
      email = Email; 
      fullname = Fullname; 
      username = Username; 
      random = Random; 
     } 

     public void sendEmail() 
     { 
      SmtpClient smtp = new SmtpClient(); 
      smtp.Host = "smtp.gmail.com"; 
      smtp.Port = 587; 
      smtp.UseDefaultCredentials = false; 
      smtp.Credentials = new 
       System.Net.NetworkCredential("[email protected]", "123456"); 
      smtp.EnableSsl = true; 
      MailMessage msg = new MailMessage(); 

      #region Msg 
      msg.Subject = "Hello " + fullname; 
      msg.Body = "Hello " + fullname + 
       "Thanks for Registering in Budgetize...Your Account Details are given below:"; 
      msg.Body += "<tr>"; 
      msg.Body += "<td>User Name :" + username + "</td>"; 
      msg.Body += "</tr>"; 
      msg.Body += "<tr>"; 
      msg.Body += "<td>Activation Number :" + random + "</td>"; 
      msg.Body += "</tr>"; 
      msg.Body += "<tr>"; 
      msg.Body += "<td>Thanking</td><td>Team Budgetize</td>"; 
      msg.Body += "</tr>"; 
      #endregion 

      string toAddress = email; // Add Recepient address 
      msg.To.Add(toAddress); 
      string fromAddress = "\"Budgetize \" <[email protected]>"; 
      msg.From = new MailAddress(fromAddress); 
      msg.IsBodyHtml = true; 

      try {smtp.Send(msg);} 
      catch (Exception ex) {throw;} 
     } 
    } 
} 
+1

実際のパスワードをそこに入れないことを願っています。 – Steve

答えて

0

問題は、私はいつも同じエラーを取得するに関係なく、私が使用してどのようなコード(私はGitHubの上でいくつかの異なるオプションが見つからなかっました)ということであり、 SMTPサーバーに安全な接続が必要な場合、またはクライアントが認証されていない場合サーバーの応答は、5.5.1認証が必要でした。

+0

安全な接続とはどういう意味ですか?私はssl(smtp.enablessl = true)を使用しました –

+0

http://www.aspsnippets.com/Articles/GMAIL-Error-The-SMTP-server-requires-a-secure-connection-or-the-client-was-not -authenticated.aspx あなたの電子メールは認証されていないと思います。 –

+0

ねえ、私は解決策を見つけた!あなたは以下のコメントでそれを見ることができます:) –

0

解決策が見つかりました。私は別のコンピュータ(家庭用コンピュータ)で同じコードを実行し、うまくいきました。 ポート(587)は私の仕事のPCの別のセキュリティ層(ファイアウォールはブロックしておらず、アンチウィルスも行っていませんでした)でブロックされているようです。 ITマネージャーは、私がアクセスできない別のレイヤーで多くのポートをブロックしていると言いました。あなたの仕事で同じバグが起きた場合は、別のPC(同じネットワークに接続されていないPC)で実行してみて、コードがうまくいくかどうか調べてください。

関連する問題