我的ASP.Net代码可以从sendgrid得到确认邮件已发送吗?

前端之家收集整理的这篇文章主要介绍了我的ASP.Net代码可以从sendgrid得到确认邮件已发送吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的应用程序中使用了我使用的代码
private async Task configSendGridasync(IdentityMessage message)
    {
        var myMessage = new SendGridMessage();
        myMessage.AddTo(message.Destination);
        myMessage.From = new System.Net.Mail.MailAddress(
                            "a@b.com","AB Registration");
        myMessage.Subject = message.Subject;
        myMessage.Text = message.Body;
        myMessage.Html = message.Body;

        var credentials = new NetworkCredential(
                   ConfigurationManager.AppSettings["mailAccount"],ConfigurationManager.AppSettings["mailPassword"]
                   );

        // Create a Web transport for sending email.
        var transportWeb = new Web(credentials);

        // Send the email.
        if (transportWeb != null)
        {
            await transportWeb.DeliverAsync(myMessage);
        }
        else
        {
            Trace.TraceError("Failed to create Web transport.");
            await Task.FromResult(0);
        }
    }

这里叫:

public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {

        var user = new ApplicationUser()
        {
            Email = model.Email,FirstName = model.FirstName,LastName = model.LastName,RoleId = (int)ERole.Student,UserName = model.UserName
        };
        var result = await UserManager.CreateAsync(user,model.Password);
        if (result.Succeeded)
        {
            var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
            var callbackUrl = model.Server +
                              "/index.html" +
                              "?load=confirmEmail" +
                              "&userId=" + user.Id +
                              "&code=" + HttpUtility.UrlEncode(code);
            await UserManager.SendEmailAsync(user.Id,"Confirm your account","Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
        }
        if (!result.Succeeded)
        {
            return GetErrorResult(result);
        }
        return Ok();

    }

有什么办法可以从sendgrid确认邮件已经发送还是其他信息?

解决方法

通过SendGrid Web API发送的电子邮件是异步的,所以要确认,您需要实现一个webhook. Event Webhook将会将您选择的活动发布到您定义的URL.在这种情况下,您对“交付”事件感兴趣.

您将需要服务器上的一些代码来处理传入的webhook,并根据结果执行任何逻辑,例如记录传递的事件.有几个社区提供的图书馆,让您轻松创建一个webhook处理程序.我建议sendgrid-webhooks,这是在nuget上可用.

然后取出传入的POST并将其传递给解析器以获取对象.

由于您使用ASP.NET MVC,因此您可以使用控制器内的[HttpPost]方法从SendGrid接收POST数据.然后可以使用sendgrid-webhooks来解析它.

sendgrid-webhooks readme

var parser = new WebhookParser();
var events = parser.ParseEvents(json);

var webhookEvent = events[0];

//shared base properties
webhookEvent.EventType; //Enum - type of the event as enum
webhookEvent.Categories; //IList<string> - list of categories assigned ot the event
webhookEvent.TimeStamp; //DateTime - datetime of the event converted from unix time
webhookEvent.UniqueParameters; //IDictionary<string,string> - map of key-value unique parameters

//event specific properties
var clickEvent = webhookEvent as ClickEvent; //cast to the parent based on EventType
clickEvent.Url; //string - url on what the user has clicked

我在SendGrid工作,所以请让我知道,如果有什么我可以帮助.

猜你在找的asp.Net相关文章