2016-05-18 18 views
1

新しいユーザーがasp.net c#を使用して自分のウェブサイトに登録すると、どのようにアクティベーション電子メールを送信できますか?私は this link説明した手順を試してみましたが、私はこのエラーが直面している:Asp.net電子メールを送信

System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.

これまでの任意の溶液または電子メールを送信するための他の方法はありますか?

これは、提案された重複しても問題が解決しなかった私のコード

using (MailMessage mm = new MailMessage("[email protected]", txtEmail.Text)) 
{ 
    mm.Subject = "Account Activation"; 
    string body = "Hello " + txtUsername.Text.Trim() + ","; 
    body += "<br /><br />Please click the following link to activate your account"; 
    body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>"; 
    body += "<br /><br />Thanks"; 
    mm.Body = body; 
    mm.IsBodyHtml = true; 
    SmtpClient smtp = new SmtpClient(); 
    smtp.Host = "smtp.gmail.com"; 
    smtp.EnableSsl = true; 
    NetworkCredential NetworkCred = new NetworkCredential("[email protected]", "<password>"); 
    smtp.UseDefaultCredentials = true; 
    smtp.Credentials = NetworkCred; 
    smtp.Port = 587; 
    smtp.Send(mm); 
} 

です。それは完璧に動作し、

public static class email_utility 
{ 
    public static async Task<bool> send_email(this string body, string subject, string email, int try_count) 
    { 
     return await Task.Run(() => 
     { 
      var cli = new SmtpClient(); 
      using (var message = new MailMessage(config.sender_email, email)) 
      { 
       message.Subject = subject; 
       message.SubjectEncoding = UTF8Encoding.UTF8; 
       message.Body = body; 
       message.BodyEncoding = UTF8Encoding.UTF8; 
       message.DeliveryNotificationOptions = DeliveryNotificationOptions.Never; 
       for (var count = 0; count < try_count; ++count) 
       { 
        try 
        { 
         lock (config.sender_email) 
         { 
          cli.Send(message); 
          return true; 
         } 
        } 
        catch (SmtpFailedRecipientsException) 
        { 
         return false; 
        } 
        catch (SmtpException) 
        { 
         Thread.Sleep(config.send_timeout); 
        } 
       } 
       return false; 
      } 
     }); 
    } 
} 
+2

1)コードを表示する必要があります。 2)あなたは* ACTUAL EMAIL SERVER *と話す必要があります。サーバーはあなたからの接続を受け入れる必要があります。このエラー 'net_io_connectionclosed'に基づいて、それは* NOT *のように起こります。 – paulsm4

+4

"net_io_connectionclosed"エラーでdetalのリンクを見てください:[SmtpException:トランスポート接続からデータを読み取れません:net \ _io \ _connectionclosed](http://stackoverflow.com/questions/20228644/smtpexception-unable-to -read-data-from-the-transport-connection-net-io-connect) – paulsm4

+0

投稿を編集しました。 @ paulsm4 – Bayan

答えて

1

ポート587を使用しています。

using System.Net; 
using System.Net.Mail; 

var fromAddress = new MailAddress("[email protected]", "From Name"); 
var toAddress = new MailAddress("[email protected]", "To Name"); 
const string fromPassword = "fromPassword"; 
const string subject = "Subject"; 
const string body = "Body"; 

var smtp = new SmtpClient 
      { 
       Host = "smtp.gmail.com", 
       Port = 587, 
       EnableSsl = true, 
       DeliveryMethod = SmtpDeliveryMethod.Network, 
       UseDefaultCredentials = false, 
       Credentials = new NetworkCredential(fromAddress.Address, fromPassword) 
      }; 
using (var message = new MailMessage(fromAddress, toAddress) 
        { 
         Subject = subject, 
         Body = body 
        }) 
{ 
    smtp.Send(message); 
} 
1

これを試して、あなただけのvar SMTP = newSMTPCientの前にあるフィールドを変更する必要があります:私は、すでに以下のコードを参照してください

+0

FYI、それはあなたのコードが正しいという事実のほかに、完璧に働いている理由です)1)あなたは電子メールリレーとしてGMailを使用しています2) "From"アドレスで指定した有効なgmailアカウント、3)ポート587とTLS暗号化SMTPを使用しています。 OPが最初にやるべきことは、ネットワーク管理者と協力して、 "優先する"電子メールサーバーで可能な構成/権限の問題を調べることです。 – paulsm4

関連する問題