3

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 = "[email protected]";
        var sentFrom = "[email protected]";
        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 :-)

2 Answers 2

11

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

0
5

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 = "[email protected]";
        var sentFrom = "[email protected]";
        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);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.