1

最近、ASP.NET MVC に切り替えました。人々が私のウェブサイトにサインアップしたときに、確認メールを送信したいと考えています。

したがって、ASP.NET MVC がデフォルトでこのために持っているコードのコメントを外し、構成を追加しましweb.configたが、それは機能せず、このエラーが発生し続けました。

SMTP サーバーが安全な接続を必要とするか、クライアントが認証されませんでした。サーバーの応答は次のとおりです。5.5.1 認証が必要です。

私はasp.net Webフォームを作成し、そのプロジェクトから電子メールを送信しようとしましたが、うまくいきました。そのため、Webフォームでメールを送信するためにページの読み込みにあったコードをコピーし、アカウントコントローラーの登録アクションに入れましたが、再びそのエラーが発生しました.

ASP.NET MVC でこのエラーが発生する理由はよくわかりませんが、まったく同じコードが Web フォームで正常に機能します。

これは、ASP.NET MVC 登録アクションのコードです。

public async Task<ActionResult> Register(RegisterViewModel model)
{
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email, BirthDate=model.BirthDate };

            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                //string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                //create the mail message 
                MailMessage mail = new MailMessage();

                //set the addresses 
                mail.From = new MailAddress("info@wwwebco.com"); //IMPORTANT: This must be same as your smtp authentication address.
                mail.To.Add(user.Id);

                //set the content 
                mail.Subject = "This is an email";
                mail.Body = "This is from system.net.mail using C sharp with smtp authentication.";
                //send the message 
                SmtpClient smtp = new SmtpClient("mail.wwwebco.com");

                //IMPORANT:  Your smtp login email MUST be same as your FROM address. 
                NetworkCredential Credentials = new NetworkCredential("info@wwwebco.com", "MyPassWord");
                smtp.Credentials = Credentials;
                await smtp.SendMailAsync(mail);

                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

そして、私がasp.net webformsアプリに持っていて正常に動作するコードは次のとおりです。

protected void Page_Load(object sender, EventArgs e)
{
    //create the mail message 
    MailMessage mail = new MailMessage();

    //set the addresses 
    mail.From = new MailAddress("info@wwwebco.com"); //IMPORTANT: This must be same as your smtp authentication address.
    mail.To.Add("armanhafezi@gmail.com");

    //set the content 
    mail.Subject = "This is an email";
    mail.Body = "This is from system.net.mail using C sharp with smtp authentication.";
    //send the message 
    SmtpClient smtp = new SmtpClient("mail.wwwebco.com");

    //IMPORANT:  Your smtp login email MUST be same as your FROM address. 
    NetworkCredential Credentials = new NetworkCredential("info@wwwebco.com", "MyPassWord");
    smtp.Credentials = Credentials;
    smtp.Send(mail); 

}

私は本当にこの問題で立ち往生しています。助けてください!

ありがとうございました。

4

1 に答える 1

1

アップデート:

user.Idを使用していて、使用していないのはなぜmodel.Emailですか?

空白または欠落している詳細がある場合、エラーが発生する可能性がありますか?

オリジナル:

を に変更する必要がmail.Fromありますmail.Sender

ASP.NET Web フォームと MVC を同じホスト環境にロードしていますか? ポート 25 をブロックしていたり​​、SMTP を HTTPS 経由で送信するように要求していたり​​する可能性はありますか?

HTTPS の場合、ポート番号 (通常はポート 465) を変更する必要がありますが、メールの送信者によって異なる場合があります。

SMTP サーバーに制限がある場合は、無料のアカウントを持つSendGridを使用してみてください。

于 2015-12-02T09:04:27.160 に答える