最近、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);
}
私は本当にこの問題で立ち往生しています。助けてください!
ありがとうございました。