2017-12-13 4 views
0

私はASP.Net MVCでアプリケーションを開発していますが、要件の1つがパスワード機能をリセットしていますユーザーがパスワードをリセットすることができるようにします。ここでasp.netでパスワードリセットの電子メールを送信するようにEmailConvice.csでEmailServiceクラスを設定する方法

は、Web configに私のコードです:ここでは

<system.net> 
    <mailSettings> 
    <smtp from="[email protected]"> 
    <network host="smtp.gmail.com" defaultCredentials="false" password="password" port="587" enableSsl="true" /> 
    </smtp> 
    </mailSettings> 
    </system.net> 

は私のEmailServiceクラスはIdentityConfig.csである:

public class EmailService : IIdentityMessageService 
{ 
    public Task SendAsync(IdentityMessage message) 
    { 
     // Plug in your email service here to send an email. 
     SmtpClient client = new SmtpClient(); 
     return client.SendMailAsync("email from web config", 
            message.Destination, 
            message.Subject, 
            message.Body); 

    } 
    } 

これは、電子メールを送信しない、私は知りません何が問題か、誰かが助けてくれることを願っています。

+0

私はあなたのパスワードを削除しました... –

答えて

0

私はあなたがthis postのように多少やるしようとしていると仮定、あなたは、いわゆる発生した場合this referenceを参照してください((それはSystem.Threading.Tasks.Task方法だから)asyncawaitキーワードを使用して試してみて、簡単にIDisposableインスタンスを管理するためにSmtpClientusing内部のステートメントをラップTaskCanceledException )::あなたはusingステートメントを使用したくない場合は、SmtpClient.SendCompletedイベントは、手動でメールを送信した後SmtpClientインスタンスを配置するために処理しなければならない

public class EmailService : IIdentityMessageService 
{ 
    public async Task SendAsync(IdentityMessage message) 
    { 
     // Plug in your email service here to send an email. 
     using (SmtpClient client = new SmtpClient()) 
     { 
      await client.SendMailAsync("email from web config", 
             message.Destination, 
             message.Subject, 
             message.Body); 
     } 
    } 
} 
public Task SendAsync(IdentityMessage message) 
{ 
    // Plug in your email service here to send an email. 
    SmtpClient client = new SmtpClient(); 

    // Registering SendCompleted event and dispose SmtpClient after sending message 
    client.SendCompleted += (s, e) => { 
     client.Dispose(); 
    }; 

    return client.SendMailAsync("email from web config", 
           message.Destination, 
           message.Subject, 
           message.Body); 
} 
+0

返信ありがとう、私は両方の答えを実装しましたが、パスワードリセットのための電子メールにまだメッセージは送信されません。私は何が間違っているのか分からない。 – Haro

+0

解決策のソースを調べてみると、メールを送信する前後に例外やその他のエラーが出ますか?送信に使用するメールアカウントがテストメールをバウンスしているかどうかを確認します。 –

関連する問題