有人可以查看以下代码,看看导致错误的原因是什么?#34;处理您的请求时出错。"?我使用的是ASP.NET Identity 2.0,请参阅下面的代码:
IdentityConfig
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
var credentialUserName = "info@ourdoamin.com";
var sentFrom = "noreply@ourdoamin.com";
var pwd = "ourpassword";
// Configure the client:
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("mail.ourdoamin.com");
client.Port = 25;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
// Creatte the credentials:
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(credentialUserName, pwd);
client.EnableSsl = false;
client.Credentials = credentials;
// Create the message:
var mail = new System.Net.Mail.MailMessage(sentFrom, message.Destination);
mail.Subject = message.Subject;
mail.Body = message.Body;
return Task.FromResult(0);
}
}
出于显而易见的原因,出于安全原因,我没有在上面的代码中使用正确的凭据,但我在同一个项目中有一个使用正确凭据的联系表单,并且电子邮件的效果非常好。
如果我注释掉上面的代码,public class EmailService : IIdentityMessageService { public Task SendAsync(IdentityMessage message) { return Task.FromResult(0); } }
除外注册过程成功完成。
非常感谢任何帮助: - )
答案 0 :(得分:11)
问题在于return Task.FromResult(0);
行应该是return client.SendMailAsync(mail);
,现在一切正常: - )
答案 1 :(得分:5)
在我看来,使用await更正确(至少对于身份版本> = 2.0
public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
await configSMTPasync(message);
}
// send email via smtp service
private async Task configSMTPasync(IdentityMessage message)
{
// Plug in your email service here to send an email.
var credentialUserName = "info@ourdoamin.com";
var sentFrom = "noreply@ourdoamin.com";
var pwd = "ourpassword";
// Configure the client:
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("mail.ourdomain.com");
client.Port = 25;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
// Creatte the credentials:
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(credentialUserName, pwd);
client.EnableSsl = false;
client.Credentials = credentials;
// Create the message:
var mail = new System.Net.Mail.MailMessage(sentFrom, message.Destination);
mail.Subject = message.Subject;
mail.Body = message.Body;
await client.SendMailAsync(mail);
}
}