我最近已切换到ASP.NET MVC。当人们注册我的网站时,我想发送电子邮件确认。
因此,我默认取消注释ASP.NET MVC默认的代码,并在web.config
中添加配置,但这不起作用,我一直有这个错误:
SMTP服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:5.5.1需要身份验证。
我创建了一个asp.net webforms,并尝试从该项目发送电子邮件,并且工作正常。所以我复制了我在页面加载时使用的代码,用于在webforms中发送电子邮件,并将其放入帐户控制器的注册操作中,但我又遇到了错误。
我真的不明白为什么我在ASP.NET MVC中遇到这个错误,但完全相同的代码在webforms中工作正常。
这是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);
}
我真的很困惑这个问题。请帮忙!
谢谢。
答案 0 :(得分:1)
更新:
您为什么使用user.Id
而非model.Email
?
如果您有任何空白或遗漏的详细信息,这可能会导致错误?
<强>原始强>
您应该将mail.From
更改为mail.Sender
。
您是否将ASP.NET Web窗体和MVC加载到同一主机环境中? 可以阻止端口25,还是要求通过HTTPS发送SMTP?
对于HTTPS,您需要更改端口号,通常是端口465,但可能会有所不同,具体取决于您的邮件发件人。
如果您的SMTP服务器有限制,请尝试使用SendGrid,该帐户有免费帐户。