سؤال

Can somebody please look at the following code and see what is responsible for causing the error "An error occurred while processing your request."? I am using ASP.NET Identity 2.0, please see code below:

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);
    }
}

For obvious reasons I have not used the correct credentials in the code above for security reasons, but I have a contact form in the same project that uses the correct credentials and the email works perfectly well.

If I comment out the code above with the exception of public class EmailService : IIdentityMessageService { public Task SendAsync(IdentityMessage message) { return Task.FromResult(0); } } The registration process completes successfully.

Any help would be much appreciated :-)

هل كانت مفيدة؟

المحلول

The issue was with the line return Task.FromResult(0); it should be return client.SendMailAsync(mail); instead, all is working now :-)

نصائح أخرى

using await is more correct in my opinion (at least with versions of identity >= 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);
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top