SendGrid - Owin身份电子邮件身份验证

时间:2014-10-01 08:45:02

标签: c# smtp identity owin sendgrid

我很难通过电子邮件实施Owin Identity 2.0身份验证。

enter image description here

以下是我用于smtp客户端配置的代码片段。

IdentityConfig.cs

public class EmailService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        // Credentials:
        string sendGridUserName = "HansMuster456";
        string sentFrom = "OwinIdentityTest@TestDomain.ch";
        string sendGridPassword = "xxxxx";

        // Configure the client
        var client = new System.Net.Mail.SmtpClient("smtp.sendgrid.net", 587);

        client.Port = 587;
        client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;

        // Create the credentials:
        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(sendGridUserName, sendGridPassword);

        client.EnableSsl = true;
        client.Credentials = credentials;

        // Create the message:
        var mail = new System.Net.Mail.MailMessage(sentFrom, message.Destination);

        mail.Subject = message.Subject;
        mail.Body = message.Body;

        // Send:
        return client.SendMailAsync(mail);
    }
}

SMTP-登录

System.Net.Sockets Verbose: 0 : [6456] Socket#53556591::EndReceive(OverlappedAsyncResult#42931033)
System.Net.Sockets Verbose: 0 : [6456] Exiting Socket#53556591::EndReceive()    -> Int32#32
System.Net Error: 0 : [6456] Decrypt failed with error 0X90317.
System.Net.Sockets Verbose: 0 : [6456] Socket#53556591::Dispose()
System.Net Verbose: 0 : [6456] SmtpPooledStream::Dispose #49538252
System.Net Verbose: 0 : [6456] Exiting SmtpPooledStream::Dispose #49538252

完整日志文件的链接:http://pastebin.com/3ph3PmSM

1 个答案:

答案 0 :(得分:1)

删除client.EnableSsl = true;部分来自您的代码并检查。它应该工作。

我用'mail.authsmtp.com'对此进行了测试,它运行正常,你只需要删除client.EnableSsl = true;部分如下代码。

public class EmailService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        // Credentials:
        string sendGridUserName = "HansMuster456";
        string sentFrom = "OwinIdentityTest@TestDomain.ch";
        string sendGridPassword = "xxxxx";

        // Configure the client
        var client = new System.Net.Mail.SmtpClient("smtp.sendgrid.net", 587);

        client.Port = 587;
        client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;

        // Create the credentials:
        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(sendGridUserName, sendGridPassword);

        //client.EnableSsl = true;
        client.Credentials = credentials;

        // Create the message:
        var mail = new System.Net.Mail.MailMessage(sentFrom, message.Destination);

        mail.Subject = message.Subject;
        mail.Body = message.Body;

        // Send:
        return client.SendMailAsync(mail);
    }
}