1

I'm trying to send standard email for standard account verification in my ASP.NET MVC App , but e-mail's isn't delivered or even sent ...all my settings on my sendgrid dashboard is set to default,

On my" IP Access Management" tab in sendgrid Dashboard menu I see my IP address on "Recent Access Attempts" list, so I think the connection from my App is trying to establish...

I'm trying to connect via generated API key from site that I'm hosting on Azure.

I'm using Sendgrid C# client library v6.3.4. and Sendgrid Smtp.Api v1.3.1. installed via NuGet

Here's my code sample :

public class EmailService : IIdentityMessageService
{
    public async Task SendAsync(IdentityMessage message)
    {
        await configSendGridasync(message);

    }
    private async Task configSendGridasync(IdentityMessage message)
    {
        var myMessage = new SendGridMessage();
        myMessage.AddTo(message.Destination);
        myMessage.From = new MailAddress("Joe@contoso.com", "Joe S.");
        myMessage.Subject = message.Subject;
        myMessage.Text = message.Body;
        myMessage.Html = message.Body;

        var transportWeb = new Web("SG.sendgrid general api key blah blah blah");

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

this is my register controller:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> _Register(BigViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser { UserName = model.RegisterViewModel.Email, Email = model.RegisterViewModel.Email };
        var result = await UserManager.CreateAsync(user, model.RegisterViewModel.Password);
        if (result.Succeeded)
        {
            //  Comment the following line to prevent log in until the user is confirmed.
             // await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

            // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
            // Send an email with this link
            string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
             var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
             await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

            // Uncomment to debug locally
            // TempData["ViewBagLink"] = callbackUrl;

            ViewBag.Message = "Check your email and confirm your account, you must be confirmed "
                            + "before you can log in.";

            return View("Info");

        }
        AddErrors(result);
    }

    return View("Register");

}

Where is the problem ?

Thanks in advance.

joint_ops
  • 312
  • 1
  • 5
  • 20
  • Possible duplicate of [asp net mvc sendgrid account email verification](http://stackoverflow.com/questions/35865859/asp-net-mvc-sendgrid-account-email-verification) – joint_ops May 18 '16 at 13:15

1 Answers1

0
 private async Task configSendGridasync(IdentityMessage message,FormCollection fc)
{
    var myMessage = new SendGridMessage();
    myMessage.AddTo(message.Destination);
    myMessage.From = new MailAddress("YourEmail", "Joe S.");
    myMessage.AddTo(fc["Email"]);
    myMessage.Subject = message.Subject;
    myMessage.Text = message.Body;
    myMessage.Html = message.Body;
    var credentials = new NetworkCredential(
             ConfigurationManager.AppSettings["mailAccount"],
             ConfigurationManager.AppSettings["mailPassword"]
             );
    var transportWeb = new Web(credentials);

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

Try this bro.

<appSettings>   
  <add key="webpages:Version" value="3.0.0.0" />
  <!-- Markup removed for clarity. -->      
  <add key="mailAccount" value="xyz" />
  <add key="mailPassword" value="password" />
</appSettings>

store the app settings in the web.config file...

  • Yo. I've tried this approach before with no results... just to make sure... in web config the "mail account value" property is for sengrid account login right ? – joint_ops May 08 '16 at 22:12
  • @joint_ops i'm sorry....i've updated the snippet... myMessage.AddTo("EmailTOSend@example.com"); is missing....try again bro... – Anh Quốc Lê May 09 '16 at 03:48
  • I think that adding myMessage.AddTo("EmailTOSend@example.com") , isn't great idea because I want send Mail to email address which user types in my reg. form input, but I've tried this approach with no result either... If this solution is working well on Your project could you post Your register Controller ? I'll try to make some copy & paste implementation and see what happens . ..Peace – joint_ops May 09 '16 at 09:44
  • @joint_ops okay i got this....i've update the snippet...with FormCollection Input...Fc["Email"] get value from you input tag with ID="Email" Name="Email". – Anh Quốc Lê May 09 '16 at 10:45
  • FYI, using var transportWeb = new Web("SG.sendgrid general api key blah blah blah"); is preferable to (and a better practice than) using var credentials = new NetworkCredential( ConfigurationManager.AppSettings["mailAccount"], ConfigurationManager.AppSettings["mailPassword"] ); var transportWeb = new Web(credentials); – Justin Steele May 09 '16 at 14:07